-   Linux - Networking (
-   -   Set up IP on specific eth device? (

johnnybhoy67 10-20-2005 07:01 AM

Set up IP on specific eth device?
Hi All,

I'd like to be able to set up IP on eth? depending on which device the cable is plugged into.

I have more than one network card in my machine for various reasons and need to script the commands to configure whichever device is currently plugged in?

Is this possible and if so how can I do this?

Many Thanks


demian 10-20-2005 08:12 PM

If your network card supports MII queries you can use mii-tool to find out if a link could be negotiated. Example output for an established link is something like:

[demian@dirac]# mii-tool eth0
eth0: negotiated 100baseTx-FD, link ok

without a cable it's this:

[demian@dirac]# mii-tool eth3
eth3: Link down, cable problem?

Then you can just grep for "link ok" or something and start an appropriate ifconfig command.

johnnybhoy67 10-21-2005 09:01 AM

what if it doesn't support these queries?
Would it be possible to setup all eth devices and then try each for connection or is that just silly?



demian 10-21-2005 12:33 PM

Sure you can do that. Pseudo code:

ifconfig eth0 up blah bleh

if (! ping -c 1
ifconfig eth0 down
ifconfig eth1 up blah bleh

You might want to send more than on ping packet if there's a chance for packet loss. However, I don't know what the return code of ping on partial packet loss is. But it's probably still 0.

just reread your question:

Would it be possible to setup all eth devices and then try each for connection
I take it you want to assign a certain ip to whatever interface happens to be connected, right? One thing you have to be careful about is to not assign the same ip to two interfaces. This will lead to routing problems. Also assigning to different ips from the same network will lead to arp flux problems. So you need to deconfigure one interface before the other comes up.

Another possible solution (the proper albeit most difficult one) is to use ethernet bonding (see /usr/src/linux/Documentation/networking/bonding.txt)

All times are GMT -5. The time now is 12:48 PM.