-
Notifications
You must be signed in to change notification settings - Fork 0
/
mailip
30 lines (30 loc) · 839 Bytes
/
mailip
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
#!/bin/bash
#Script to report public IP address change
#Path to mail addr
TO=/home/pi/bin/raspberry_tools/mailipaddr
#The file that contains the current pubic IP
EXT_IP_FILE=/home/pi/bin/raspberry_tools/ip
#hostname
HOST_NAME=$(hostname)
#Check file for previous IP address
if [ -f $EXT_IP_FILE ]; then
KNOWN_IP=$(cat $EXT_IP_FILE)
else
KNOWN_IP=
fi
if [ "$1" = "-r" ]; then
MSG="$(date)\n$KNOWN_IP"
echo -e $MSG | mail -s "${HOST_NAME} reboot" $(cat $TO)
else
CURRENT_IP=$(curl ipinfo.io/ip)
MSG="$(date) | $(hostname) | $(cat /proc/device-tree/model)\n$CURRENT_IP"
#See if the IP has changed
if [ "$CURRENT_IP" != "$KNOWN_IP" ]; then
echo $CURRENT_IP > $EXT_IP_FILE
#If so send an alert
echo -e $MSG | mail -s "${HOST_NAME} ipchange" $(cat $TO)
logger -t ipcheck -- IP changed to $CURRENT_IP
else
logger -t ipcheck -- NO IP change
fi
fi