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:
- Using 'ifconfig' - since a network cable may be connected but the network not properly configured or not currently up.
- 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!
On the low level, these events can be caught using rtnetlink sockets, without any polling. Side note: if you use rtnetlink, you have to work together with udev, or your program may get confused when udev renames a new network interface.
The problem with doing network configurations with shell scripts is that shell scripts are terrible for event handling (such as a network cable being plugged in and out). If you need something more powerful, take a look at my NCD programming language, a programming language designed for network configurations.
For example, a simple NCD script that will print "cable in" and "cable out" to stdout (assuming the interface is already up):
(internally,
net.backend.waitlink()
uses rtnetlink, andnet.backend.waitdevice()
uses udev)The idea of NCD is that you use it exclusively to configure the network, so normally, configuration commands would come in between, such as:
The important part to note is that execution is allowed to regress; in the second example, for instance, if the cable is pulled out, the IP address will automatically be removed.
You want to look at the nodes in
I experimented with mine:
Wire Plugged in:
Wire Removed:
Wire Plugged in Again:
Side Trick: harvesting all properties at once the easy way:
This forms a nice list of
key:value
pairs.I use this command to check a wire is connected:
If the result will be up or down. Sometimes it shows unknown, then you need to check
It shows 0 or 1
Some precisions and tricks
I do all this as normal user (not root)
Grab infos from
dmesg
Using
dmesg
is one of the 1st things to do for inquiring current state of system:could answer something like:
or
depending on state, message could vary depending on hardware and drivers used.
Nota: this could by written
dmesg|grep eth.*Link.is|tail -n1
but I prefer usingsed
.Test around
/sys
pseudo filesystemReading or writting under
/sys
could break your system, especially if run as root! You've been warned ;-)This is a pooling method, not a real event tracking.
Could render something like (once you've unplugged and plugged back, depending ):
(Hit Enter to exit loop)
Nota: This require
patch
to be installed.In fine, there must already be something about this...
Depending on Linux Installation, you could add
if-up
andif-down
scripts to be able to react to this kind of events.On Debian based (like Ubuntu), you could store your scripts into
see
man interfaces
for more infos.on arch linux. (im not sure on other distros) you can view the operstate. which shows up if connected or down if not the operstate lives on
You can use ifconfig.
If the entry shows RUNNING, the interface is physically connected. This will be shown regardless if the interface is configured.
This is just another way to get the information in
/sys/class/net/eth0/operstate
.