LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Run script when IP address changes (https://www.linuxquestions.org/questions/linux-newbie-8/run-script-when-ip-address-changes-895539/)

veeruk101 08-04-2011 08:54 AM

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.

bskrakes 08-04-2011 09:26 AM

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.

veeruk101 08-04-2011 11:32 AM

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.)

Tinkster 08-04-2011 11:56 AM

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

sneakyimp 08-04-2011 01:43 PM

Quote:

Originally Posted by veeruk101 (Post 4433753)
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.

bskrakes 08-04-2011 01:55 PM

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?

bskrakes 08-04-2011 02:05 PM

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!

veeruk101 08-05-2011 05:19 AM

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?

sneakyimp 08-05-2011 02:18 PM

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.

Tinkster 08-05-2011 05:27 PM

Quote:

Originally Posted by veeruk101 (Post 4434404)
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.

sneakyimp 08-05-2011 05:35 PM

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'];
?>



All times are GMT -5. The time now is 12:56 AM.