King0770-Notes-ldapsearch-to-csv

Revision as of 22:13, 25 May 2016 by King0770 (talk | contribs)

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 'rubygems'
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.zimbraMailDeliveryAddress} " 
		print "#{entry.zimbraAccountStatus} " 
		puts
end

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

Jump to: navigation, search