-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathipspec_v2.0.sh
169 lines (142 loc) · 7.28 KB
/
ipspec_v2.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
#!/bin/bash
#
# name : ipspec.sh
# desciption : show IP and LAN Information
# autor : Speefak ( [email protected] )
# licence : (CC) BY-NC-SA
# version : 2.0
#
#------------------------------------------------------------------------------------------------------------
############################################################################################################
####################################### define global variables ########################################
############################################################################################################
#------------------------------------------------------------------------------------------------------------
RequiredPackets="lynx curl nmap"
CODENAME=$(lsb_release -d | tr -d : | cut -f 2)
ARCHITECTURE=$(getconf LONG_BIT)
LANIP=$(hostname -I | tr " " "\n" | uniq)
GatewayIP=$(ip route | sed -n 1p | cut -d " " -f3)
FritzboxIP=$GatewayIP
FritzboxPrintNewIPDelay=3
#------------------------------------------------------------------------------------------------------------
############################################################################################################
########################################### define functions ###########################################
############################################################################################################
#------------------------------------------------------------------------------------------------------------
usage() {
clear
printf "\n"
printf " Usage: $(basename $0) <option>\n"
printf "\n"
printf " default show IP / LAN information\n"
printf " -sl scan LAN\n"
printf " -fbr renew fritzbox WAN IP\n"
printf " -cfrp check for required packets"
# output error message
printf "\e[0;31m\n $1\e[0m\n"$(tput sgr0)
printf "\n"
exit
}
#------------------------------------------------------------------------------------------------------------
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 $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"
printf " Press any key to start scan\n"
read
for IP in $LANIP ;do
nmap $IP/24
done
}
#------------------------------------------------------------------------------------------------------------
network_information () {
# get WAN IP and geo location
IPDiscoverServices=" https://check.torproject.org;Your IP address appears to be:
https://showip.net;.
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)
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 ; fi
done
IFS=$SAVEIFS
#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/$(tput setaf 1)"inactive" $(tput sgr0) }
TorCheck=${TorCheck//Congratulations/$(tput setaf 2)"active"$(tput sgr0) }
# get gateway http interface inforamtions
GatewayHTTPString=$(wget -q -O - $GatewayIP)
# Fritzbox gateway
if [[ -n $(echo $GatewayHTTPString | grep 'FRITZ!Box') ]] ;then
GatewayDevice=$(echo $GatewayHTTPString | grep 'FRITZ!Box' | awk -F "bluBarTitle" '{print $2}' | cut -d '"' -f3)
fi
# no gateway information available
GatewayDevice=${GatewayDevice:-unknown}
printf "WAN IP => $WANIP\n"
printf "LAN IP => $LANIP\n"
printf "Gateway IP => $GatewayIP\n"
printf "GW Device => $GatewayDevice\n"
printf "TOR Net => $TorCheck\n"
printf "Service => $ServiceURL\n"
}
#-------------------------------------------------------------------------------------------------------------------------------------------------------
fritzbox_reconnect () {
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 () {
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 #############################################
############################################################################################################
#------------------------------------------------------------------------------------------------------------
case $1 in
-sl ) scan_lan;;
-h ) usage "help dialog";;
-fbr) fritzbox_renew_ip ;;
-cfrp) check_for_required_packages;;
* ) network_information;;
esac