[NBLUG/talk] Ethernet connection status

Ron Wickersham rjw at alembic.com
Tue Jun 27 16:51:55 PDT 2006


On Tue, 27 Jun 2006, Barry Stump wrote:

> Date: Tue, 27 Jun 2006 14:29:53 -0700 (PDT)
> From: Barry Stump <barry at shrewdlogic.com>
> To: "General NBLUG chatter about anything Linux, answers to questions,
>     etc." <talk at nblug.org>
> Subject: [NBLUG/talk] Ethernet connection status
>
>
> Does anyone know of an easy way to determine the status of an ethernet
> connection from a shell script?  I'm hoping there's some parameter for
> ifconfig or possibly something in the /proc filesystem that will do it.
> I'd like to be able to determine whether or not eth0 is up and connected.
> A disconnected cable at boot time is easy to detect because eth0 won't
> initialize and therefore won't show up in the output from ifconfig.
> However, disconnecting the cable once the box is running doesn't change
> the output, and I don't want to resort to ping.

not all linux distributions show the behavior you quote (in fact i'm
not sure if any do) and the interfaces will be configured on boot up
and show in ifconfig whether the interaces are connected or not.

to broaden the issue to unix-like systems in general, OpenBSD is very
informative on ifconfig so it's trivial to grep for active on an interface
and see the link layer connectivity.   here's an interface that i pulled
the cable from and then reinserted:

# ifconfig hme3
hme3: flags=8863<UP,BROADCAST,NOTRAILERS,RUNNING,SIMPLEX,MULTICAST> mtu 1500
        lladdr 08:00:20:cc:df:f0
	groups: egress
	media: Ethernet autoselect (none)
	status: no carrier
	inet 192.82.17.44 netmask 0xffffff00 broadcast 192.82.17.255
	inet6 fe80::a00:20ff:fecc:dff0%hme0 prefixlen 64 scopeid 0x6

# ifconfig hme3
hme3: flags=8063<UP,BROADCAST,NOTRAILERS,RUNNING,MULTICAST> mtu 1500
	lladdr 08:00:20:cc:df:f0
	groups: egress
	media: Ethernet autoselect (100baseTX full-duplex)
	status: active
	inet 192.82.17.44 netmask 0xffffff00 broadcast 192.82.17.255
	inet6 fe80::a00:20ff:fecc:dff0%hme0 prefixlen 64 scopeid 0x6

(in OpenBSD the interfaces are named for the hardware type, in this case
the happy-meal ethernet device which autonegotiates 10/100 half/full...
part of a quad port sbus card)

i don't find in general that UP and RUNNING show link status on linux
or unix.

and also point out that link status is not the same as internet
connectivity so think that even though you don't desire to use ping,
i'd suggest reconsidering, and at least ping the gateway address to
give you the internet level status of connectivity to the first hop.

i've found automating backup connectivity schemes with shell scripts
using ping to be very effective and trouble-free.   you didn't mention
what you wanted your script to do, but if you can't communicate you
can't page or e-mail or otherwise notify you remotely that there is
some trouble.

one other non-ping approach is to use dig or host to cause the resolver
to interrogate a name server and see if you have connectivity beyond
the gateway (and DNS uses UDP so you're not using ICMP as you would be
with ping if that's what you want to avoid).  this also is more robust
if you have multiple name servers configured since a failure of one
doesn't report that the whole Internet is unreachable - the resolver
will go down the list.

if there are some special considerations you have then posting more
about what your goals and constraints are would be helpful.

-ron



More information about the talk mailing list