wolfpi024
Posts: 5
Joined: Mon Jun 29, 2015 9:33 pm

[Pi B+] How to detect Ethernet link by low level reading

Mon Jun 29, 2015 9:41 pm

Hello folks,

I am fairly new to the Pi community, although I already have acquired some linux skills with debian based distros over the years. Now I am trying to write a script that will check if an ethernet cable is plugged in the LAN9514 chip of my Pi B+ on boot. I want to solve this by reading the status bit of the GPIO pin which is responsible for the green LAN LED (the one that indicates a link). If a link is detected, my script will enable the eth0 interface for this boot, if not it will stay disabled. The only thing missing is my lack of information towards how to read the status bit of the ethernet chip. Can someone help me with that or provide information that the GPIO pins of the ethernet chip can't be read in a terminal?


PS: I am only using SSH / command line, no GUI system.

User avatar
DougieLawson
Posts: 39302
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: [Pi B+] How to detect Ethernet link by low level reading

Mon Jun 29, 2015 11:47 pm

You'll do much better (and it's much easier) to connect to something on the other end of your ethernet cable.

Code: Select all

#!/usr/bin/python3
import socket
import os
testIP = "8.8.8.8"
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect((testIP, 0))
ipaddr = s.getsockname()[0]
host = socket.gethostname()
print ("IP:", ipaddr, " Host:", host)
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

wolfpi024
Posts: 5
Joined: Mon Jun 29, 2015 9:33 pm

Re: [Pi B+] How to detect Ethernet link by low level reading

Tue Jun 30, 2015 12:44 am

Thanks, but that's not my question at all. I have no connectivity problems. My plan is to run the Pi headless on a wifi connection, but if I plug in an ethernet cable prior to starting, then switch on the eth0 interface.

User avatar
rpdom
Posts: 17275
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: [Pi B+] How to detect Ethernet link by low level reading

Tue Jun 30, 2015 6:06 am

Rather than poking around to find how to access the LED status, you could use ethtool to interrogate the interface.

Code: Select all

pi@raspi2 ~ $ ethtool eth0
Settings for eth0:
	Supported ports: [ TP MII ]
	Supported link modes:   10baseT/Half 10baseT/Full 
	                        100baseT/Half 100baseT/Full 
(blah...blah...blah)
	Auto-negotiation: on
Cannot get wake-on-lan settings: Operation not permitted
	Current message level: 0x00000007 (7)
			       drv probe link
	Link detected: yes
The last line "Link detected" is the one you're interested in.

User avatar
DougieLawson
Posts: 39302
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: [Pi B+] How to detect Ethernet link by low level reading

Tue Jun 30, 2015 8:05 am

wolfpi024 wrote:My plan is to run the Pi headless on a wifi connection, but if I plug in an ethernet cable prior to starting, then switch on the eth0 interface.
There's a package called ifplugd that will do that. It's already installed in Raspbian.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

wolfpi024
Posts: 5
Joined: Mon Jun 29, 2015 9:33 pm

Re: [Pi B+] How to detect Ethernet link by low level reading

Tue Jun 30, 2015 10:50 am

Ow, thanks. ifplugd is exactly what I needed :) (ethtool's link detection only works if the interface is up afaik, thus not rally viable :( )

Edit 1:

Sadly though, it doesn't work. even without a cable inserted, /etc/init.d/ifplugd restart leads to:

Code: Select all

ifplugd restart
[ ok ] Network Interface Plugging Daemon...stop eth0...done.
[ ok ] Network Interface Plugging Daemon...start eth0...done.
My ifplugd config contains the following:

Code: Select all

INTERFACES="eth0"
HOTPLUG_INTERFACES=""
ARGS="-l -q -f -u0 -d10 -w -I"
SUSPEND_ACTION="stop"
Edit 2:
Also, when I bring eth0 down manually (without a cable inserted), ifplugd immediately brings it up again. So basically it does exactly the opposite of what it is supposed to do. :|


Edit 3:
I am actually going to use ethtool or maybe exploit its functionality, as I discovered a loophole in the whole matter. Whilst the "link detected" output is not reliable for my cause, the "Speed:" output actually is. I will provide my script when it's done so that future users don't have to figure it out themselves. :)


-------------------------------------

Final Edit: This is the final script I wrote, which controls eth0 when executed. If eth0 is up without an existing cable connection, the script will bring down the interface. If eth0 is not up with an existing cable connection, it will bring it up. Have fun =)

Code: Select all

#!/bin/bash
#Extract speed info from ethtool. If speed is 10 Mb/s, no cable is connected. If it is 100 Mb/s, there is a cable present.
SPEED=`ethtool eth0 | grep -i "Speed" | awk '{print $2}' | grep -o '[0-9]*'`
if ifconfig | grep -i "eth0"  > /dev/null 2>&1; then
        echo "Ethernet interface is already up. Checking Cable connection..."
        if [ $SPEED == 100 ]; then
                echo "Ethernet connection OK."
        else
                echo "Error: No cable connected. Bringing down eth0"
                ifdown --force eth0
                exit
        fi


        exit
else
        if [ $SPEED == 100 ]; then
                echo "Cable connection detected. Bringing up eth0..."
                ifup eth0
        else
                echo "Error: No cable connected."
                exit
        fi
fi
exit

Return to “Advanced users”