summaryrefslogtreecommitdiff
path: root/monitor.sh
blob: 1bf51297dabd1f39faf5ee71dc445c0c4d0425e3 (plain)
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
#! /bin/bash -

#  SJP 16 August 2014
#  Monitor my external IP address


cd ~/mail/monitor

# set up logfile
if (test -e data/tmp.*) then
	datafile=$(ls data/tmp.*)
	newfile=0
else
	datafile=`mktemp --tmpdir=data`
	newfile=1
fi

# echo "datafile = $datafile"

ip=$(curl ifconfig.me/ip)
[ ${#ip} = 0 ] && ip="Timed out"
# Could also do [ -z $ip ]
dt=$(\date  +%Y-%m-%dT%T%:z)

# either write date + IP to new file or compare IPs
# write date and IP address to file but delete if it's a dud
if (test "$newfile" -eq 1) then
	echo -e "$dt\t$ip" > $datafile
	[ "$ip" == "Timed out" ] && rm $datafile

else	# datafile already exists, so compare
	read line < $datafile
	oldip=$(echo $line | awk '{print $2 }')
	if (test "$ip" != "$oldip") then
		. notifynewip.sh
		rm $datafile
	fi
fi