LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Slackware (https://www.linuxquestions.org/questions/slackware-14/)
-   -   Detect network cable being plugged in (https://www.linuxquestions.org/questions/slackware-14/detect-network-cable-being-plugged-in-458854/)

Vgui 06-27-2006 01:39 PM

Detect network cable being plugged in
 
Hi all,
When I plug or unplug my network cable (from eth0, a standard 10/100 nic), and use "dmesg", I see status notices like:
Code:

eth0: network connection down
eth0: network connection up using port A

My question: is there a way to detect these changes without using dmesg? Perhaps something changes in /proc/ or some event I could listen to? Basically I want to run a simple script when the network cable is plugged in. I have tried ifplugd, but it didn't seem to work properly for me.
Any ideas / advice?

cwwilson721 06-27-2006 03:46 PM

Did you try the scripts at Alien Bobs site? They work like a charm for me. Just change all of them at once to be sure everything works right.

Vgui 06-28-2006 11:00 AM

Thanks for the link, it looked like a good soluation, but then I realized it basically wraps ifplugd (and libdaemon), which I still haven't had any luck with.
Any more ideas? If not I guess I will have to write some sort of dmesg watching script :(

katayamma 06-28-2006 11:09 AM

Have you tried syslog? It can log kernel messages that show up in dmesg and you can send output to the console. If you don't want to see all the kernel message ring data, you could log to a file and tail it to the console as a background process with a grep for just ethernet messages.

Not very elegant, but it's a quick hack & slash for what you want.

(Note: That's according to the man page for dmesg. I haven't bothered to test it.)

Alien Bob 06-28-2006 11:14 AM

Quote:

Originally Posted by Vgui
it basically wraps ifplugd (and libdaemon), which I still haven't had any luck with.

Why don't you just give it a try? Can you tell me what went wrong with your own ifplugd experiences?

Eric

Vgui 06-28-2006 11:24 AM

Code:

tail -f /var/log/syslog | grep "eth0: network connection up"
That seems to get me the notification I want; I can latch onto it and call my script when it changes.
I like this solution a bit better for 2 reasons: it's simple and clean, and it doesn't require 2 additional software packages (even if they are tiny).
So although I imagine your script is very useful Alien Bob, it is probably overkill for my needs in this situation. And since I didn't elaborate earlier, the problem I was having with ifplugd was it did not trigger anything when the cable was un/plugged. I could see the process running in 'ps', but it didn't seem to actually do anything. I imagine I could have dug deeper and tried different configurations, but the tail solution seems fine for now.

Thanks a lot everyone!

Alien Bob 06-28-2006 12:20 PM

FYI,
If you want sophisticated logfile monitoring with action triggers, take a look at SEC (simple event correlator) at http://www.estpak.ee/~risto/sec/
I've used this, and found it practical and useful. It's basically no more than one intelligent perl script that can daemonize itself.

Eric

tiekookeit 01-03-2009 06:22 AM

I scripted this, and its work to me,



if [ "`ifconfig eth0|sed -n '/running/I p'`" == '' ];then echo off;else echo on;fi

change eth0 by e eth device you want to.


Good Luck!

Visit Brazil, it a nice place :D


All times are GMT -5. The time now is 06:24 PM.