-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetmyip
More file actions
executable file
·75 lines (65 loc) · 1.73 KB
/
getmyip
File metadata and controls
executable file
·75 lines (65 loc) · 1.73 KB
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
#!/bin/bash
if [ "$(id -u)" != "0" ]
then
echo "Only root can run this."
exit 2
fi
MYIP=$(hostname -I)
# only run this on the 192.168.0 network
GO=FALSE
for I in $MYIP
do
TMP=$(echo $I | cut -f1-3 -d.)
if [ $TMP == 192.168.0 ]
then
GO=TRUE
break
fi
done
[ $GO == "FALSE" ] && exit 1
# lets check to see if we can ping extender
# (the other router at 192.168.0.7)
ping -c4 extender > /dev/null 2>&1
#ping -c4 blah > /dev/null 2>&1
# if we get a 0 return value, we can assume we are home.
if [ $? -ne 0 ]
then
echo not at home
exit 1
fi
# get our extern IP address
IP=$(curl icanhazip.com 2> /dev/null)
# we do this a couple of times so have put it in a function.
# update the /etc/hosts with our external IP address.
add_ip() {
echo "$1 wombat" >> /etc/hosts
}
#echo $IP
# test to see if there is already an entry for wombat in the hosts file.
if grep wombat /etc/hosts > /dev/null
then
# grab the ip from the hosts file and check to see if it matches the
# newly reported one.
OLD=$(grep wombat /etc/hosts | gawk '{print $1}')
echo "found current entry: $OLD"
if [ $OLD == $IP ]
then
# the newly report IP is the same as the one in the hosts file
# not need to do anything.
echo "Current entry is correct"
exit 0
else
# the newly report IP is not the same as the one we have.
# lets fix it.
echo "Correcting hosts file"
DATE=$(date '+%y-%m-%d_%H-%M')
echo "$DATE $OLD" >> ~michael/wombat.old
cp /etc/hosts /tmp/
grep -v wombat /tmp/hosts > /etc/hosts
add_ip $IP
fi
else
# No entry is found in the host file. We need to add it.
echo "Adding to hosts"
add_ip $IP
fi