Skip to main content
1 of 2

Issue with network switching

I have a desktop PC running debian that has two network cards installed. One network card is connected to a secure company network and requires a static IP. The other network card (eth1) is connected to a dlink router and then to a cable modem with DHCP addressing.

I'd like to create a script to switch between the two networks. I want to make sure that the two networks aren't connected at the same time. Right now my script checks the up/down status of each interface and toggles them with ifup/ifdown. However, I'm having a number of issues trying to switch networks which I'll attempt to outline but I get the feeling that I'm just going about this the wrong way.

If I configure /etc/network/interfaces to reflect the static IP settings required for the company network connection and the DHCP required for the cable connection than I get the error "RTNETLINK answers: file exists" because I have more than one gateway defined in the file.

If I define the gateways via 'route add' and include metering ifup won't bring up eth0. If I leave eth0 and eth1 undefined in the interfaces file and use network manager to manage them, ifup won't bring up either interface. In both cases I get the error "Failed to bring up eth#" (replace # with either 0 or 1) with no explanation for further detail when I try to use ifup.

If I replace ifup/ifdown with "ifconfig eth# up/down" than the interfaces go up and down properly but the DNS settings in resolv.conf don't update so I can't do much. Adding "resolvconf -u" doesn't seem to do the trick either.

I can get everything working properly on either network if I manually edit resolv.conf after switching but I'd like to have this as automated and easy as possible. Any help you can provide is greatly appreciated.