-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmypubip
executable file
·92 lines (68 loc) · 1.71 KB
/
mypubip
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
#!/bin/bash
# 2015.08.29
# Sends public ip if changed.
#How often should we check for ip (in sec)?
often=300
urls=(ipinfo.io/ip ifconfig.me ipecho.net/plain)
nooftimes=10
emailaddr="[email protected]"
function getthatip {
# Go through the servers
out=""
for currurl in ${urls[*]}
do
# Sometimes we get no response
wcn=1
while [[ -z "$trueip" && "$wcn" -le "$nooftimes" ]]; do
#trueip=`curl -m 20 $currurl`
trueip=`curl --connect-timeout 20 $currurl`
wcn=$((wcn+1))
sleep 1
done
if [[ $wcn -gt $nooftimes ]]; then
trueip="Got no response from $currurl"
fi
# What do we got?
if [[ $trueip =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]
then
out="$trueip"
nonoftimes=0
break
else
out="$out; $trueip"
fi
trueip=""
done
}
function emailip {
echo "The ip for $HOSTNAME at $nowis changed from $filedip to $out" | mail -s "$HOSTNAME pubip" $emailaddr
}
nowis=`date +'%y.%m.%d %a, %H:%M'`
# Do we have the dir?
if [ ! -d ~/.mypubip ]; then
mkdir ~/.mypubip
fi
# Do we have the file?
if [ -e ~/.mypubip/mypubip.txt ]; then
# The file was refreshed at:
refreshedat=`ls -l --time-style\=+%s ~/.mypubip/mypubip.txt | awk '{ print$6 }' `
# Time now is, then subtract $often:
shouldhaverefreshed=`date +%s`
let shouldhaverefreshed=$shouldhaverefreshed-$often
# If file is too old, refresh:
if [ $refreshedat -lt $shouldhaverefreshed ]; then
getthatip
filedip=`cat ~/.mypubip/mypubip.txt`
echo "$out" > ~/.mypubip/mypubip.txt
if [ "$out" != "$filedip" ]; then
echo "The ip at $nowis changed from $filedip to $out"
emailip
fi
fi
else
# If file doesn't exist, refresh:
getthatip
echo "$out" > ~/.mypubip/mypubip.txt
echo "The ip at $nowis is $out"
emailip
fi