forked from rjorgenson/geeklet-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
netstats.rb
executable file
·144 lines (124 loc) · 3.39 KB
/
netstats.rb
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#!/usr/bin/env ruby
#coding: utf-8
require 'open-uri'
require 'optparse'
options = {}
parser = OptionParser.new do |opts|
opts.banner = "Usage: netstats.rb [options]"
options[:iface] = 'en1'
opts.on( '-i IFACE', '--iface IFACE', 'Set iface to monitor' ) do |iface|
options[:iface] = iface
end
options[:wifi] = false
opts.on( '-w', '--wifi', 'iface is a wireless access point' ) do
options[:wifi] = true
end
options[:ping] = false
opts.on('-p', '--ping', 'run ping test to specified server[default: google.com]' ) do
options[:ping] = true
end
options[:server] = 'google.com'
opts.on( '-s', '--server', 'set the server to gauge ping response[google.com]' ) do |server|
options[:server] = server
end
opts.on( '-h', '--help', 'Displays this help screen' ) do
puts opts
exit
end
end
parser.parse!
class Net_Stats
AIRPORT_UTILITY = "/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport"
def initialize(opts)
@options = opts
end
def get_internal_ip
ifc = %x{ifconfig #{@options[:iface]}}
if ifc.each_line.grep(/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/)[0] == nil then
iip = "none"
else
iip = ifc.each_line.grep(/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/)[0].strip.split(" ")[1]
end
return iip
end
def get_external_ip
begin
return %x{curl -s http://icanhazip.com}.strip
rescue
return "none"
end
end
def get_access_point
ap = %x{#{Net_Stats::AIRPORT_UTILITY} -I | grep ' SSID' | awk '{ print $2 }'}
if ap.to_s == "" then
return "none"
else
return ap.to_s.strip
end
#ap.to_s == "" ? return "none" : return ap
end
def get_response_time
begin
return %x{ping -c 1 -t 2 -Q #{@options[:server]}}.each_line.grep(/round-trip/)[0].strip.split(" = ")[1].split("/")[2].to_f.round.to_s + "ms"
rescue
return "No Network"
end
end
def get_txrx_totals
rx = %x{netstat -I #{@options[:iface]} -b | grep -e "#{@options[:iface]}" -m 1 | awk '{print $7}'}.to_i
tx = %x{netstat -I #{@options[:iface]} -b | grep -e "#{@options[:iface]}" -m 1 | awk '{print $10}'}.to_i
return self.human_readable_bytes(tx) + " : " + self.human_readable_bytes(rx)
end
def human_readable_bytes(bytes)
level = 0
until bytes < 1024
remainder = bytes % 1024
bytes = bytes / 1024
level += 1
end
remainder = ((remainder.to_f / 1024) * 100).to_i
output = bytes.to_s + "." + remainder.to_s
case level
when 0
output = output + " B"
when 1
output = output + " KB"
when 2
output = output + " MB"
when 3
output = output + " GB"
when 4
output = output + " TB"
end
end
end
netstats = Net_Stats.new(options)
iface = options[:iface]
ping_server = options[:server]
internal_ip = netstats.get_internal_ip
external_ip = netstats.get_external_ip
txrx = netstats.get_txrx_totals
if options[:wifi] then
access_point = netstats.get_access_point
else
access_point = "none"
end
if external_ip == "none" then
ping_time = "No network"
else
if options[:ping] then
ping_time = netstats.get_response_time
else
ping_time = 'disabled'
end
end
output =
<<-EOS
Internal IP : #{internal_ip}
External IP : #{external_ip}
Interface : #{iface}
TX:RX : #{txrx}
Access Point : #{access_point}
Ping Time : #{ping_time} (#{ping_server})
EOS
puts output