Verified Commit 3c496512 authored by Nick Thomas's avatar Nick Thomas
Browse files

Reduce execution time from 90 seconds to 2 seconds

parent 0a9b686e
...@@ -4,4 +4,4 @@ set -euo pipefail ...@@ -4,4 +4,4 @@ set -euo pipefail
IFS=$'\n\t' IFS=$'\n\t'
echo -e "Date: $(date '+%F %T')\n" echo -e "Date: $(date '+%F %T')\n"
exec ruby "$(dirname $0)/hostinfo.rb" "$@" | column -t -s $'\t' ruby "$(dirname $0)/hostinfo.rb" "$@" | column -t -s $'\t'
#!/usr/bin/env ruby -w #!/usr/bin/env ruby
def resolv(hostname, ptr: false) Thread.abort_on_exception = true
result = `dig #{'-x' if ptr} #{hostname} +short | tail -1`.strip
result == "" ? nil : result
end
def whois(thing) require 'net/https'
result = `whois #{thing} | grep OrgName | sed -E 's/^.*: +//'`.strip
result == "" ? nil : result
end
def network_owner(hostname) class Check
ip = resolv(hostname) # These are provided or computed
if ip attr_reader :hostname, :https_url, :ssh_port
whois(ip) || "N/A"
else # These are looked up in parallel
"DOES_NOT_RESOLVE" attr_reader :ip, :https_response, :ssh_port_open, :network_owner, :rev_name
def initialize(hostname)
@hostname = hostname
@https_url = "https://#{hostname}"
@ssh_port = hostname.start_with?('altssh') ? 443 : 22
end end
end
def rev_name(hostname) def execute
ip = resolv(hostname) parallel_lookup!
if ip [
rev_ip = resolv(ip, ptr: true) hostname,
network_owner,
rev_ip || ip rev_name,
else https_status,
"DOES_NOT_RESOLVE" ssh_port_open,
redirect
].join("\t")
end end
end
def ssh_port_open(hostname, port) def https_status
result = `ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -oConnectTimeout=5 -p "#{port}" "git@#{hostname}" 2>/dev/null`.strip return "Invalid" unless https_response
if result =~ /Welcome to GitLab/ https_response.code
"Yes"
else
"No"
end end
end
def ssh_port(hostname) def redirect
if hostname.start_with?('altssh') return "-" unless https_response && https_response.key?('Location')
443
else https_response['Location'][0..39]
22
end end
end
def http_status(url) private
status = `(curl --insecure --head --connect-timeout 5 --max-time 5 --silent #{url} | head -1 | cut -d\\ -f2) || echo "Error"`.strip
if status == "" def parallel_lookup!
"Invalid" # Checking whois and reverse IP lookup depends on the IP being resolved,
else # so set it off separately
status ip_thread = Thread.new { @ip = resolv(hostname) }
threads = [
Thread.new { @https_response = curl(https_url) },
Thread.new { @ssh_port_open = check_ssh }
]
# Once this join completes, it's safe to reference the IP
ip_thread.join
threads.concat([
Thread.new { @network_owner = check_network_owner },
Thread.new { @rev_name = check_rev_name }
])
threads.map(&:join)
end end
end
def popen(cmd)
cmd.push(err: :close)
result = IO.popen(cmd) { |io| io.read.strip }
if $?.success?
result
else
nil
end
end
def check_ssh
result = popen(%W[
ssh -o UserKnownHostsFile=/dev/null \
-o StrictHostKeyChecking=no \
-oConnectTimeout=5 \
-p #{ssh_port} \
-q -T \
git@#{hostname}
])
if result =~ /Welcome to GitLab/
"Yes"
else
"No"
end
end
def check_network_owner
return "DOES_NOT_RESOLVE" unless ip
whois(ip) || "N/A"
end
def check_rev_name
return "DOES_NOT_RESOLVE" unless ip
resolv(ip, ptr: true) || ip
end
def resolv(hostname, ptr: false)
cmd = ['dig', '+short']
cmd << '-x' if ptr
cmd << hostname
result = popen(cmd)
return nil unless result
result.split("\n")[-1]
end
def whois(thing)
result = popen(%W[whois #{thing}])
return nil unless result
orgname = result.lines.find { |l| l =~ /OrgName:/ }
orgname&.split(":", 2)[1].strip
end
def curl(url)
result = popen(%W[curl --insecure --head --connect-timeout 5 --max-time 5 --silent #{url}])
return nil unless result
lines = result.lines.map(&:chomp)
return nil unless lines.size > 1
def redirect(url) version, code, message = lines[0].split(" ")
result = `curl --insecure --head --connect-timeout 5 --max-time 5 --silent "#{url}" | grep Location | sed -E 's/^.*: +//' |cut -c 1-40`
if $?.success? kls = Net::HTTPResponse::CODE_TO_OBJ[code] || Net::HTTPResponse
result.strip out = kls.new(version, code, message)
else
"-" lines[1..-1].map do |line|
key, value = line.split(": ")
out.add_field(key, value)
end
out
end end
end end
class Checks
class << self
def header
["HOST", "NETWORK", "REV", "HTTPS", "SSH", "REDIRECT"].join("\t")
end
end
printf("%s\t%s\t%s\t%s\t%s\t%s\t\n", "HOST", "NETWORK", "REV", "HTTPS", "SSH", "REDIRECT") attr_reader :checks
ARGV.each do |hostname|
printf( def initialize(hostnames)
"%s\t%s\t%s\t%s\t%s\t%s\n", @checks = hostnames.map { |hostname| Check.new(hostname) }
hostname, end
network_owner(hostname),
rev_name(hostname), def execute
http_status("https://#{hostname}"), threads = checks.map { |check| Thread.new { check.execute } }
ssh_port_open(hostname, ssh_port(hostname)), threads.map(&:value)
redirect("https://#{hostname}") end
)
end end
results = Checks.new(ARGV).execute
STDOUT.puts Checks.header
results.each { |result| STDOUT.puts result }
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment