views:

1938

answers:

5

In a Linux environment, I need to detect the physical connected or disconnected state of an RJ45 connector to its socket. Preferably using BASH scripting only.

The following solutions which have been proposed on other sites do NOT work for this purpose:

  1. Using 'ifconfig' - since a network cable may be connected but the network not properly configured or not currently up.
  2. Ping a host - since the product will be within a LAN using an unknown network configuration and unknown hosts.

Isn't there some state which can be used in the /proc file system (everything else is in there)?

How is the Linux world suppose to have their own version of the Windows bubble that pop up from the icon tray indicating that you've just unplugged the network cable?


Kent Fredric and lothar, both of your answers satisfy my need... thanks a lot! Which one I'll use... I still don't know.

I guess I can't put you both down as the correct answer? And its probably fair for you that I do choose one. Flip a coin I guess? Again, thanks!

A: 

Most modern Linux distributions use NetworkManager for this. You could use D-BUS to listen for the events.

If you want a command-line tool to check the status, you can also use mii-tool, given that you have Ethernet in mind.

andri
mii-tool has been superceded by ethtool. mii-tool is unaware of GigE links.
JimB
additionally, most servers have manually configured adapters, which are ignored by NM.
JimB
+5  A: 

You can use ethtool:

$ sudo ethtool eth0
Settings for eth0:
    Supported ports: [ TP ]
    Supported link modes:   10baseT/Half 10baseT/Full
                            100baseT/Half 100baseT/Full
                            1000baseT/Full
    Supports auto-negotiation: Yes
    Advertised link modes:  10baseT/Half 10baseT/Full
                            100baseT/Half 100baseT/Full
                            1000baseT/Full
    Advertised auto-negotiation: Yes
    Speed: 1000Mb/s
    Duplex: Full
    Port: Twisted Pair
    PHYAD: 0
    Transceiver: internal
    Auto-negotiation: on
    Supports Wake-on: umbg
    Wake-on: g
    Current message level: 0x00000007 (7)
    Link detected: yes

To only get the Link status you can use grep:

$ sudo ethtool eth0 | grep Link
    Link detected: yes
lothar
+8  A: 

You want to look at the nodes in

/sys/class/net/

I experimented with mine:

Wire Plugged in:

eth0/carrier:1
eth0/operstate:unknown

Wire Removed:

eth0/carrier:0
eth0/operstate:down

Wire Plugged in Again:

eth0/operstate:up
eth0/carrier:1

Side Trick: harvesting all properties at once the easy way:

grep "" eth0/*

This forms a nice list of key:value pairs.

Kent Fredric
A: 

You could try this:

gw="$(ip route | awk "/^default via/ {print \$3}")"
ping -c 1 "$gw" > /dev/null
result="$?"

as a workaround for the product will be within a LAN using an unknown network configuration and unknown hosts problem.

If there is something else than "0" in the result, than Your gateway is not responding to ICMP pings. Beware, some router admins might block icmp. I haven't tested it on a networkless workstation (it's unusual to have one, these days).

Reef
Ping wont work if you've got a connection, but no assigned IP address, and not routing table because your router refuses to give you one because it doesn't recognize your MAC address. Yet the cable is still plugged in and you can still have traffic across the medium. So your analysis only determines "routable network" that actually *has* a gateway. If you're just point-to-point with another computer, this will bomb.
Kent Fredric
Additonally, "Unknown networking configuration" also involves non-tcp-ip networks, ie: IPX, AppleTalk, etc etc. There are *lots* of network families and 'ip' is merely one.
Kent Fredric
Yes, that is true, but look what Jeach said in "1.". In my opinion He doesn't really want to find if the cable is there or not, but He wants to check wether the network is up or not.
Reef
A: 

I am having a problem with ethtool. When I call the ethtool from the script inside the cgi-bin it doesnt giving any output. here is my bash shell file

!/bin/bash

char1=$(ethtool eth0 | grep Link | awk '{print $3}')

echo Content-type: text/html echo "" echo $char1 echo "" echo "
" echo "hello" echo ""

but when i try the command in terminal, i can see all the output. then i have changed my script

!/bin/bash

char1=$(ethtool eth0)

echo Content-type: text/html echo "" echo $char1 echo "" echo "
" echo "hello" echo ""

when I check from webpage i am getting the following only

Settings for eth0: No data available

do you know the reason why i am getting like this?

Sri
It has been a while since I've done cgi-scripts. But if it works from command line and not from the web, aren't you missing the two newline chars that are required at the end or something? Try adding '\n\n' within your last 'echo' quotes.
Jeach