What could prevent frequently switching default source ip of a machine with several interfaces

155 views Asked by At

The goal was to frequently change default outgoing source ip on a machine with multiple interfaces and live ips.

I used ip route replace default as per its documentation and let a script run in loop for some interval. It changes source ip fine for a while but then all internet access to the machine is lost. It has to be remotely rebooted from a web interface to have any thing working

Is there any thing that could possibly prevent this from working stably. I have tried this on more than one servers?

Following is a minimum example

# extract all currently active source ips except loopback
IPs="$(ifconfig  | grep 'inet addr:'| grep -v '127.0.0.1' | cut -d: -f2 |
awk '{ print $1}')"

read -a ip_arr <<<$IPs

# extract all currently active mac / ethernet addresses
Int="$(ifconfig  | grep 'eth'| grep -v 'lo' | awk '{print $1}')"
read -a eth_arr <<<$Int

ip_len=${#ip_arr[@]}
eth_len=${#eth_arr[@]}

i=0;
e=0;

while(true); do

    #ip route replace 0.0.0.0 dev eth0:1 src 192.168.1.18
    route_cmd="ip route replace 0.0.0.0 dev ${eth_arr[e]} src ${ip_arr[i]}"
    echo $route_cmd
    eval $route_cmd

    sleep 300

    (i++)
    (e++)

    if [ $i -eq $ip_len ]; then
        i=0;
        e=0;
        echo "all ips exhausted - starting from first again"
    #   break;
    fi

done
1

There are 1 answers

8
Morph On BEST ANSWER

I wanted to comment, but as I'm not having enough points, it won't let me.

Consider:

  • Does varying the delay time before its run again change the number of iterations before it fails?
  • Exporting the ifconfig & routes every time you change it, to see if something is meaningfully different over time. Maybe some basic tests to it (ping, nslookup, etc) Basically, find what is exactly going wrong with it. Also exporting the commands you send to a logfile (text file per change?) to see changes in them to see if some is different after x iterations.
  • What connectivity is lost? Incoming? Outgoing? Specific applications?
  • You say you use/do this on other servers without problems?
  • Are the IP's: Static (/etc/network/interfaces), bootp/DHCP, semi-static (bootp/DHCP server serving, based on MAC address), and if served by bootp/DHCP, what is the lease duration?

On the last remark: bootp/dhcp will give IP's for x duration. say its 60 minutes. After half that time it will "check" with the bootp/dhcp server if it can keep the IP, and extend the lease to 60 minutes again, this can mean a small reconfig on the ifconfig (maybe even at the same time of your script?).

hth