Detecting a change of IP address in Linux

前端 未结 7 1345
轮回少年
轮回少年 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:21

    What I thought of was running this script from cron every 10 or so minutes, depending on your link. If I wrote this right, it only nsupdates when there is an IP change, so no undue load is creater on the zone's master server.

    #!/bin/bash
    
    OLD_IP=`cat ip.txt`
    
    NEW_IP=`/sbin/ifconfig  | awk -F "[: ]+'{ print $4}'` #adapted from something I got from the internets.
    
    if [ $NEW_IP != OLD_IP ]; then
        nsupdate <commands> #it seems like the keys need to be in the same directory from where nsupdate was called
    fi
    
    echo $NEW_IP > ip.txt
    
    exit 0 #not sure if this is necessary
    

    Not tested!

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