Verified Commit 0a9b686e authored by Nick Thomas's avatar Nick Thomas
Browse files

Straightforward port of hostinfo to ruby

parent 4d1ae181
......@@ -3,76 +3,5 @@
set -euo pipefail
IFS=$'\n\t'
function network_owner() {
local ip=$(dig ${1} +short|tail -1)
if [[ -z $ip ]]; then
echo "DOES_NOT_RESOLVE"
else
local who_is=$(whois ${ip}|grep OrgName|sed -E 's/^.*: +//')
if [[ -z $who_is ]]; then
echo "N/A"
else
echo "$who_is"
fi
fi
}
function rev_name() {
local ip=$(dig ${1} +short|tail -1)
if [[ -z $ip ]]; then
echo "DOES_NOT_RESOLVE"
else
local rev_ip=$(dig -x ${ip} +short)
if [[ -z $rev_ip ]]; then
echo $ip
else
echo $rev_ip
fi
fi
}
function ssh_port_open() {
result=$(ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -oConnectTimeout=5 -p "$2" "git@$1" 2>/dev/null)
if [[ "$result" =~ "Welcome to GitLab" ]]; then
echo Yes
else
echo No
fi
}
function ssh_port() {
if [[ $1 == altssh* ]]; then
echo 443
else
echo 22
fi
}
function http_status() {
local status=$((curl --insecure --head --connect-timeout 5 --max-time 5 --silent ${1}|head -1|cut -d\ -f2) || echo "Error")
if [[ -z $status ]]; then
echo "Invalid"
else
echo "$status"
fi
}
function redirect() {
if ! curl --insecure --head --connect-timeout 5 --max-time 5 --silent "$1"|grep Location|sed -E 's/^.*: +//'|cut -c 1-40; then
echo "-"
fi
}
function run() {
printf '%s\t%s\t%s\t%s\t%s\t%s\t\t%s\n' "HOST" "NETWORK" "REV" "HTTPS" "SSH" "REDIRECT"
for host in "$@"
do
printf "%s\t%s\t%s\t%s\t%s\t%s\n" "$host" $(network_owner $host) $(rev_name $host) $(http_status "https://$host") $(ssh_port_open $host $(ssh_port $host)) $(redirect "https://$host")
done
}
echo -e "Date: $(date '+%F %T')\n"
run $@ | column -t -s $'\t'
exec ruby "$(dirname $0)/hostinfo.rb" "$@" | column -t -s $'\t'
#!/usr/bin/env ruby -w
def resolv(hostname, ptr: false)
result = `dig #{'-x' if ptr} #{hostname} +short | tail -1`.strip
result == "" ? nil : result
end
def whois(thing)
result = `whois #{thing} | grep OrgName | sed -E 's/^.*: +//'`.strip
result == "" ? nil : result
end
def network_owner(hostname)
ip = resolv(hostname)
if ip
whois(ip) || "N/A"
else
"DOES_NOT_RESOLVE"
end
end
def rev_name(hostname)
ip = resolv(hostname)
if ip
rev_ip = resolv(ip, ptr: true)
rev_ip || ip
else
"DOES_NOT_RESOLVE"
end
end
def ssh_port_open(hostname, port)
result = `ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -oConnectTimeout=5 -p "#{port}" "git@#{hostname}" 2>/dev/null`.strip
if result =~ /Welcome to GitLab/
"Yes"
else
"No"
end
end
def ssh_port(hostname)
if hostname.start_with?('altssh')
443
else
22
end
end
def http_status(url)
status = `(curl --insecure --head --connect-timeout 5 --max-time 5 --silent #{url} | head -1 | cut -d\\ -f2) || echo "Error"`.strip
if status == ""
"Invalid"
else
status
end
end
def redirect(url)
result = `curl --insecure --head --connect-timeout 5 --max-time 5 --silent "#{url}" | grep Location | sed -E 's/^.*: +//' |cut -c 1-40`
if $?.success?
result.strip
else
"-"
end
end
printf("%s\t%s\t%s\t%s\t%s\t%s\t\n", "HOST", "NETWORK", "REV", "HTTPS", "SSH", "REDIRECT")
ARGV.each do |hostname|
printf(
"%s\t%s\t%s\t%s\t%s\t%s\n",
hostname,
network_owner(hostname),
rev_name(hostname),
http_status("https://#{hostname}"),
ssh_port_open(hostname, ssh_port(hostname)),
redirect("https://#{hostname}")
)
end
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