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.
---update---
/etc/network/interfaces
auto lo
iface lo inet loopback
auto eth0
iface eth0 inet static
address 10.0.0.152
netmask 255.255.255.0
gateway 10.0.0.1
network 10.0.0.0
broadcast 10.0.0.255
dns-search company.com
dns-nameservers 10.0.0.2 10.0.0.3 10.0.0.4
meter 100 #without this line I get RNETLINK answers: file does not exist
auto eth1
iface eth1 inet dhcp #assigns an address in the 192.168.0.0/24 space with 192.168.0.1 as DNS
meter 200 #without this line I get RNETLINK answers: file does not exist
/etc/resolv.conf
nameserver 10.0.0.2
nameserver 10.0.0.3
nameserver 10.0.0.4
search company.com cable.provider.com
Unless I manually edit the file, my resolv.conf always has the DNS servers for the eth0 connection and both domains in the search path. In order to get the cable connection to work I have to delete the other nameserver lines and add 192.168.0.1 and in order for the company connection to work I just have to delete the cable provider search path.