-
Notifications
You must be signed in to change notification settings - Fork 7
/
asn
executable file
·55 lines (39 loc) · 1.21 KB
/
asn
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
#!/usr/bin/env ruby
########################################################
require 'epitools'
gem 'slop', "~> 3.6"
require 'slop'
########################################################
########################################################
# Parse options
opts = Slop.parse(help: true, strict: true) do
banner "Usage: asn <host(s), url(s), or ip address(es)>"
end
args = ARGV
if args.empty?
puts opts
exit 1
end
########################################################
args.each do |arg|
case arg
when %r{(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})}
ip = $1
when %r{^(?:https?|ftp|gopher)://(.+)/$}, %r{(\w+\.\w+)}
host = $1.gsub(/:\d+$/, '')
puts "<8>[<9>+<8>] <7>Resolving <15>#{host}<7>...".colorize
ip = Resolv.getaddress(host)
else
puts "Error: don't recognize #{arg}"
end
puts "<8>[<3>-<8>] <7>IP address is <15>#{ip}".colorize
begin
reversehost = Resolv.getname(ip)
puts "<8>[<3>-<8>] <7>Reverse host is <15>#{reversehost}".colorize
rescue Resolv::ResolvError
end
puts "<8>[<9>+<8>] <7>Looking up BGP info...".colorize
result = IO.popen(["bgpview-cli", "ip", ip], &:read)
puts
puts result.gsub(/^(\s*\w+): (.+)$/) { "<3>#{$1}: <11>#{$2}" }.colorize
end