Privately report the IP address of a remote server


A dyndns alternative for private use when your remote machines are on an ADSL line without a static IP



This script sends gpg encrypted mail via TOR to an email account like your.account@gmail.com, reporting the dynamic IP address assigned to the remote connection by the ISP only if it has changed.

It is useful when you have a server in a remote location with an ADSL line. Most ADSL lines have IP addresses assigned dynamically, so they are reacheable from the ouside but they change sometimes, and each time the home router reboots.

Here is the code with comments:


#!/bin/sh

### Function of this script:
# If no ip number has been checked, it makes a request, crypts it with gpg key and sends
# as an attachment to mail (sent trough tor).
# If ip number has been already checked, makes a new request, confronts it and sends the 
# result only if the ip number has changed

#####

### FINETUNINGS

# You'll need the 'sendemail' package installed (apt-get install sendemail)
# If you don't want to use TOR, simply remove the "torify" command before the "sendemail" 
# command (BUT THEN YOU CAN READ THE HEADERS OF RECEIVED MAIL TO SEE IP ADDRESS OF SENDER)
# If your smtp server does not use TLS, remove the "-o tls=yes" option (BUT YOUR PASSWORD
#  WILL BE SENT IN CLEARTEXT!)
# Change the "-u" option to whatever you want the title of your mail to be.

### CRONTAB

# You should add this file in your "/etc/crontab", to enable it to check at given periods
# the changement in your server's IP address
# sample crontab entry (checks every hour):
#
#                       30 *    * * *   user    /usr/local/bin/ipreport.sh


#####

### Alternate site to check ip
# wget www.whatismyip.com/automation/n09230945.asp -O - -q

######

### VARIABLES

# gpg encrypts for this user id name
GPG_ID=`your_gpg_id`

# sender email address
FROM_ADDRESS=`sender.account@example.com`

# sender id on smtp server (only login name, omit @gmail.com or @example.com)
SENDER_ID=`your.account`

# sender password on smtp server (in clear text without tls! Do not use whitespaces)
SENDER_PASSWD=`your_secret_password`

# receiver email address
TO_ADDRESS=`receiver.account@example.com`

# smtp address of sender mail account
SMTP_ADDRESS=`smtp.example.com`

#####

if [ -f  "/tmp/attachment.txt" ] ; then
        wget -q -O - checkip.dyndns.org | sed -e 's/[^[:digit:]|.]//g' > /tmp/attachment2.txt ;

        if diff "/tmp/attachment.txt" "/tmp/attachment2.txt" >/dev/null ; then
                exit ;
        else
                gpg --armor --encrypt -r $GPG_ID /tmp/attachment2.txt ;
                torify sendemail -f $FROM_ADDRESS -t $TO_ADDRESS -u "Report" \
                -o message-file=/tmp/attachment2.txt.asc -s $SMTP_ADDRESS \
		-o tls=yes -xu $SENDER_ID -xp $SENDER_PASSWD
        fi
else
        wget -q -O - checkip.dyndns.org | sed -e 's/[^[:digit:]|.]//g' > /tmp/attachment.txt ;
        gpg --armor --encrypt -r $GPG_ID /tmp/attachment.txt ;
        torify sendemail -f $FROM_ADDRESS -t $TO_ADDRESS -u "Report" \
        -o message-file=/tmp/attachment.txt.asc -s $SMTP_ADDRESS \
	-o tls=yes -xu $SENDER_ID -xp $SENDER_PASSWD
fi

You can download the ipreport.sh script, make it executable ( chmod 755 ./ipreport.sh ), copy it to the /usr/local/bin/ directory.

Now you can add it in your crontab so that it runs every hour. Simply add a similar line to /etc/crontab:

00 *   * * * user     /usr/local/bin/ipreport.sh


-----------------------------
last update: inputs_marmalade 15/9/2009

###
contributions / feedback / questions


gpg public key
http://ram.squat.net/tech/inputs.marmalade.asc
###