Skip to content

Instantly share code, notes, and snippets.

@bortels
Last active December 17, 2016 04:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bortels/c9931998b5921d696584406ec6ebf6fc to your computer and use it in GitHub Desktop.
Save bortels/c9931998b5921d696584406ec6ebf6fc to your computer and use it in GitHub Desktop.
take ldapsearch output and save big json blob
#!/usr/bin/env ruby
# Save all AD objects in a big json blob you can mess around with
require 'json'
require 'Open3'
require 'pry'
# replace below with correct AD server, baseDN, and bind account DN and password
cmd = "ldapsearch -E pr=1000/noprompt -xLLL " +
"-H 'ldap://10.10.10.20' " +
"-b 'dc=example,dc=com' " +
"-s sub " +
"-D 'CN=service bind,OU=Service Accounts,DC=example,DC=com' " +
"-w 'PASSWORD_FOR_THAT_ACCOUNT' " +
"'*'"
stdin, stdout, stderr = Open3.popen3(cmd)
big = {}
o = {}
currentkey = ''
stdout.each do |line|
line.chomp!
next if line =~ /^#/
if line == '' # end of record
if o.size > 0
if o.key?('dn')
big[o['dn']] = o
#puts o.to_json
end
end
o = {}
currentkey = nil
next
end
if line =~ /^\s+(.*)/ # line starting with space is continuation
next if currentkey.nil?
d = o[currentkey]
if d.instance_of? Array
i = o[currentkey].size()-1
t = o[currentkey][i] + $1
o[currentkey][i] = t
next
else
d = d + $1
o[currentkey] = d
next
end
end
if line =~ /^(.*): (.*)$/
k=$1
currentkey = k
v=$2
d = o[k]
if d.nil?
o[k] = v
next
end
if d.instance_of? Array
o[k] << v
next
end
o[k] = [o[k]]
o[k] << v
next
end
puts "ERROR: Something is screwy"
binding.pry
end
File.open("ad.json", 'w').write(big.to_json)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment