Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get notified for IP address changes automatically

In linux is there any method like callback or signal to get notified whenever there is any change in the IP Address. I want to register a callback with with kernel or get a signal from kernel which is done automatically rather my code polling on the socket.

thanks in advance.

like image 337
maheshgupta024 Avatar asked Dec 17 '11 02:12

maheshgupta024


People also ask

Is IP address changes automatically?

When a device is assigned a static IP address, the address does not change. Most devices use dynamic IP addresses, which are assigned by the network when they connect and change over time.

How do I know if my IP has been changed?

To determine when your IP address may change or if it already has, start by determining your current IP address. You can do this by searching “what is my IP address” or using free tools like WhatIsMyIP.com. Then, compare the returned address to the examples below.

How often does an IP address update?

Every 14 days there is a DHCP lease renewal that takes place that acts kind of like a handshake between the ISP and a household modem. If the connection is still valid the ISP will move on and not disrupt service via provisioning a new IP address.

Why is my IP address changing every day?

Generally ISPs use dynamic IP address for home users. Using dynamic IPs allow them to re-utilise the available set of IPs for a large number of users as all the users are not online at all the times. This is why your IP changes every time whenever you create a new connection/everyday.


1 Answers

Without C programming: the command

$ ip monitor

produces output on its stdout, whenever some configuration in the IP subsystem changes. The command

# ip addr add 10.10.10.10/24 dev em1 

produces the following output

2: em1    inet 10.10.10.10/24 scope global em1
local 10.10.10.10 dev em1  table local  proto kernel  scope host  src 0.10.10.10 
10.10.10.0/24 dev em1  proto kernel  scope link  src 10.10.10.10 
broadcast 10.10.10.0 dev em1  table local  proto kernel  scope link  src 10.10.10.10 
broadcast 10.10.10.255 dev em1  table local  proto kernel  scope link  src 10.10.10.10 

deleting the 10.10.10.10 addresss with the command

# ip addr del 10.10.10.10/24 dev em1

produces the following output

Deleted 2: em1    inet 10.10.10.10/24 scope global em1
Deleted 10.10.10.0/24 dev em1  proto kernel  scope link  src 10.10.10.10 
Deleted broadcast 10.10.10.255 dev em1  table local  proto kernel  scope link  src 10.10.10.10 
Deleted broadcast 10.10.10.0 dev em1  table local  proto kernel  scope link  src 10.10.10.10 
Deleted local 10.10.10.10 dev em1  table local  proto kernel  scope host  src 10.10.10.10 

you can use either the shell and some awk or perl to process these messages, or you can use the popen() and friends C library functions and process the outpout in C.

Using C programming you can hook into the kernel via NETLINK. This is rather complicated and not very well documentd. See this Wikipedia article for a starting point into the Netlink interface.

like image 71
bofh.at Avatar answered Oct 29 '22 21:10

bofh.at