-
Notifications
You must be signed in to change notification settings - Fork 2
/
getip.sh
executable file
·349 lines (309 loc) · 11 KB
/
getip.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
#!/bin/bash
# Getip by Mickael Asseline (PAPAMICA)
# A simple script to improve the display of certain essential information.
# Use :
# Make the script executable with "chmod +x getip.sh"
# Execute it with "./getip.sh"
# The "-s" parameter is used to launch a speedtest. (Require speedtest-cli)
# You can add an alias to launch it with a simple command.
if [[ $1 =~ "h" ]]; then
echo ""
tput setaf 7; echo "________________________________________________________________________________"
echo ""
echo " Bash script to improve the reading of network informations "
echo " created by Mickael Asseline (PAPAMICA) "
echo ""
echo " Options availables :"
echo " -s Launch a speedtest (require speedtest-cli)"
echo " -i Allows the choice of the interface to display"
echo " -r Change the default route by the chosen interface"
echo " (For speedtest and pings tests)"
echo " -h Diplay this help"
echo ""
echo " Use :"
echo " getip IP informations"
echo " getip -s IP informations + Speedtest"
echo " getip -i IP informations + Choice interface"
echo " getip -ir IP informations + Choice interface + Change route"
echo " getip -sir IP informations + Choice interface + Change route + Speedtest"
echo
exit
fi
network=$(nmcli con show --active)
if [ "$network" = "" ]; then
tput setaf 1; echo "No active interface !"
exit
fi
interface=$(ip route get 8.8.8.8 | awk -F"dev " 'NR==1{split($2,a," ");print a[1]}')
if [ $interface != "unreachable" ]; then
if [[ $1 =~ "i" ]]; then
int1=$(nmcli con show --active | awk '{if(NR==2) print $(NF-1)}')
intname1=$(nmcli con show --active | awk '{if(NR==2) print $NF}')
int2=$(nmcli con show --active | awk '{if(NR==3) print $(NF-1)}')
intname2=$(nmcli con show --active | awk '{if(NR==3) print $NF}')
int3=$(nmcli con show --active | awk '{if(NR==4) print $(NF-1)}')
intname3=$(nmcli con show --active | awk '{if(NR==4) print $NF}')
int4=$(nmcli con show --active | awk '{if(NR==5) print $(NF-1)}')
intname4=$(nmcli con show --active | awk '{if(NR==5) print $NF}')
int5=$(nmcli con show --active | awk '{if(NR==6) print $(NF-1)}')
intname5=$(nmcli con show --active | awk '{if(NR==6) print $NF}')
int6=$(nmcli con show --active | awk '{if(NR==7) print $(NF-1)}')
intname6=$(nmcli con show --active | awk '{if(NR==7) print $NF}')
int7=$(nmcli con show --active | awk '{if(NR==8) print $(NF-1)}')
intname7=$(nmcli con show --active | awk '{if(NR==8) print $NF}')
int8=$(nmcli con show --active | awk '{if(NR==9) print $(NF-1)}')
intname8=$(nmcli con show --active | awk '{if(NR==9) print $NF}')
int9=$(nmcli con show --active | awk '{if(NR==10) print $(NF-1)}')
intname10=$(nmcli con show --active | awk '{if(NR==10) print $NF}')
tput setaf 7; echo "________________________________________________"
echo ""
#echo "Liste des interfaces :"
io=0
if [ -n $int1 ]; then
echo " 1 - $int1 ($intname1)"
((io++))
fi
if [ "$int2" != "" ]; then
echo " 2 - $int2 ($intname2)"
((io++))
fi
if [ "$int3" != "" ]; then
echo " 3 - $int3 ($intname3)"
((io++))
fi
if [ "$int4" != "" ]; then
echo " 4 - $int4 ($intname4)"
((io++))
fi
if [ "$int5" != "" ]; then
echo " 5 - $int5 ($intname5)"
((io++))
fi
if [ "$int6" != "" ]; then
echo " 6 - $int6 ($intname6)"
((io++))
fi
if [ "$int7" != "" ]; then
echo " 7 - $int7 ($intname7)"
((io++))
fi
if [ "$int8" != "" ]; then
echo " 8 - $int8 ($intname8)"
((io++))
fi
if [ "$int9" != "" ]; then
echo " 9 - $int9 ($intname9)"
((io++))
fi
echo ""
read -p " Choice interface (1-$io) : " intchoix
if [ "$intchoix" -lt 1 ] && [ "$intchoix" -gt $io ]; then
echo " Error : choice interface !"
exit
fi
if [ "$intchoix" = 1 ]; then
interface=$intname1
fi
if [ "$intchoix" = 2 ]; then
interface=$intname2
fi
if [ "$intchoix" = 3 ]; then
interface=$intname3
fi
if [ "$intchoix" = 4 ]; then
interface=$intname4
fi
if [ "$intchoix" = 5 ]; then
interface=$intname5
fi
if [ "$intchoix" = 6 ]; then
interface=$intname6
fi
if [ "$intchoix" = 7 ]; then
interface=$intname7
fi
if [ "$intchoix" = 8 ]; then
interface=$intname8
fi
if [ "$intchoix" = 9 ]; then
interface=$intname9
fi
fi
if [[ $1 =~ "r" ]]; then
gateway=$(nmcli dev show $interface |grep IP4.GATEWAY | awk '{print $2 }')
echo ""
tput setaf 3; echo " Requires root rights."
sudo route del default
sudo route add default gw $gateway $interface
tput setaf 2; echo " The default route has been changed to pass to $interface"
fi
# Récuparation des informations
ipadress=$(ifconfig "$interface" | awk '/inet /{ print $2;}')
gateway=$(nmcli dev show $interface |grep IP4.GATEWAY | awk '{print $2 }')
mask=$(ifconfig "$interface" | awk '/netmask/{ print $4;}')
dns=$(nmcli dev show $interface | grep DNS | awk '{if(NR==1) print $2}')
dns2=$(nmcli dev show $interface |grep DNS | awk '{if(NR==2) print $2}')
domain=$(nmcli dev show $interface | grep DOMAIN | sed 's/\s\s*/\t/g' | cut -f 2)
mac=$(cat /sys/class/net/$interface/address)
nom=$(nmcli dev show $interface |grep GENERAL.CONNECTION | awk '{print $2 " " $3 " " $4 " " $5 " " $6}')
mtu=$(cat /sys/class/net/$interface/mtu)
rxerror=$(cat /sys/class/net/$interface/statistics/rx_errors)
txerror=$(cat /sys/class/net/$interface/statistics/tx_errors)
rxdropped=$(cat /sys/class/net/$interface/statistics/rx_dropped)
txdropped=$(cat /sys/class/net/$interface/statistics/tx_dropped)
# Affichage des informations
tput setaf 7; echo "________________________________________________"
echo ""
echo " LAN IP Address : $ipadress"
echo " Gateway : $gateway"
echo " Mask : $mask"
echo ""
echo " DNS Server : $dns"
if [ -n $dns2 ]; then
echo " DNS Server 2 : $dns2"
fi
echo " Interface : $interface"
echo " Name : $nom"
echo " MAC Address : $mac"
echo " MTU : $mtu"
echo " Domain : $domain"
ping="$(ping -c 1 -W 1 google.fr | tail -1| awk -F '/' '{print $5}')"
if [ "$ping" != 0 ]; then
wan=$(curl -s ifconfig.io)
fi
echo " WAN IP Address : $wan"
tput setaf 7; echo "________________________________________________"
echo ""
echo " RX : $rxerror errors / $rxdropped dropped"
echo " TX : $txerror errors / $txdropped dropped"
echo ""
# Vérification de la connexion à Internet via IP
t="0"
t="$(ping -c 1 -W 1 8.8.8.8 | tail -1| awk -F '/' '{print $5}')"
if [ -z "$t" ]; then
tput setaf 1; echo " INTERNET IP : ERROR"
fi
if [ -n "$t" ]; then
t=${t%.*}
((t++))
if [ "$t" -eq 1 ]; then
tput setaf 2; echo " INTERNET IP : OK => <$t ms"
elif [ "$t" -gt 1 ] && [ "$t" -le 100 ]; then
tput setaf 2; echo " INTERNET IP : OK => $t ms"
else
tput setaf 3; echo " INTERNET IP : BAD => $t ms"
fi
fi
# Vérification de la connexion à Internet via DNS
t="0"
t="$(ping -c 1 -W 1 google.fr | tail -1| awk -F '/' '{print $5}')"
if [ -z "$t" ]; then
tput setaf 1; echo " INTERNET DNS : ERROR"
fi
if [ -n "$t" ]; then
t=${t%.*}
((t++))
if [ "$t" -eq 1 ]; then
tput setaf 2; echo " INTERNET DNS : OK => <$t ms"
elif [ "$t" -gt 1 ] && [ "$t" -le 100 ]; then
tput setaf 2; echo " INTERNET DNS : OK => $t ms"
else
tput setaf 3; echo " INTERNET DNS : BAD => $t ms"
fi
fi
# Vérification de la connexion à la passerelle
t="0"
t="$(ping -c 1 -W 1 $gateway | tail -1| awk -F '/' '{print $5}')"
if [ -z "$t" ]; then
tput setaf 1; echo " GATEWAY : ERROR"
fi
if [ -n "$t" ]; then
t=${t%.*}
((t++))
if [ "$t" -eq 1 ]; then
tput setaf 2; echo " GATEWAY : OK => <$t ms"
elif [ "$t" -gt 1 ] && [ "$t" -le 100 ]; then
tput setaf 2; echo " GATEWAY : OK => $t ms"
else
tput setaf 3; echo " GATEWAY : BAD => $t ms"
fi
fi
# Vérification de la connexion au serveur DNS
if [ "$dns" = "" ]; then
tput setaf 1; echo " DNS SERVER : ERROR"
else
t="0"
t="$(ping -c 1 -W 1 $dns | tail -1| awk -F '/' '{print $5}')"
if [ -z "$t" ]; then
tput setaf 1; echo " DNS SERVER : ERROR"
fi
if [ -n "$t" ]; then
t=${t%.*}
((t++))
if [ "$t" -eq 1 ]; then
tput setaf 2; echo " DNS SERVER : OK => <$t ms"
elif [ "$t" -gt 1 ] && [ "$t" -le 100 ]; then
tput setaf 2; echo " DNS SERVER : OK => $t ms"
else
tput setaf 3; echo " DNS SERVER : BAD => $t ms"
fi
fi
fi
tput setaf 7; echo "________________________________________________"
# Si -s est présent, lancement du Speedtest
if [ -z $1 ]; then
exit
fi
if [[ $1 =~ "s" ]]; then
echo ""
speedtest --accept-license > temp.txt
ping=$(grep "Latency" "temp.txt" | awk '{print $2}')
pingfull=$(grep "Latency" "temp.txt" | awk '{print $2, $3, $4, $5, $6}')
upload=$(grep "Upload" "temp.txt" | awk '{print $3}')
download=$(grep "Download" "temp.txt" | awk '{print $3}')
fai=$(grep "ISP" "temp.txt" | awk '{print $2, $3, $4, $5}')
rm temp.txt
echo " FAI : $fai"
ping=${ping%.*}
if [ "$ping" -eq 0 ];then
tput setaf 2; echo " PING : OK => $pingfull"
elif [ "$ping" -gt 0 ] && [ "$ping" -le 100 ]; then
tput setaf 2; echo " PING : OK => $pingfull"
else
tput setaf 3; echo " PING : BAD => $pingfull"
fi
download=${download%.*}
if [ "$download" -eq 0 ];then
tput setaf 1; echo " DOWNLOAD : ERROR"
elif [ "$download" -gt 0 ] && [ "$download" -le 2 ]; then
tput setaf 1; echo " DOWNLOAD : BAD => $download Mbps"
elif [ "$download" -gt 2 ] && [ "$download" -le 20 ]; then
tput setaf 3; echo " DOWNLOAD : OK => $download Mbps"
else
tput setaf 2; echo " DOWNLOAD : GOOD => $download Mbps"
fi
upload=${upload%.*}
if [ "$upload" -eq 0 ];then
tput setaf 1; echo " UPLOAD : ERROR"
elif [ "$upload" -gt 0 ] && [ "$upload" -le 2 ]; then
tput setaf 1; echo " UPLOAD : BAD => $upload Mbps"
elif [ "$upload" -gt 2 ] && [ "$upload" -le 20 ]; then
tput setaf 3; echo " UPLOAD : OK => $upload Mbps"
else
tput setaf 2; echo " UPLOAD : GOOD => $upload Mbps"
fi
tput setaf 7; echo "________________________________________________"
fi
if [[ $1 =~ "r" ]]; then
echo ""
tput setaf 3; read -p " Reset the default route? (y/n) : " dellroute
if [[ "$dellroute" = "y" ]]; then
sudo route del default
sudo systemctl restart NetworkManager
fi
tput setaf 2; echo " Default route reset."
fi
else
tput setaf 1; echo " NO IP"
exit
fi