Skip to content

Instantly share code, notes, and snippets.

@willgarcia
Created October 20, 2016 21:53
Show Gist options
  • Save willgarcia/83c7b78eefd6933764f37bb557298812 to your computer and use it in GitHub Desktop.
Save willgarcia/83c7b78eefd6933764f37bb557298812 to your computer and use it in GitHub Desktop.
#!/usr/local/bin/ruby
require 'json'
require 'optparse'
trap "SIGINT" do
puts "Exiting.."
Process.exit(1)
end
options = {}
OptionParser.new do |opt|
opt.on('--ping') { |o| options[:ping] = o }
opt.on('--port-scan') { |o| options[:port_scan] = o }
end.parse!
puts "-- Ping disabled, enable it with --ping" if !options[:ping]
puts "-- Port scan disabled, enable it with --port-scan" if !options[:port_scan]
file = File.open(ARGV[0], "r")
machines = file.read
JSON.parse(machines).group_by{ |h| [h['definition']['machine_role']] }.each do |machine_role, matches|
c_pdc = c_sdc = c_sdc_dr = 0
matches.map { |machine|
case machine['definition']['zone']
when 'pdc'
c_pdc += 1
when 'sdc'
c_sdc += 1
when 'sdc-dr'
c_sdc_dr += 1
else
raise 'Invalid zone'
end
}
puts ""
puts "#{machine_role} - #{matches.length} machine(s) found - PDC(#{c_pdc}) - SDC (#{c_sdc}) - DR (#{c_sdc_dr})."
puts "FQDN | ZONE | PING | PORTS"
puts "-------------------|--------|---------------|------"
matches.each do |machine|
machine_fqdn = machine['fqdn'].ljust(18)
machine_zone = machine['definition']['zone'].ljust(6)
machine_state = 'disabled'.ljust(13)
if options[:ping]
system "ping -c 3 #{machine['fqdn']} 1>/dev/null 2>/dev/null"
if $?.exitstatus == 0
machine_state = 'ok'.ljust(13)
end
end
ports = 'disabled'
if options[:port_scan]
ports = `nmap -oG - #{machine['fqdn']} 2>/dev/null | grep 'Ports' 2>/dev/null`
if $?.exitstatus > 0
ports = 'na'
else
ports = ports[/Ports:(.*)Ignored State/,1]
ports = ports.scan(/\d+/).join(',')
end
end
puts "#{machine_fqdn} | #{machine_zone} | #{machine_state} | #{ports}"
end
end
Process.exit(0)
source 'https://rubygems.org'
gem "table_print"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment