-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmail-ip.sh
executable file
·67 lines (56 loc) · 2.33 KB
/
mail-ip.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
#!/bin/bash
################################################################################
# Script Name : mail-ip.sh
# Author : Podesta
# Date : October 2019
# Version : 1.0
# Dependencies : Miniupnpc for creating upnpc rules.
# Msmtp for sending email.
# Description : Opens port on router using upnp, and mails the info. Works
# great with a cron job. Make sure you have msmtp configured.
################################################################################
# User variables
PORT=
TOMAIL=
FROMMAIL=
TIMER=21600
DESCRIPTION=""
# Makes sure system has fully started for boot cron job.
sleep 30
date=$( date -u +%F )
newEPOCH=$( date +%s )
oldEPOCH=$( grep "EPOCH" ~/Documents/ip-upnp/mail.txt | sed 's/[^0-9]//g' )
# Save upnpc to file, so it is run only once
upnpc -l > ~/Documents/ip-upnp/upnpc.txt
newExtIP=$( grep "ExternalIPAddress" ~/Documents/ip-upnp/upnpc.txt | sed 's/[^0-9\.]//g' )
newLanIP=$( grep "Local LAN ip address" ~/Documents/ip-upnp/upnpc.txt | sed 's/[^0-9\.]//g' )
oldExtIP=$( grep "External IP" ~/Documents/ip-upnp/mail.txt | sed 's/[^0-9\.]//g' )
oldLanIP=$( grep "LAN IP" ~/Documents/ip-upnp/mail.txt | sed 's/[^0-9\.]//g' )
# See if there is a rule on upnp with the desired port
upnpIP=$( grep ~/Documents/ip-upnp/upnpc.txt -E -e \
'^[[:space:]]*[[:digit:]]+[[:space:]]*TCP[[:space:]]'+"$PORT" \
| sed -E 's/.*->(.+):.*/\1/g')
# If there is no rule on upnp or the IP is different, delete rule and add new one
if [[ ! "$newLanIP" == "$upnpIP" ]]
then
upnpc -d $PORT
upnpc -e "$DESCRIPTION" -r $PORT TCP
echo "Creating upnp rule"
fi
# If same IP and less than 6 (21600) hours passed, do not send another email
if [[ "$newEPOCH" -le $(("$oldEPOCH" + "$TIMER")) ]] && \
[[ "$newExtIP" == "$oldExtIP" ]] && \
[[ "$newLanIP" == "$oldLanIP" ]]
then
echo "No need to send email"
exit 1
# Otherwise update email and send it
else
printf "To: '$TOMAIL'\nFrom: '$FROMMAIL'\nSubject: RPI - New IP\n\n" > ~/Documents/ip-upnp/mail.txt
printf "Date: $date\nEPOCH: $newEPOCH\n" >> ~/Documents/ip-upnp/mail.txt
printf "LAN IP: $newLanIP\nExternal IP: $newExtIP\n\n" >> ~/Documents/ip-upnp/mail.txt
upnpc -l >> ~/Documents/ip-upnp/mail.txt
cat ~/Documents/ip-upnp/mail.txt | msmtp -a default "$TOMAIL"
echo "Email sent"
exit 2
fi