-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathipspec_v3.0.sh
412 lines (350 loc) · 17.8 KB
/
ipspec_v3.0.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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
#!/bin/bash
#
# name : ipspec.sh
# desciption : show IP and LAN Information
# autor : Speefak ( [email protected] )
# licence : (CC) BY-NC-SA
# version : 3.0
#
#------------------------------------------------------------------------------------------------------------
############################################################################################################
####################################### define global variables ########################################
############################################################################################################
#------------------------------------------------------------------------------------------------------------
RequiredPackets="lynx curl geoip-bin nmap speedtest-cli"
SpeedtestTempFile=/tmp/spdt.tmp
MaxScriptExecutionIntervalBandwidth=180
Codename=$(lsb_release -d | tr -d : | cut -f 2)
Architecture=$(getconf LONG_BIT)
LANDevice=$(ip route | grep default | awk -F "dev " '{print $2}' | cut -d " " -f1 | sort -u)
LANIP=$(ip -br addr show $LANDevice | awk '{print $3}' | cut -d "/" -f1)
GatewayIP=$(ip route | grep default | sed -n 1p | cut -d " " -f3)
GatewayMAC=$(ip neigh | grep -w $GatewayIP 2> /dev/null | head -n1 | tr " " "\n" | grep "[[:alnum:]][[:alnum:]]:")
DNSServerList=$(cat /etc/resolv.conf |grep -i '^nameserver'|cut -d ' ' -f2 | tr "\n" " " | sed 's/ $//'| sed 's/ /,/g' )
DNSServerlistNmCLI="$(nmcli -t --fields NAME con show --active 2>/dev/null)"
FritzboxIP=$GatewayIP
FritzboxPrintNewIPDelay=3
Version=$(cat $(readlink -f $(which $0)) | grep "# version" | head -n1 | awk -F ":" '{print $2}' | sed 's/ //g')
ScriptFile=$(readlink -f $(which $0))
ScriptName=$(basename $ScriptFile)
#------------------------------------------------------------------------------------------------------------
############################################################################################################
########################################### define functions ###########################################
############################################################################################################
#-------------------------------------------------------------------------------------------------------------------------------------------------------
usage() {
clear
printf "\n"
printf " Usage: $(basename $0) <option>\n"
printf "\n"
printf " -h help dialog \n"
printf " -i show script information\n"
printf " -m monocrome output\n"
printf "\n"
printf " default show IP / LAN information ( used devices )\n"
printf " -v show IP / LAN information ( all devices )\n"
printf " -sl scan LAN\n"
printf " -bm bandwidth measurement\n"
printf " -fbr renew fritzbox WAN IP\n"
printf " -cfrp check for required packets\n"
# output error message
printf "\n"
printf "\e[0;31m\n $1\e[0m\n"$end
printf "\n"
exit
}
#-------------------------------------------------------------------------------------------------------------------------------------------------------
check_input_options () {
# create available options list
InputOptionList=$(cat $ScriptFile | sed -n '/usage()/,/exit/p' | grep " -[[:alpha:]]" | awk '{print $3}' | grep "^\-")
# check for valid input options
for Option in $@ ; do
if [[ -z $(grep -w -- "$Option" <<< "$InputOptionList") ]]; then
InvalidOptionList=$(echo $InvalidOptionList $Option)
fi
done
# print invalid options and exit script_information
if [[ -n $InvalidOptionList ]]; then
usage "invalid option: $InvalidOptionList"
fi
}
#-------------------------------------------------------------------------------------------------------------------------------------------------------
load_colorcodes () {
red=$'\e[0;31m'
grn=$'\e[0;32m'
yel=$'\e[0;33m'
blu=$'\e[0;34m'
mag=$'\e[0;35m'
cyn=$'\e[0;36m'
end=$'\e[0m'
}
#-------------------------------------------------------------------------------------------------------------------------------------------------------
script_information () {
printf "\n"
printf " Scriptname: $ScriptName\n"
printf " Version: $Version \n"
printf " Location: $(pwd)/$ScriptName\n"
printf " Filesize: $(ls -lh $0 | cut -d " " -f5)\n"
printf "\n"
exit 0
}
#-------------------------------------------------------------------------------------------------------------------------------------------------------
progressbar () { # usage : progressbar "MESSAGE" 3 "."
tput civis
echo -ne "$1 "
for i in `seq 1 $2`; do
echo -en "\033[K$3"
sleep 1
done
echo -en "\015"
tput cvvis
}
#-------------------------------------------------------------------------------------------------------------------------------------------------------
print_parser () {
if [[ $(grep SeperatorLine <<< $@) ]]; then
printf "+---------------------------------------------------------------------------------------------------+\n"
elif [[ $PrintParser == "info_line" ]]; then
printf "| %-97s | \n" "$1"
elif [[ $PrintParser == "network_information" ]]; then
printf "| %-11s %-4s %-38s %-41s | \n" "$1" "$2" "$3" "$4"
elif [[ $PrintParser == "bandwidth_measurement" ]]; then
printf "| %-11s %-4s %-38s %-41s | \n" "$1" "$2" "$3" "$4"
fi
}
#-------------------------------------------------------------------------------------------------------------------------------------------------------
check_for_required_packages () {
InstalledPacketList=$(dpkg -l | grep ii)
for Packet in $RequiredPackets ; do
if [[ -z $(grep -w $Packet <<< $InstalledPacketList) ]]; then
MissingPackets=$(echo $MissingPackets $Packet)
fi
done
# print status message / install dialog
if [[ -n $MissingPackets ]]; then
printf "missing packets: \e[0;31m $MissingPackets\e[0m\n"$(tput sgr0)
read -e -p "install required packets ? (Y/N) " -i "Y" InstallMissingPackets
if [[ $InstallMissingPackets == [Yy] ]]; then
# install software packets
sudo apt update
sudo apt install -y $MissingPackets
if [[ ! $? == 0 ]]; then
exit
fi
else
printf "programm error: \e[0;31m missing packets : $MissingPackets\e[0m\n\n"$(tput sgr0)
exit 1
fi
else
printf " all required packets detected\n"
fi
}
#-------------------------------------------------------------------------------------------------------------------------------------------------------
scan_lan () {
printf " \n\n"
printf " Your Linux Distibution is $Codename $Architecture bit\n"
printf " Your LAN ip is $LANIP\n"
printf " \n\n"
for IP in $LANIP ;do
nmap $IP/24
done
}
#-------------------------------------------------------------------------------------------------------------------------------------------------------
check_LAN_connection () {
# check LAN connection
if [[ -z $(hostname -I) ]] ; then
usage " No LAN / WLAN connection"
fi
}
#-------------------------------------------------------------------------------------------------------------------------------------------------------
check_WAN_connection () {
# check WAN connection
nc -zw1 8.8.8.8 443
if [[ ! $? == 0 ]] ; then
WANConnection="offline"
fi
}
#-------------------------------------------------------------------------------------------------------------------------------------------------------
check_ip_devices () {
for IPDevice in $(ip a | grep ^[[:digit:]] | cut -d ":" -f2 | grep -v lo | tr -d " "); do
# get device specs
IPDeviceSpecs=$(ip a list $IPDevice)
DeviceName=$(echo "$IPDeviceSpecs" | head -n1 | awk -F ": " '{printf $2}')
DeviceIPv4=$(echo "$IPDeviceSpecs" | awk -F "inet " '{printf $2}' | cut -d "/" -f1)
DeviceIPv6=$(echo "$IPDeviceSpecs" | awk -F "inet6 " '{printf $2}' | cut -d "/" -f1)
DeviceMac=$(echo "$IPDeviceSpecs" | awk -F "link/ether " '{printf $2}' | cut -d " " -f1)
# parse values classes
DeviceIPv4=${DeviceIPv4:-${red}none ${end} }
DeviceIPv6=${DeviceIPv6:-${red}none ${end} }
DeviceClass=$DeviceName
if [[ -n $(grep ^en <<< $DeviceName) ]]; then DeviceClass=LAN ; fi
if [[ -n $(grep ^wl <<< $DeviceName) ]]; then DeviceClass=WLAN ; fi
if [[ -n $(grep ^anbox <<< $DeviceName) ]]; then DeviceClass=AnBox ; fi
PrintParser=network_information
print_parser "$DeviceClass IPv4" "=>" "$DeviceIPv4" "($DeviceMac) (dev: $DeviceName)"
print_parser "$DeviceClass IPv6" "=>" "$DeviceIPv6" "($DeviceMac) (dev: $DeviceName)"
done
}
#-------------------------------------------------------------------------------------------------------------------------------------------------------
network_information () {
# check WAN connection and set WAN value if offline
if [[ $WANConnection == "offline" ]]; then
WANIP="${red}none${end} "
GeoLocation="WAN offline"
TorCheck="${red}none${end} "
ServiceURL="TOR offline"
else
# get WAN IP and geo location
IPDiscoverServices=" https://check.torproject.org;Your IP address appears to be:
https://showmyip.gr;Your IP is:
https://meineipadresse.de;Meine IP-Adresse
http://checkip.dyndns.org;Current IP Address:"
SAVEIFS=$IFS
IFS=$(echo -en "\n\b")
for IPDiscoverService in $IPDiscoverServices ; do
ServiceURL=$( cut -d ";" -f1 <<< $IPDiscoverService | sed 's/^[ \t]*//')
GrepExpression=$( cut -d ";" -f2 <<< $IPDiscoverService)
# check for valid WAN IP / break URL check loop if WANIP contains valid IP
WANIP=$(wget -q -O - $(eval echo $ServiceURL) | grep "$GrepExpression" | grep -Eo '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}')
if [[ -n $WANIP ]]; then
break
else
ErrorLog=$(echo "$ErrorLog" "Website unreachable: $ServiceURL\n")
fi
done
IFS=$SAVEIFS
if [[ -n $ErrorLog ]]; then usage " $(echo $ErrorLog)" ; fi
GeoLocation=$(geoiplookup $WANIP | awk -F ": " '{print $2}')
GeoLocation=${GeoLocation/IP Address not found/XX}
#TorCheck=$(wget -q -O - https://check.torproject.org | grep "Tor" | sed -n 1p | cut -d " " -f7 | cut -d "." -f1)
TorCheck=$(lynx --dump https://check.torproject.org | sed -n 6p | cut -d . -f1)
TorCheck=${TorCheck//Sorry/${red}"inactive"${end} }
TorCheck=${TorCheck//Congratulations/${grn}"active"${end} }
fi
# get gateway http interface inforamtions // timeout required for mobile wlan / tethering
GatewayHTTPString=$(timeout 2 wget -q -O - $GatewayIP)
# parse Fritzbox gateway information
if [[ -n $(echo $GatewayHTTPString | grep 'FRITZ!Box') ]] ;then
GatewayDevice=$(echo $GatewayHTTPString | tr "," "\n" | tr -d '"' | awk -F "pageTitleProduct:" '{printf $2}')
fi
# substitute empty vars
GatewayDevice=${GatewayDevice:-unknown}
GatewayIP=${GatewayIP:-${red}none${end} }
GatewayMAC=${GatewayMAC:-no connection }
if [[ -z $DNSServerlistNmCLI ]] && [[ $GatewayIP == $DNSServerList ]]; then
DNSServerlistNmCLI="$GatewayMAC) (dev: $GatewayDevice"
fi
# print network specs
PrintParser="network_information"
print_parser SeperatorLine
print_parser "WAN IP" "=>" "$WANIP" "($GeoLocation)"
print_parser "Gateway IP" "=>" "$GatewayIP" "($GatewayMAC) (dev: $GatewayDevice)"
print_parser "DNS Server" "=>" "$DNSServerList" "($DNSServerlistNmCLI)"
check_ip_devices
print_parser "TOR status" "=>" "$TorCheck " "($ServiceURL)"
print_parser SeperatorLine
}
#-------------------------------------------------------------------------------------------------------------------------------------------------------
bandwidth_measurement () {
# skip for offline WAN connections
if [[ $WANConnection == "offline" ]]; then
usage " No WAN connection. Bandwidth measurement skipped"
fi
if [[ $(stat -c %Y $SpeedtestTempFile 2> /dev/null) -ge $(( `date +%s` - $MaxScriptExecutionIntervalBandwidth )) ]]; then
WaitingTime=$(( $(stat -c %Y $SpeedtestTempFile 2> /dev/null) - $(( `date +%s` - $MaxScriptExecutionIntervalBandwidth )) ))
print_parser SeperatorLine
PrintParser="info_line"
print_parser " Bandwidth measurement in progress, waiting "$WaitingTime"s"
print_parser SeperatorLine
exit
fi
print_parser SeperatorLine
# start measurement
$(speedtest --secure > $SpeedtestTempFile) &
# wait for finish measurement
sleep 0.2
while [[ $(pgrep speedtest) ]]; do
progressbar "| Bandwidth measurement in progress " 3 .
done
# parse speedtest output to output vars
SpeedtestOutput=$(cat $SpeedtestTempFile)
ClientISPName=$(echo "$SpeedtestOutput" | awk -F "Testing from " '{printf $2}' | sed 's/ (.*//g' | cut -c1-35)
ClientISPIP=$(echo "$SpeedtestOutput" | grep -Eo '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}')
TargetHost=$(echo "$SpeedtestOutput" | awk -F "Hosted by " '{printf $2}' | sed 's/\[.*$//' | cut -c1-35)
TargetHostDistance=$(echo "$SpeedtestOutput" | grep "Hosted by " | tr "[" "\n" | tr "]" "\n" | grep " km")
Ping=$(echo "$SpeedtestOutput" | grep "Hosted by " | awk -F "km]: " '{printf "%s ms" , substr($2, 1, 5) }')
DownloadSpeed=$(echo "$SpeedtestOutput" | grep "Download:" | cut -d " " -f2-4)
UploadSpeed=$(echo "$SpeedtestOutput" | grep "Upload:" | cut -d " " -f2-4)
GeoLocation=$(geoiplookup $ClientISPIP 2> /dev/null | awk -F ": " '{print $2}' | cut -d "," -f1)
GeoLocation=${GeoLocation/IP Address not found/XX}
PrintParser="bandwidth_measurement"
print_parser "Local host" "=>" "$ClientISPName" "($ClientISPIP|$GeoLocation)"
print_parser "Remote host" "=>" "$TargetHost" "($TargetHostDistance)"
# print parser spacing doesn`t work with colored tput prompt.
# print_parser "Bandwidth" "=>" "down: $(tput setaf 2)$DownloadSpeed$(tput sgr0) up: $(tput setaf 1)$UploadSpeed $(tput sgr0)" " ping: $Ping "
print_parser "Bandwidth" "=>" "down: $(tput setaf 2)$DownloadSpeed$(tput sgr0) up: $(tput setaf 1)$UploadSpeed $(tput sgr0)" "$(printf '\033[59`%s\n' "(ping: $Ping) ") " # '`'"
print_parser SeperatorLine
rm $SpeedtestTempFile
}
#-------------------------------------------------------------------------------------------------------------------------------------------------------
fritzbox_reconnect () {
timeout 5 curl "http://$FritzboxIP:49000/igdupnp/control/WANIPConn1" -H "Content-Type: text/xml; charset="utf-8"" -H "SoapAction:urn:schemas-upnp-org:service:WANIPConnection:1#ForceTermination" -d "<?xml version='1.0' encoding='utf-8'?> <s:Envelope s:encodingStyle='http://schemas.xmlsoap.org/soap/encoding/' xmlns:s='http://schemas.xmlsoap.org/soap/envelope/'> <s:Body> <u:ForceTermination xmlns:u='urn:schemas-upnp-org:service:WANIPConnection:1' /> </s:Body> </s:Envelope>"
}
#-------------------------------------------------------------------------------------------------------------------------------------------------------
fritzbox_WAN_IP () {
timeout 5 wget -qO- "http://$FritzboxIP:49000/igdupnp/control/WANIPConn1" --header "Content-Type: text/xml; charset="utf-8"" --header "SoapAction:urn:schemas-upnp-org:service:WANIPConnection:1#GetExternalIPAddress" --post-data="<?xml version='1.0' encoding='utf-8'?> <s:Envelope s:encodingStyle='http://schemas.xmlsoap.org/soap/encoding/' xmlns:s='http://schemas.xmlsoap.org/soap/envelope/'> <s:Body> <u:GetExternalIPAddress xmlns:u='urn:schemas-upnp-org:service:WANIPConnection:1' /> </s:Body> </s:Envelope>" | grep -Eo '\<[[:digit:]]{1,3}(\.[[:digit:]]{1,3}){3}\>'
}
#-------------------------------------------------------------------------------------------------------------------------------------------------------
fritzbox_renew_ip () {
printf " old FritzBox WAN IP: $(fritzbox_WAN_IP) \n"
printf " reconnecting ...\n"
fritzbox_reconnect 2>&1> /dev/null 2>&1> /dev/null
sleep $FritzboxPrintNewIPDelay
printf " new FritzBox WAN IP: $(fritzbox_WAN_IP)\n"
}
#-------------------------------------------------------------------------------------------------------------------------------------------------------
############################################################################################################
############################################# start script #############################################
############################################################################################################
#------------------------------------------------------------------------------------------------------------
# check for valid input options
check_input_options "$@"
#------------------------------------------------------------------------------------------------------------
# check for monocrome output
if [[ -z $(grep "\-m" <<< $@) ]]; then
load_colorcodes
fi
#------------------------------------------------------------------------------------------------------------
# check connections
check_LAN_connection
check_WAN_connection
#------------------------------------------------------------------------------------------------------------
case $1 in
-h ) usage " help dialog";;
-i ) script_information;;
-v ) network_information;;
-sl ) scan_lan;;
-bm ) bandwidth_measurement;;
-fbr) fritzbox_renew_ip ;;
-cfrp) check_for_required_packages;;
* ) network_information | grep -v none;;
esac
#------------------------------------------------------------------------------------------------------------
############################################################################################################
############################################## changelog ###############################################
############################################################################################################
#------------------------------------------------------------------------------------------------------------
# TODO add printf colorcodes instead of tput
# TODO check why space character error in print_parser occurs when using colorcodes
# TODO add new standalone option: list device status and MAC adresses even when LAN WAN is offline
# 3.0
# add DNS servers output
# add DNS server infos using nmcli ( gnome )
# 2.9
# add input option check function
# 2.8
# format updated
# add ipv6 support
# add output for all and used devices
# add monocrome output
# 2.7
# LAN WAN check added