King0770-Notes-ldapsearch-to-csv

Purpose

Quick and dirty command to convert ldapsearch output to csv output.

ldapsearch -LLL -x -h `zmhostname`:389 -D $zimbra_ldap_userdn -w $zimbra_ldap_password '(&(zimbraAccountStatus=active)(&(!(zimbraIsSystemResource=TRUE))(!(zimbraIsSystemAccount=TRUE))))' cn sn displayName mail | sed -n '1h; 2,$H;${g;s/\n/,/g;p}' | sed 's/,,/\n/g'

Use Ruby!

require 'net/ldap'

ldap = Net::LDAP.new  :host => "zimbra-ldap.example.com",
                      :port => "389",
                      :base => "ou=people,DC=example,DC=com", 
                      :auth => {
                      :method => :simple,
                      :username => "uid=zimbra,cn=admins,cn=zimbra",
                      :password => "s0Mep@sSw0rD"
                      }

filter = Net::LDAP::Filter.construct("(&(zimbraAccountStatus=active)(&(!(zimbraIsSystemResource=TRUE))(!(zimbraIsSystemAccount=TRUE))))")

treebase = "ou=people,dc=example,dc=com"

ldap.search(:base => treebase, :filter => filter) do |entry| 
		print "#{entry.displayName} " 
		print "#{entry.mail} " 
		print "#{entry.zimbraAccountStatus} " 
		puts
end

Sample Output

["Admin Global "] ["admin@test.local", "root@test.local", "postmaster@test.local"] ["active"] ["20160516201714Z"] 
["Rick King"] ["rick@test.local", "rking@test.local", "richard@test.local"] ["active"] ["20160516211320Z"] 
["Bobby Smith"] ["bobby@test.local", "bob@test.local", "rob@test.local", "robert@test.local", "bossman@test.local"] ["active"] ["20160526005052Z"] 
["Bruce Wayne"] ["bwayne@test.local", "bruce@test.local", "batman@test.local", "bruce.wayne@test.local"] ["active"] ["20160526005812Z"] 

Contents in this page are not supported by Zimbra Support Team, please use at your own risk.

Jump to: navigation, search