1. Store
  2. Apps
  3. Hardware
  4. Support
  5. Solutions

ClearFoundation

Forums
Welcome, Guest
How get notification of adsl IP changed
(1 viewing) 1 Guest
Go to bottomPage: 1
TOPIC: How get notification of adsl IP changed
#33460
How get notification of adsl IP changed 1 Year, 7 Months ago  
HI! every one!

I want to get a information as email when the ADSL IP was been changed, what can i do? thank !

BTW:system is ClearOS 5.2,mail server
jplele
Fresh Boarder
Posts: 5
graphgraph
User Offline Click here to see the profile of this user
The administrator has disabled public write access.
 
#33464
Re:How get notification of adsl IP changed 1 Year, 7 Months ago  
Here is a quick script I have written for you that you can also change to suit your environment. All it does is checks the current IP on server assuming that your interface name is ppp0. It then compares this IP to an IP stored in file /etc/ip_address/old_ip. If the two values are the same, it exists otherwise it will email you the new IP and store that new IP in this file. You will hence probably have to run this via a cron job at specific intervals.

Code:

#!/bin/bash
# Author: Thomas Tapfumanei

# Define variables
today=`date '+%F %H:%M'`
MESSAGE="/etc/ip_address/emailmessage.txt"
email_msg="The IP address has changed the new one is: "
FILE="/etc/ip_address/old_ip"
new_ip=`/sbin/ifconfig ppp0 | grep 'inet addr:'| cut -d: -f2 | awk '{ print $1}'`
old_ip=`awk '{print $1}' $FILE`


# Get ip address on up interface and store it in the new ip file
if [ "$new_ip" !=  "$old_ip" ]; then
        # report new ip
        echo "$emailmsg $new_ip as at  $today" > $MESSAGE
        /bin/cat $MESSAGE |/bin/mail -s " **  IP Adress has changed!! ** " user@email.com
        /bin/echo $new_ip > $FILE
else
exit
fi

Thomas Tapfumanei
Expert Boarder
Posts: 106
graphgraph
User Offline Click here to see the profile of this user
The administrator has disabled public write access.
 
#33467
Re:How get notification of adsl IP changed 1 Year, 7 Months ago  
Thanks very much to Thomas!
jplele
Fresh Boarder
Posts: 5
graphgraph
User Offline Click here to see the profile of this user
The administrator has disabled public write access.
 
#33470
Re:How get notification of adsl IP changed 1 Year, 7 Months ago  
With a minor mod I think you can avoid a cron job
Code:

#!/bin/bash
# Author: Thomas Tapfumanei

# Define variables
today=`date '+%F %H:%M'`
MESSAGE="/etc/ip_address/emailmessage.txt"
email_msg="The IP address has changed the new one is: "
FILE="/etc/ip_address/old_ip"
old_ip=`awk '{print $1}' $FILE`


# Get ip address on up interface and store it in the new ip file
while true; do
new_ip=`/sbin/ifconfig ppp0 | grep 'inet addr:'| cut -d: -f2 | awk '{ print $1}'`
if [ "$new_ip" !=  "$old_ip" ]; then
       # report new ip
       echo "$emailmsg $new_ip as at  $today" > $MESSAGE
       /bin/cat $MESSAGE |/bin/mail -s " **  IP Adress has changed!! ** " user@email.com
       /bin/echo $new_ip > $FILE
old_ip=$new_ip
fi
sleep 120
done



This will repeat 120 seconds after the previous loop finishes. You can change the delay to suit. I did something very similar in a monitoring script for my IPSec VPN. If you wanted you could pick up the current WAN IP from /var/lib/dynamicdns (eg new_ip=$( cat /var/lib/dynamicdns ). It has the advantage of making the script interface independent, but you are then reliant on the syswatch daemon sorting out the WAN IP and you do not control how often it updates. I did not bother with mine and used a similar line to Thomas.

With what I have done, if you need to end the loop you have to do something like "ps aux |grep your_script_name" and pick up the process id (pid) of your script's process, then do s "kill your_script's_process_id"
Nick Howitt
Platinum Boarder
Posts: 4129
graphgraph
User Offline Click here to see the profile of this user
The administrator has disabled public write access.
 
#33471
Re:How get notification of adsl IP changed 1 Year, 7 Months ago  
Thanks so much Nick . I wasn't even aware of the existence of the dynamicdns file. This will definitely help me in some future scripting.
Thomas Tapfumanei
Expert Boarder
Posts: 106
graphgraph
User Offline Click here to see the profile of this user
The administrator has disabled public write access.
 
#33493
Re:How get notification of adsl IP changed 1 Year, 7 Months ago  
Does dhclient-exit-hooks get called on an adsl system?
Gerald
Senior Boarder
Posts: 51
graphgraph
User Offline Click here to see the profile of this user
The administrator has disabled public write access.
 
#33509
Re:How get notification of adsl IP changed 1 Year, 7 Months ago  
Thomas Tapfumanei wrote:
Thanks so much Nick . I wasn't even aware of the existence of the dynamicdns file. This will definitely help me in some future scripting.I first started using that file (one of the devs pointed me to it) but then I thought it maybe was better not to read the same file every two minutes. I did not know if caching meant it would get read from memory or if I was doing a lot more disk reads than necessary so in the end I did something very similar to your ifconfig line. It also meant I did not have to wait for syswatch to do an update to the file.

@Gerald, sorry, I've got no idea.
Nick Howitt
Platinum Boarder
Posts: 4129
graphgraph
User Offline Click here to see the profile of this user
The administrator has disabled public write access.
 
#33510
Re:How get notification of adsl IP changed 1 Year, 7 Months ago  
Look at the man page for dhclient-script which shows that /etc/dhclient-exit-hooks script is called when IP address changes. (At least for an connection on eth0.)
Gerald
Senior Boarder
Posts: 51
graphgraph
User Offline Click here to see the profile of this user
Last Edit: 2011/10/16 15:47 By hermantoong.Reason: left out clarification
The administrator has disabled public write access.
 
#33512
Re:How get notification of adsl IP changed 1 Year, 7 Months ago  
Gerald wrote:
Look at the man page for dhclient-script which shows that /etc/dhclient-exit-hooks script is called when IP address changes. (At least for an connection on eth0.)Neat. That will simplify my IPSec watching script. Now to work out how to test it (I have a dynamic IP which has not changed for 2 years. Overriding the MAC address may do it).
Nick Howitt
Platinum Boarder
Posts: 4129
graphgraph
User Offline Click here to see the profile of this user
The administrator has disabled public write access.
 
Go to topPage: 1
  get the latest posts directly to your desktop