Script to email current IP address

Associate
Joined
3 Jul 2011
Posts
548
Hi, I'm wanting to write a script to email me the IP address of a remote machine periodically or if the IP changes.

So far I have this
Code:
ifconfig | grep 'inet addr:' | grep -v '127.0.0.1' | cut -d: -f2 | awk '{ print $1}'
this outputs the IP address that I want to email to myself.

The questions I have are how can I set this script on startup and to run every hour?

How can I pipe the output of this script into an email using the command prompt?

Thanks
 
Isn't that just going to return your NAT address, or is that machine directly connected to the net?

Either way, you'll need to configure your machine to relay mail. Then just use mailx or ssmtp to do what you need in said script.

Crontab is where you want to run that script from, save it in your home directory and make sure it's executable with chmod 700.

Also DDNS would be my preferred way around this problem. :)
 
ifconfig.me is a quick and easy site to use for this, as it will always return the external IP
This Script with executable set,
#!/bin/bash
curl ifconfig.me | tee /tmp/currentip
sendmail -v user@domainname < /tmp/currentip

Crontab Entry

0 * * * * /path/to/wherever/script/is

that would be my solution
 
Here's a script I have running on my Pi. It retrieves my global IP and if it has changed compared with the previous run of the script, it uses send mail to notify me. Hope this helps.

pi@raspberrypi:~$ more IP_Checker.sh
#!/bin/sh



while [ 1 ]
do
MYIP=`awk '{print $1}' /home/pi/MYIP.log`
CURRENTIP=`wget -O - -q icanhazip.com`
if [ "$CURRENTIP" != "$MYIP" ]; then
echo "New IP is ${CURRENTIP}"
`echo $CURRENTIP > /home/pi/MYIP.log`
`sendmail <email address> < /home/pi/MYIP.log`
else
sleep 1800
fi
done
pi@raspberrypi:~$


****************


Replace <email address> with your email address, then make the file executable and add it to init.d
 
Back
Top Bottom