LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-04-2011, 09:54 AM   #1
veeruk101
Member
 
Registered: Mar 2005
Distribution: Ubuntu 12.04 LTS
Posts: 249

Rep: Reputation: 16
Run script when IP address changes


Would it be possible to run a script every time my Linux laptop's IP address changes from my ISP? (I don't mean dynamic DNS, where I'd update a DNS entry somewhere - I'd rather just like to run an arbitrary script.)

What exactly happens when the IP address changes from my ISP? Will my connection (wired or wireless) momentarily disconnect and reconnect with the new IP address? In this case, I imagine it could be possible to run some sort of 'pre interface start up' script which could compare to the last IP address used.

Or would the change to the IP address happen seamlessly, meaning the wired/wireless connection doesn't momentarily disconnect and reconnect but rather just takes the new IP address instantaneously? In this case, I really don't know how to do it. I could run a cron task every couple of minutes to check for a change in IP address, then run the script, but ideally I'd like a way to do it without having a cron task run every couple of minutes.

If you have any ideas about how to do this, that would be great.
 
Old 08-04-2011, 10:26 AM   #2
bskrakes
Member
 
Registered: Sep 2006
Location: Canada, Alberta
Distribution: RHEL 4 and up, CentOS 5.x, Fedora Core 5 and up, Ubuntu 8 and up
Posts: 251

Rep: Reputation: 32
Hi veeruk101, are you connecting to your ISP through a router or are you using the ADSL/Cable modem provided with no router in between?

I doubt you would notice when/if the IP address changes, when the computer boots up it makes a request to any listening DHCP servers, if there is a DHCP is will offer an address to your device and the device will accept and use the address offered. By the time the process completes your system will be running and connected to the network. (in a perfect world, this not the exact DHCP process - its a high level explanation that I hope makes sense!)

Yes you could write a script to check your local machines IP... I am not sure where you are located but where I live it seems that my ISP changes the IP addresses once a year, meaning my public facing IP changes once a year. However I am behind a router which is using DHCP for my local computers, they change from time to time but rarely - most of my computers are set with static IP's.

In any case I would recommend you connect a router to your ISP modem, it adds a firewall to your network helping prevent unwanted access.

Last edited by bskrakes; 08-04-2011 at 10:48 AM.
 
Old 08-04-2011, 12:32 PM   #3
veeruk101
Member
 
Registered: Mar 2005
Distribution: Ubuntu 12.04 LTS
Posts: 249

Original Poster
Rep: Reputation: 16
I am connecting to my modem through a wireless router (and if using a wired connection, that would still go through a direct ethernet connection into the same router). In the DHCP process you describe, if an ISP decided it was time to change my IP address, would they generally 'wait' until the next time my computer makes a request to the DHCP server, or would they boot me off the network and have me try to reconnect so that I get the new IP address?

Is there some way to use a script that executes when the network interface starts up? (I'd rather use that than running the script at boot time, because the IP address could change once the computer is already running.)
 
Old 08-04-2011, 12:56 PM   #4
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
When you say "my IP address changes"... do you mean the IP of the laptop
changes, or the IP your modem/router presents to the big wide world changes?

If it's the first, it's your own router doing it; if it's the latter it's
your ISP. In either case they should never change while you're connected/
working because that would disrupt any connections you have currently going.



Cheers,
Tink
 
Old 08-04-2011, 02:43 PM   #5
sneakyimp
Senior Member
 
Registered: Dec 2004
Posts: 1,027

Rep: Reputation: 67
Quote:
Originally Posted by veeruk101 View Post
I am connecting to my modem through a wireless router (and if using a wired connection, that would still go through a direct ethernet connection into the same router). In the DHCP process you describe, if an ISP decided it was time to change my IP address, would they generally 'wait' until the next time my computer makes a request to the DHCP server, or would they boot me off the network and have me try to reconnect so that I get the new IP address?

Is there some way to use a script that executes when the network interface starts up? (I'd rather use that than running the script at boot time, because the IP address could change once the computer is already running.)
If your ISP changes your IP address, that will not affect the IP addresses of the devices on your LAN because they probably get their IP address using DHCP from your router. Depending on your router, you might be able to configure it to give a consistent IP address to a given machine. I configured my router to always give my Ubuntu server the same IP address.

When your ISP decides to change your IP address, it tells your router and your router alters its configuration. If your router runs cool linux-based firmware like DD-WRT, then you might be able to configure it with a cron job that checks its own ip or something. I'm not really sure how to do that.

If you have a linux machine running on your network and want a script to run automatically when *that* machine's IP address changes, I reckon that might be feasible. I don't know if these are of any use, but in the /etc/network directory on my ubuntu machine, there appear to be a variety of directories such as if-up.d, if-pre-up.d, etc. that contain scripts related to network events. One of these might be useful.

If you have a linux box on your network and want that machine to do something when your *ISP changes your address* then you could set up a cron job to check an externa server for your IP and remember it. Whenever the IP is different than last time, you could trigger a script. You can look up your external IP address (the one given to you by your ISP) easily. Just google "what is my ip" or go to http://nwtools.com

There are also plenty of forum posts on this matter. This one mentions a very specific hook related to dhclient which you may find useful.
 
Old 08-04-2011, 02:55 PM   #6
bskrakes
Member
 
Registered: Sep 2006
Location: Canada, Alberta
Distribution: RHEL 4 and up, CentOS 5.x, Fedora Core 5 and up, Ubuntu 8 and up
Posts: 251

Rep: Reputation: 32
This is how the internet and your network work (at a very high level):

www (world wide web/internet) --> isp --> you

You connect to the internet using a ADSL or cable modem provided by your ISP. At this point you have the internet but most people go to Best Buy or Future Shop and buy a router, normally wired/wireless routers. Your ISP will provide you with a public IP address which will be dynamic unless you request a static IP, if you request static you normally pay additional fees. You can obtain this IP address from websites such as:

What's my IP http://www.whatsmyip.org/
IP Chicken http://ww.ipchicken.com

This is the IP address which is used on the internet, it doesn't matter if you are directly connected to the modem or if you are connected to a router and then to your modem. When you add a router to your network it preforms NAT (network address translation). This results in a different IP address for your laptop, usually 192.168.0.xxx, 192.168.1.xxx, etc. The router translates your internal address to the public address and vise verse, make sense? Check here for more info: http://en.wikipedia.org/wiki/Network...ss_translation.

With all of that said I hope it makes sense, which IP address are you trying to isolate?
 
Old 08-04-2011, 03:05 PM   #7
bskrakes
Member
 
Registered: Sep 2006
Location: Canada, Alberta
Distribution: RHEL 4 and up, CentOS 5.x, Fedora Core 5 and up, Ubuntu 8 and up
Posts: 251

Rep: Reputation: 32
The following command will give you the IP address for the laptop/desktop you are working on: (if you are are on CentOS/RHEL)

Quote:
ifconfig eth0 | grep 'inet addr:' | cut -d: -f2 | awk '{ print $1}'
There are better ways to do this I am sure but this is quick!
 
Old 08-05-2011, 06:19 AM   #8
veeruk101
Member
 
Registered: Mar 2005
Distribution: Ubuntu 12.04 LTS
Posts: 249

Original Poster
Rep: Reputation: 16
Thanks for the information - I didn't realize the distinction between the laptop's IP address and the public IP address. What I'm trying to do is run a script only when the public IP address changes. I could do it using the cron task as suggested, but if there's another way that doesn't require use of a cron task running every few minutes that would be even better.

But as someone said above, the public IP address "should never change while you're connected/working because that would disrupt any connections you have currently going" - so then if the only time the IP address would change is when my network interface (e.g. wireless card) is restarted, then maybe there's a way for me to run my script at those times and first check whether it changed from the last recorded IP address.

I don't have if-pre-up.d directories as I'm running CentOS - is there any way I could run a script when network interfaces are started or restarted?
 
Old 08-05-2011, 03:18 PM   #9
sneakyimp
Senior Member
 
Registered: Dec 2004
Posts: 1,027

Rep: Reputation: 67
Sadly, I don't know what sort of hooks CentOS might have. I would recommend a search for something like "CentOS network hooks" or something.
 
Old 08-05-2011, 06:27 PM   #10
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Quote:
Originally Posted by veeruk101 View Post
But as someone said above, the public IP address "should never change while you're connected/working because that would disrupt any connections you have currently going" - so then if the only time the IP address would change is when my network interface (e.g. wireless card) is restarted, then maybe there's a way for me to run my script at those times and first check whether it changed from the last recorded IP address.
Ummm ... no; if your wireless IP changes it means that your router
has handed out a new DHCP lease. Your ISP will never know of that
event, and it is completely unrelated to your external IP address.
 
Old 08-05-2011, 06:35 PM   #11
sneakyimp
Senior Member
 
Registered: Dec 2004
Posts: 1,027

Rep: Reputation: 67
Doh. Yes I wasn't thinking. Your computer on your LAN typically won't notice that your ISP has changed your public IP address. Your router would know. Or you could ping some remote site. The remote site could be something as simple as a PHP script:

Code:
<?php
echo $_SERVER['REMOTE_ADDR'];
?>
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
My BASH script work perfect when i run it manualy, but not when it run in the crontab roqarg Linux - Newbie 30 05-06-2010 02:37 PM
run a shell script/cronjob when any email arrive to specific email address ikillu Linux - General 3 05-30-2009 09:18 AM
I want to run script on the server from client machine in windows in a perl script vpradeep Linux - Newbie 2 09-01-2008 04:29 AM
Is a script, run at boot time from init.d, run with root authority? tmbrwolf53 Linux - Server 2 03-31-2007 09:15 PM
on Network Up Script run? On Battery power run script? v2-ncl Linux - General 0 12-08-2003 10:34 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 04:05 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration