#! /usr/local/bin/ruby -Ke require 'ldap' require 'uconv' require 'parsearg' LDAPHOST = "ldap.HOGE.co.jp" LDAPPORT = LDAP::LDAP_PORT SEARCHBASE = "ou=FUGA, o=HOGE.co.jp" ATTRS = 'ou:title:cn:mail' parseArgs(0, nil, nil, 'h:', 'p:', 'b:', 'a:') ldaphost = $OPT_h || LDAPHOST ldapport = $OPT_p || LDAPPORT searchbase = $OPT_b || SEARCHBASE attrs = $OPT_a || ATTRS if ARGV.length == 1 && ARGV[0] =~ /^\(.*\)$/ searchstr = ARGV[0] elsif ARGV.length > 0 searchstr = '(|' ARGV.each do |arg| arg.gsub!(/[\*\(\)\\\0]/) {|m| sprintf('\%02x', m[0]) } searchstr += '(cn=*' + arg + '*)' end searchstr += ')' else puts "Usage: #{$0} [-h ldap_host] [-p ldap_port] [-b search_base]" + "[-a attributes] filters..." exit 1 end printf("search: %s\n", searchstr) conn = LDAP::Conn.new(ldaphost, ldapport) conn.bind do conn.search(searchbase, LDAP::LDAP_SCOPE_SUBTREE, Uconv.euctou8(searchstr), attrs.split(':')) do |e| puts e.dn e.attrs.each do |a| printf "%s\t%s\n", a, Uconv.u8toeuc(e.vals(a).join('/')) end puts end end