Detecting a change of IP address in Linux

前端 未结 7 1352
轮回少年
轮回少年 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条回答
  •  旧时难觅i
    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
    

提交回复
热议问题