Dynip
Automatically
Email WAN IP Address at Login - Linux
While
I can only take a partial credit
for this bash script which emails the WAN IP address at login, I
expanded it and modified it for use with Gmail. The basic idea is
that your Linux machine retrieves the internet IP address from
whatsmyip.org and sends it out to
an email recipient.
Why
is this useful in the first place?
My mom lives abroad. She uses Ubuntu and wants me to get inside of
her computer to do occasional software updates or fixes. Because her
provider assigns dynamic IP addresses to the customers, this script
informs me of the IP address changes so I can log into her computer
via SSH.
Other
uses may include detection of
unauthorized use, GPS-free geo tracking, etc.
The
script gets the IP address from a
third party service, compares it to the previous known address stored
in a file called ipaddress, and if different, it emails it to the
recipient.
#!/bin/bash
# Emails current IP address
to a
recipient. Requires package sendEmail with SSL and TLS.
#Last
known IP address
IPFILE=/tmp/ipaddress
#Allow
60 seconds for the Internet
connection to become active
sleep
60
#Retrieve
and assign WAN IP address to
CURRENT_IP
CURRENT_IP=$(wget
-q -O -
http://automation.whatismyip.com/n09230945.asp)
DATUM="$(date)"
#Customize
the “from” address
MAIL_FROM="From:
YOURNAME@gmail.com"
#Verify
that ipaddress file exists,
compare to the current IP, update ipaddress file
if
[ -f $IPFILE ]; then
KNOWN_IP=$(cat
$IPFILE)
else
KNOWN_IP=
fi
if
[ "$CURRENT_IP" !=
"$KNOWN_IP" ]; then
echo
$CURRENT_IP > $IPFILE
#Customize
email addresses and Gmail
password
sendEmail
-f "$MAIL_FROM"
-t RECIPIENT@YOURDOMAIN.COM -u "$CURRENT_IP" -m
"$CURRENT_IP" "Local time is" "$DATUM"
-s smtp.gmail.com:587 -xu YOURNAME@gmail.com -xp GMAIL-PASSWORD -o
tls=yes > /dev/null
logger
-t ipcheck -- IP changed
to $CURRENT_IP
else
logger
-t ipcheck -- No IP
change
fi
Next Page: 1,
2
1,
2
|