Class: ADAssault::DNS::FindDCs
- Inherits:
-
Object
- Object
- ADAssault::DNS::FindDCs
- Defined in:
- lib/adassault/dns/find_dcs.rb
Overview
Spot all domain controllers in a Microsoft Active Directory environment
Instance Attribute Summary collapse
-
#dcs ⇒ Hash
readonly
Hash containing all DCs.
Instance Method Summary collapse
-
#dc_fqdn ⇒ Array
Get DC(s) FQDN directly from the DNS server.
-
#dc_ip ⇒ Array
Get DC(s) IP address name directly from the DNS server.
-
#dc_name ⇒ Array
Get DC(s) computer name directly from the DNS server.
-
#display ⇒ nil
Display a CLI-friendly output listing all DCs with their short name, FQDN and IP addresses.
-
#display_old ⇒ Object
deprecated
Deprecated.
Use #display instead.
-
#fetch_dcs ⇒ Hash
Generates a hash containing all DCs.
-
#initialize(ad_domain, dns_opts = nil) ⇒ FindDCs
constructor
Create the FindDCs object.
Constructor Details
#initialize(ad_domain, dns_opts = nil) ⇒ FindDCs
Create the FindDCs object.
26 27 28 29 30 |
# File 'lib/adassault/dns/find_dcs.rb', line 26 def initialize(ad_domain, dns_opts = nil) @ad_domain = ad_domain @dns_opts = dns_opts @dcs = fetch_dcs end |
Instance Attribute Details
#dcs ⇒ Hash (readonly)
Hash containing all DCs. Cached result of #fetch_dcs.
16 17 18 |
# File 'lib/adassault/dns/find_dcs.rb', line 16 def dcs @dcs end |
Instance Method Details
#dc_fqdn ⇒ Array
Get DC(s) FQDN directly from the DNS server. Not cached in ‘@dcs`.
40 41 42 43 44 45 46 47 48 49 |
# File 'lib/adassault/dns/find_dcs.rb', line 40 def dc_fqdn Resolv::DNS.open(@dns_opts) do |dns| # _kerberos._tcp, _kpasswd._tcp, _ldap._tcp works too but are not MS only # _kerberos._tcp.dc._msdcs # _ldap._tcp.pdc._msdcs, _gc._tcp # _udp variants ress = dns.getresources "_ldap._tcp.dc._msdcs.#{@ad_domain}", Resolv::DNS::Resource::IN::ANY ress.map { |x| x.target.to_s } end end |
#dc_ip ⇒ Array
Get DC(s) IP address name directly from the DNS server. Not cached in ‘@dcs`.
70 71 72 73 74 75 |
# File 'lib/adassault/dns/find_dcs.rb', line 70 def dc_ip Resolv::DNS.open(@dns_opts) do |dns| ress = dns.getresources "gc._msdcs.#{@ad_domain}", Resolv::DNS::Resource::IN::A ress.map { |x| x.address.to_s } end end |
#dc_name ⇒ Array
Get DC(s) computer name directly from the DNS server. Not cached in ‘@dcs`. Call #dc_fqdn and extract the name from it (substract the domain).
59 60 61 |
# File 'lib/adassault/dns/find_dcs.rb', line 59 def dc_name dc_fqdn.map { |x| x[...-@ad_domain.size - 1] } end |
#display ⇒ nil
Display a CLI-friendly output listing all DCs with their short name, FQDN and IP addresses.
103 104 105 106 107 108 |
# File 'lib/adassault/dns/find_dcs.rb', line 103 def display dcs.each do |short_name, dc| puts "#{Paint[short_name, :bold, 'dark turquoise']} (#{Paint[dc[:fqdn], 'cyan']}) - #{Paint[dc[:ips].join(', '), 'aquamarine']}" end end |
#display_old ⇒ Object
Use #display instead.
113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/adassault/dns/find_dcs.rb', line 113 def display_old puts Paint['DC(s) name', :underline, :bold, 'dark turquoise'] dc_name.each do |name| puts Paint["🔍 #{name}"] end puts Paint["\nDC(s) FQDN", :underline, :bold, 'cyan'] dc_fqdn.each do |fqdn| puts Paint["🔍 #{fqdn}"] end puts Paint["\nDC(s) IP address", :underline, :bold, 'aquamarine'] dc_ip.each do |ip| puts Paint["🔍 #{ip}"] end end |
#fetch_dcs ⇒ Hash
Generates a hash containing all DCs. The key is the short name, the value is a hash with two keys: the FQDN and the IPs addresses. This method is called while instantiating the class and teh result stored in ‘@dcs` attribute.
84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/adassault/dns/find_dcs.rb', line 84 def fetch_dcs h = {} dc_fqdn.each do |fqdn| Resolv::DNS.open(@dns_opts) do |dns| ress = dns.getresources fqdn, Resolv::DNS::Resource::IN::A short_name = fqdn[...-@ad_domain.size - 1].upcase h[short_name] = { fqdn: fqdn, ips: ress.map { |x| x.address.to_s } } end end h end |