Detecting a change of IP address in Linux

前端 未结 7 1344
轮回少年
轮回少年 2020-12-29 10:53

Does anyone know a way to detect a change of IP address in Linux. Say I have dhcpcd running, and it assigns a new IP address, is there a way I can get a notification when it

相关标签:
7条回答
  • 2020-12-29 11:02

    If you have a router running DD-WRT and have the status page in use when going to the router, you can, with a script... wget the status page, cat for the ip address and write it to a file for comparison, have an email send when the latest wget ip address has changed from what is in the comparison file.

    I'm running dd-wrt on a linksys wrt54g router and use this script: It wgets the router status page from 192.168.3.1, uses cat on the page (index.html) and greps for the wan ip address, then writes it to a file (gotip.txt).

    A comparison is made between the captured ip (gotip.txt) and the current working ip (workingip.txt). If the ip addresses are different, I get an email sent by send mail of the new ip, and the new working ip is written into the workingip.txt file.

    Cron run this every 5 min or so and I have the cron output silenced to /dev/null

    #!/bin/bash
    
    getip=$(wget http://192.168.3.1/)
    cat index.html | grep "wan_ipaddr" > gotip.txt
    
    gotip=$(cat gotip.txt)
    compare=$(cat workingip.txt)
    
    if [[ "$compare" != "$gotip" ]]
        then 
        EMAIL="youremail@foo.net"
        EMAILMESSAGE="/home/pi/ipmessage.txt"
        echo "ip address is now $gotip" >> $EMAILMESSAGE
        /usr/sbin/sendmail -t "$EMAIL" < $EMAILMESSAGE
        rm ipmessage.txt
        cp gotip.txt workingip.txt
        rm index.html
    
    else
    echo "done"
    rm index.html
    fi
    
    0 讨论(0)
  • 2020-12-29 11:06

    This is an old question, but I will answer for those who will arrive by Google (such as myself). After struggling for a while, I found out that you don't necessarily need to poll or hack a C solution for this. For my case, I wanted to update my home server's (dynamic dns) domain when the IP changes.

    If you are running dhcpcd, you are in luck. dhcpcd will run hook scripts when anything happens. See man dhcpcd-run-hooks (online here). Basically you will want to modify or create your own dhcpcd.enter-hook or dhcpcd.exit-hook depending on what you want to do with the data provided by the event.

    0 讨论(0)
  • 2020-12-29 11:07

    I think you can use dbus to do this on modern Linux distributions. If your distribution uses NetworkManager, see this document for information about its dbus interface:

    http://people.redhat.com/dcbw/NetworkManager/NetworkManager%20DBUS%20API.txt

    0 讨论(0)
  • 2020-12-29 11:09

    Since DHCP activity is sent to syslogd you could create a named pipe, direct syslog traffic to it and watch the stream for IP address updates. See 'man syslogd' and 'man syslog.conf'.

    Edit: Another approach would be to use inotify to monitor the DHCP leases file for the interface. Under Ubuntu 9.10 that is in the /var/lib/dhcp3 directory.

    0 讨论(0)
  • 2020-12-29 11:16

    The command

    ip monitor
    

    will show you this kind of thing happening. It uses some the netlink API which is rather tricky and not documented well (at least for humans to understand). However, it is able to get notified by the kernel of various events, such as changes of assigned IPs, routing tables and link status (e.g. someone unplugged the network)

    0 讨论(0)
  • 2020-12-29 11:17

    This is an older thread but in case someone finds it like I did, I wrote something that does network change detection/notification in Linux awhile back (mostly targeted at helping VPN users), and thanks to some pushy friends I put it up for others to use. It's a pet project now and I'm actively maintaining it, so feature requests and feedback are welcome.

    http://code.google.com/p/ipcheck/source/browse/ipcheck.sh

    0 讨论(0)
提交回复
热议问题