DrayTek UK Users' Community Forum

Help, Advice and Solutions from DrayTek Users

Script needed to email dynamic ip address

  • allat39
  • Topic Author
  • Offline
  • Junior Member
  • Junior Member
More
11 Jan 2015 09:12 #82292 by allat39
Hi,

We have a Vigor 2850Vn router which is assigned a dynamic public ip address by our ISP,

Can?? or has anyone got a script which will email the administrator the public ip address every time the router is rebooted?

Thanks

Please Log in or Create an account to join the conversation.

More
12 Jan 2015 22:18 #82302 by j1mbo
Try this:

Code:
#!/bin/bash # Checks the current public IP, and sends a mail if it has changed. # Environment Variables LOG=/var/log/check-ip.log RESTTIMER=3600 EMAILSERVER=mail.server.fqdn MAILFROM=yourserviceaccount@yourdomain MAILTO=youremailaddress@yourdomain COMMANDLINE="curl -s ipecho.net/plain" send_email () { # Log the change of status echo `date +'%Y%m%d %T'` $1 >> $LOG # Send a mail # Build the email EMAILSUBJECT="Public IP address change!" EMAILBODY="$1\n" # echo $EMAILBODY echo -e "$EMAILBODY" | mail -s "$EMAILSUBJECT" $MAILTO } # Script starts here... echo `date +'%Y%m%d %T'` check-ip.sh started >> $LOG # Check current public IP Old_IP=`$COMMANDLINE` while true; do # Now rest for RESTTIME seconds. We don't need to poll too often. sleep $RESTTIMER New_IP=`$COMMANDLINE` if ([ "$New_IP" != "$Old_IP" ]); then # IP address has changed! - send notification and update variable send_email "Public IP Address has changed. Was $Old_IP, now $New_IP." else # Just log that we checked echo `date +'%Y%m%d %T'` Public IP is $New_IP >> $LOG fi Old_IP=$New_IP done


Also mobile operators have opt-in (chargable) email to SMS services generally, like mmail.co.uk for O2. RESTTIMER is in seconds, so choose something sensible if you are going to use a chargeable SMS service in case something goes wrong!

You could also use SYSLOG and a log watcher to trigger the test only if the router produced some syslog message like a reboot or line drop etc.

Hope that helps.

Please Log in or Create an account to join the conversation.

  • allat39
  • Topic Author
  • Offline
  • Junior Member
  • Junior Member
More
14 Jan 2015 16:47 #82332 by allat39
Replied by allat39 on topic Re: Script needed to email dynamic ip address

J1mbo wrote: Try this:

Code:
#!/bin/bash # Checks the current public IP, and sends a mail if it has changed. # Environment Variables LOG=/var/log/check-ip.log RESTTIMER=3600 EMAILSERVER=mail.server.fqdn MAILFROM=yourserviceaccount@yourdomain MAILTO=youremailaddress@yourdomain COMMANDLINE="curl -s ipecho.net/plain" send_email () { # Log the change of status echo `date +'%Y%m%d %T'` $1 >> $LOG # Send a mail # Build the email EMAILSUBJECT="Public IP address change!" EMAILBODY="$1\n" # echo $EMAILBODY echo -e "$EMAILBODY" | mail -s "$EMAILSUBJECT" $MAILTO } # Script starts here... echo `date +'%Y%m%d %T'` check-ip.sh started >> $LOG # Check current public IP Old_IP=`$COMMANDLINE` while true; do # Now rest for RESTTIME seconds. We don't need to poll too often. sleep $RESTTIMER New_IP=`$COMMANDLINE` if ([ "$New_IP" != "$Old_IP" ]); then # IP address has changed! - send notification and update variable send_email "Public IP Address has changed. Was $Old_IP, now $New_IP." else # Just log that we checked echo `date +'%Y%m%d %T'` Public IP is $New_IP >> $LOG fi Old_IP=$New_IP done


Also mobile operators have opt-in (chargable) email to SMS services generally, like mmail.co.uk for O2. RESTTIMER is in seconds, so choose something sensible if you are going to use a chargeable SMS service in case something goes wrong!

You could also use SYSLOG and a log watcher to trigger the test only if the router produced some syslog message like a reboot or line drop etc.

Hope that helps.



J1mbo,
Many thanks.
I realise that we now dont have even the basic knowledge to create this.

We are not using Exchange, and in your example whilst the MAILTO is obvious, we are not sure what MAILFROM or EMAIL SERVER refers to..
If I am not pushing my luck, can you (or anyone) please 'unpack' this into a 1-2-3-4 step guide for entering the script into the Vigor 2850 router.

Thanks in advance guys

Please Log in or Create an account to join the conversation.

More
14 Jan 2015 22:07 #82343 by j1mbo
Hi again, the script is a bash script so needs to be run on a Linux machine - anything from a Raspberry Pi up will do, with sendmail configured. As far as I know, it's not possible to run scripts on the Draytek routers themselves, except if the firmware has been replaced with OpenWRT or similar.

I'd be very interested to help you with this though, unfortunately there is no PM system here... presumably you are a Windows environment? Do you have a server?

Please Log in or Create an account to join the conversation.

More
15 Jan 2015 18:32 #82379 by takeo_ischi
Replied by takeo_ischi on topic Re: Script needed to email dynamic ip address
You might also be interested in this:

http://en.wikipedia.org/wiki/Dynamic_DNS#DDNS

e.g. http://www.noip.com/

Please Log in or Create an account to join the conversation.

  • allat39
  • Topic Author
  • Offline
  • Junior Member
  • Junior Member
More
15 Jan 2015 22:38 #82388 by allat39
Replied by allat39 on topic Re: Script needed to email dynamic ip address

J1mbo wrote: Hi again, the script is a bash script so needs to be run on a Linux machine - anything from a Raspberry Pi up will do, with sendmail configured. As far as I know, it's not possible to run scripts on the Draytek routers themselves, except if the firmware has been replaced with OpenWRT or similar.

I'd be very interested to help you with this though, unfortunately there is no PM system here... presumably you are a Windows environment? Do you have a server?



Thanks J1mbo.. I have a couple of enquiries going on with this... I appreciate your offer of help. I will come back as soon as I have something useful, one way or another.

Thanks

Please Log in or Create an account to join the conversation.

Moderators: Sami