-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnettest.sh
executable file
·195 lines (160 loc) · 4.78 KB
/
nettest.sh
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#!/bin/bash
# test the inetz
TMPFILE=/tmp/$(whoami)-$(basename $0).tmp
ALLGOOD=y
# depends
NMCLI=$(which nmcli)
WGET=$(which wget)
NPING=$(which nping)
MTR=$(which mtr)
PING=$(which ping)
# library
function ntStandardPing()
# send $1 standard echo requests to $2 (8.8.8.8, by default) cisco style
# (print a "!" in each place where we receive a reply or
# print a "." in each place where we do not receive a reply)
# return number of packets received
{
count=${1:-15}
server_ip=${2:-"8.8.8.8"}
ret=0
for (( i = 0; i < $count; i++ ))
do
if ( $PING -c 1 -W 2 $server_ip >/dev/null 2>&1 ); then
echo -ne "!"
let ret++
else
echo -ne "."
fi
done
echo # newline after bangs and dots
return $ret
}
# some basic tests
echo -e "Starting basic tests..."
# main()
if [[ "${BASH_SOURCE[0]}" == "${0}" ]];
then
# ping default gateway
echo -en "Ping default gateway:\t"
ntStandardPing 10 $gw
fi
#default gateway?
echo -en "L3\t"
s=$(route -n | grep "^0.0.0.0" | awk '{ print $2 }');
$PING -c 1 $s >/dev/null 2>&1; r=$?;
if [ $r -eq 0 ]; then
echo -ne "OK";
else
echo -ne "NOT OK";
ALLGOOD="n";
fi;
echo -e "\t(${s:-"No Gateway"})"
#google dns servers?
s=8.8.8.8;
$PING -c 1 $s >/dev/null 2>&1;
if [ $? -eq 0 ]; then
echo -e "Ping remote node\tOK\t($s)";
else
echo -e "Ping remote node\tNOT OK\t($s)";
ALLGOOD="n";
fi;
# old-style nameserver?
s=$(grep -i nameserver /etc/resolv.conf | awk '{ print $2 }');
$PING -c 1 $s >/dev/null 2>&1;
if [ $? -eq 0 ]; then
echo -e "Nameserver\tOK\t($s via /etc/resolv.conf)";
else
echo -e "Nameserver\tNOT OK\t($s via /etc/resolv.conf)";
ALLGOOD="N";
fi;
# new-style nameserver?
if [ "x$NMCLI" == "x" ]; then
echo "Network manager not installed (time to upgrade Linux?)";
else
s=$($NMCLI dev show | grep "IP4\.DNS" | awk '{ print $2 }');
$PING -c 1 $s >/dev/null 2>&1;
if [ $? -eq 0 ]; then
echo -e "Nameserver\tOK\t($s via $NMCLI)";
else
echo -e "Nameserver\tNOT OK\t(${s:-"NO ADDRESS"} via $NMCLI)";
ALLGOOD="n";
fi;
fi;
# can we resolve and connect to web via google.com?
s=$(host -t A google.com | grep "has address" | head -n 1 | awk '{ print $4 }');
if [ "x$s" == "x" ]; then
echo -e "Inet L7 DNS\tNOT OK\t(Unable to resolve google.com)"
$PING -c 1 74.125.129.102 >/dev/null 2>&1
if [ $? -eq 0 ]; then
echo -e "Google.com\tOK\t(74.125.129.102 via hard-coded address is reachable)"
else
echo -e "Google.com\tNOT OK\t(74.125.129.102 via hard-coded address is unreachable)"
ALLGOOD="n";
fi
else
echo -e "Inet L7 DNS\tOK\t(Resolved google.com to $s)"
$NPING -c 1 --tcp-connect $s -p 443 >/dev/null 2>&1
if [ $? -eq 0 ]; then
echo -e "Inet L7 HTTPS\tOK\t(TCP connect to https://google.com successful)"
else
echo -e "Inet L7 HTTPS\tNOT OK\t(TCP connect to https://google.com unsuccessful)"
ALLGOOD="n";
fi
fi
# some extended tests
if [ $ALLGOOD == "n" ]; then
echo -e "\nSome basic tests failed. Exiting."
exit 1
else
echo -e "\nSome extended tests..."
fi
# download something over http
# check NTP time vs local time for https
# download something over https
# $PING flood to default gateway (local)
s=$(route -n | grep "^0.0.0.0" | awk '{ print $2 }');
if [ "$(id -u)" -eq 0 ]; then
echo -ne "Fast local ping flood ($s):\t"
$PING -q -c 50 -f $s | grep "packet loss" | \
awk '{ print $4 " out of " $1 " received (" $6 " loss)" }'
else
echo -ne "Slow local ping flood ($s):\t"
$PING -q -c 25 -f -i 0.2 8.8.8.8 | grep "packet loss" | \
awk '{ print $4 " out of " $1 " received (" $6 " loss)" }'
fi
# $PING flood to google dns (remote)
if [ "$(id -u)" -eq 0 ]; then
echo -ne "Fast remote ping flood (8.8.8.8):\t"
$PING -q -c 50 -f 8.8.8.8 | grep "packet loss" | \
awk '{ print $4 " out of " $1 " received (" $6 " loss)" }'
else
echo -ne "Slow remote ping flood (8.8.8.8):\t"
$PING -q -c 25 -f -i 0.2 8.8.8.8 | grep "packet loss" | \
awk '{ print $4 " out of " $1 " received (" $6 " loss)" }'
fi
#mtr (if found) or traceroute/path for multiple NAT, etc
if [ "x$MTR" == "x" ]; then
echo -e "mtr not found. Giving up." # maybe i'll add traceroute/tracepath later. i like mtr
else
echo -ne "Hops to Google DNS:\t\t\t"
$MTR -4nlc 1 google.com | grep "^h" | awk '{ print $3 }' >$TMPFILE
hops_text=$(wc -l $TMPFILE | awk '{ print $1 }')
let hops="$hops_text - 1" #mtr adds an extra hop on the end? i hope subtracting 1 is ok :S
echo -e "$hops"
echo -e "First 5 hops outbound:\t\t\t$(cat $TMPFILE | head -n 1)"
for i in $(cat $TMPFILE | head -n 5 | tail -n 4); do
echo -e "\t\t\t\t\t$i"
done
#clean up
rm $TMPFILE
unset hops
fi
# network benchmark?
# send packets as fast as possible and time how long it takes
# (but why? maybe i won't do this.. what does it accomplish?)
# nmap some stuff maybe? idk
# internet benchmarks?
#download small pictures over http and https
#download large files and time it
exit 0