forked from ManageIQ/manageiq
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ldap_ping.rb
executable file
·59 lines (48 loc) · 1.66 KB
/
ldap_ping.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/usr/bin/env ruby
require File.expand_path('../config/environment', __dir__)
$log = Vmdb::Loggers.create_logger("ldap_ping.log")
$log.level = Logger::INFO
#################################
$log.info("==================================")
$log.info("Starting")
unless MiqLdap.using_ldap?
$log.info("Not Configured to use LDAP")
exit
end
class MiqLdap
def self.resolve_ldap_host?
false
end
end
ldap_hosts = Settings.authentication.ldaphost
username = Settings.authentication.bind_dn
password = Settings.authentication.bind_pwd
bind_timeout = Settings.authentication.bind_timeout.to_i_with_method
if ldap_hosts.to_s.strip.empty?
$log.info("LDAP Host cannot be blank")
exit
end
ldap_addresses = []
Array.wrap(ldap_hosts).each do |host|
_canonical, _aliases, _type, *addresses = TCPSocket.gethostbyname(host)
$log.info("Resolved host <#{host}> has these IP Address: #{addresses.inspect}")
ldap_addresses += addresses
end
ldap_addresses.each do |address|
$log.info("----------------------------------")
$log.info("Binding to LDAP: Host: <#{address}>, User: <#{username}>...")
ldap = MiqLdap.new(:host => address)
raw_ldap = ldap.ldap
raw_ldap.authenticate(username, password)
Timeout.timeout(bind_timeout) do
if raw_ldap.bind
$log.info("Binding to LDAP: Host: <#{address}>, User: <#{username}>... successful")
else
$log.warn("Binding to LDAP: Host: <#{address}>, User: <#{username}>... unsuccessful because <#{raw_ldap.get_operation_result.message}>")
end
end
rescue Exception => err
$log.warn("Binding to LDAP: Host: <#{address}>, User: <#{username}>... failed because <#{err.message}>")
end
$log.info("Done")
exit 0