6
6
# to make all your piping dreams come true.
7
7
#
8
8
9
-
10
9
require 'slop'
11
10
require 'rest-client'
12
11
require 'json'
13
12
require 'os'
14
13
require 'socket'
15
14
16
-
17
15
# Contains option parsing logic
18
16
def netgeo
19
17
opts = Slop . parse suppress_errors : true do |o |
20
18
o . banner = 'Usage: netgeo [flag]'
21
19
22
20
# Gets WAN address
23
21
o . on '-w' , '--wan' , 'Returns WAN IP' do
24
- url = " https://api.ipify.org"
22
+ url = ' https://api.ipify.org'
25
23
response = RestClient . get ( url )
26
24
puts response
27
25
end
28
26
29
27
# Gets LAN(s)
30
28
o . on '-l' , '--lan' , 'Returns LAN IP(s)' do
31
- lans = Socket . ip_address_list . select { | intf | intf . ipv4_private? } . map { | intf | intf . ip_address }
29
+ lans = Socket . ip_address_list . select ( & : ipv4_private?) . map ( & : ip_address)
32
30
puts lans . join ( ', ' )
33
31
end
34
32
35
33
# Gets Router IP
36
34
o . on '-r' , '--router' , 'Returns Router IP' do
37
35
if OS . linux?
38
- router = %x[ ip route | grep default | head -1 | awk '{print$3}']
36
+ router = ` ip route | grep default | head -1 | awk '{print$3}'`
39
37
elsif OS . mac?
40
- router = %x[ netstat -rn | grep default | head -1 | awk '{print$2}']
38
+ router = ` netstat -rn | grep default | head -1 | awk '{print$2}'`
41
39
else
42
- puts " OS not supported!"
40
+ puts ' OS not supported!'
43
41
exit ( 1 )
44
42
end
45
43
puts router
@@ -48,21 +46,20 @@ def netgeo
48
46
# Gets DNS nameserver
49
47
o . on '-d' , '--dns' , 'Returns DNS Nameserver' do
50
48
if OS . linux?
51
- dns = %x[ cat /etc/resolv.conf | grep nameserver | head -1 | awk '{print$2}']
49
+ dns = ` cat /etc/resolv.conf | grep nameserver | head -1 | awk '{print$2}'`
52
50
elsif OS . mac?
53
- dns = %x[ scutil --dns | grep nameserver | head -1 | awk '{print$3}']
51
+ dns = ` scutil --dns | grep nameserver | head -1 | awk '{print$3}'`
54
52
else
55
- puts " OS not supported!"
53
+ puts ' OS not supported!'
56
54
exit ( 1 )
57
55
end
58
56
puts dns
59
57
end
60
58
61
59
o . string '-m' , '--mac [interface]' , 'Returns MAC address for interface. Ex. eth0'
62
60
63
-
64
61
o . on '-g' , '--geo' , 'Returns Current IP Geodata' do
65
- url = " http://ip-api.com/json/"
62
+ url = ' http://ip-api.com/json/'
66
63
response = RestClient . get ( url )
67
64
info = JSON . parse ( response )
68
65
puts info [ 'query' ]
@@ -83,7 +80,6 @@ def netgeo
83
80
end
84
81
end
85
82
86
-
87
83
# Logic for specific geodata
88
84
options = opts [ :s ]
89
85
ip = opts . arguments || ''
@@ -107,12 +103,10 @@ def netgeo
107
103
puts info [ 'isp' ] if options . include? 'isp'
108
104
end
109
105
110
-
111
106
# Logic for specific MAC address
112
107
options = opts [ :m ]
113
- unless options == nil
114
- puts %x[ifconfig #{ options } | grep -o -E '([[:xdigit:]]{1,2}:){5}[[:xdigit:]]{1,2}']
115
- end
108
+ return if options . nil?
109
+ puts `ifconfig #{ options } | grep -o -E '([[:xdigit:]]{1,2}:){5}[[:xdigit:]]{1,2}'`
116
110
end
117
111
118
112
netgeo
0 commit comments