Search code examples
shelliprefresharpneighbours

Shell Script IP Neighbor Refresh


I am working on an application of multiple devices in the same network. One of them takes the lead and performs some functions. I arbitrate the "lead" by the highest IP I find.

My application is intended to be fault-tolerant and if I loose the controller, next in the line should take over. I am running the script below.

#define SHELLSCRIPT "\
    #/bin/bash \n\
    echo $(ifconfig | grep -A 1 'eth0' | tail -1 | cut -d ':' -f 2 | cut -d ' ' -f 1) > IP.txt\n\
    while read LINE; do echo $LINE | grep -i -E 192.168 | grep -v .254 | cut -c1-13; done < /proc/net/arp >> IP.txt\n\
    "

The problem I have found is that the ARP cache does not refresh automatically. Does anyone has any idea how to "refresh" the ARP table (without using arp commands). I have already tried "ip -s neigh flush all", but it seems to have a delay to refresh the ARP table.

Anyway, I would appreciate any suggestion.


Solution

  • You can disable and enable the use of the ARP protocol on interface at the same time.
    ARP cache will be cleared quitly fast.

    ip link set arp off dev eth0 ; ip link set arp on dev eth0
    

    Please, at first run this on test environment for checking and be sure there no connection interruption.