LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-11-2009, 04:40 AM   #1
christyyim
Member
 
Registered: Oct 2008
Location: earth
Distribution: Ubuntu 'Gutsy Gibbon'
Posts: 32

Rep: Reputation: 15
How to Write a Program in C to detect the Ethernet Cable?


I wish to write a C program to detect whether the Ethernet cable is plugged or unplugged. I found out by using a command "nm-tool" in Linux terminal will show me whether a Ethernet cable is plugged or not. If Ethernet cable is plugged, in the device part of eth0, the Hardware Link of Wired Settings will indicate a "yes" and "no" if no Ethernet cable.

Hence, in my previous code, I use one function called popen to read the state as shown below:

PHP Code:
f1 popen"nm-tool | grep Link""r" );        
            while ( 
fgetscmd_linesizeof cmd_linef1 ))
            {
                
printf"%s"cmd_line );
              }
               
pclosef1 );

            if( 
strstrcmd_line"no" ) == NULL )
            {
                
printf"LAN cable is plugged\n");
            } 
However, now my project wish to not use the NetworkManager (where the "nm-tool" command comes from). And this gives me trouble to detect the Ethernet cable. So is there any other method for me to detect the Ethernet cable in C programming?

Any advice is welcomed and appreciated. Thanks for help.


Christyyim
 
Old 03-11-2009, 04:48 AM   #2
Maligree
Member
 
Registered: Mar 2008
Distribution: Gentoo, CentOS, Fedora, Arch
Posts: 231
Blog Entries: 1

Rep: Reputation: 42
Have you heard of ifplugd? It's a deamon that sets up the appropriate interface when a cable is plugged in. Check its source and see how it's done?
 
Old 03-11-2009, 05:13 AM   #3
christyyim
Member
 
Registered: Oct 2008
Location: earth
Distribution: Ubuntu 'Gutsy Gibbon'
Posts: 32

Original Poster
Rep: Reputation: 15
The reason I cannot use NetworkManager is that I need to set a static IPv6 address permanently to eth0. Once I set the static IP permanently, the NetworkManager will be disabled which make me cannot use the "nm-tool" command because it will give me error. Therefore, I wonder will the ifplugd daemon will give me the same problem? Sorry because I did not state clearly in the previous post.

Thanks.

Christyyim
 
Old 03-11-2009, 05:21 AM   #4
gnashley
Amigo developer
 
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,928

Rep: Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612
There is a program out there that does exactly what you want -it's called 'ethwireck'
(Ethernet Wire Check). It returns true if the ethernet port is connected to a live wire and false if not. It's very handy for use in init scripts where you are connecting using dhcp and don't want to waste time waitning for a timeout if the connection is not even live.
 
Old 03-11-2009, 05:51 AM   #5
ddaemonunics
Member
 
Registered: May 2008
Location: Romania
Distribution: Debian
Posts: 242

Rep: Reputation: 41
ethtool can help you to see if the link is present or not
also ip link show can tell if the CARRIER is present or not

Last edited by ddaemonunics; 03-11-2009 at 05:53 AM.
 
Old 03-11-2009, 10:26 AM   #6
christyyim
Member
 
Registered: Oct 2008
Location: earth
Distribution: Ubuntu 'Gutsy Gibbon'
Posts: 32

Original Poster
Rep: Reputation: 15
First of all, thanks for all replies.

I had tried ethwireck, but it cannot detect my ethernet cable. It gives me the error as follow:
Code:
christyyim@christyyim-laptop:~/Desktop/ethwireck-1.2$ ./ethwireck -v
ETHTOOL_GLINK failed: Operation not permitted
SIOCGMIIPHY on eth0 failed: Operation not permitted
SIOCDEVPRIVATE on eth0 failed: Operation not supported
eth0: could not determine status
Besides that, I had tried ethtool in the terminal, but I need to type sudo to get the information:
PHP Code:
sudo ethtool eth0 
and this will require my password and i cannot use the "popen" command in my C program in this case. And for your information, I am a beginner in programming , quite a big task for me to port the source code from ethtool into my program since when I tried just now, I cannot link some of their header files and libraries, keep giving me a lot of error

So I wonder is there other method for me to detect the Ethernet cable?

Thanks in advance.


Christyyim
 
Old 03-12-2009, 01:30 AM   #7
ddaemonunics
Member
 
Registered: May 2008
Location: Romania
Distribution: Debian
Posts: 242

Rep: Reputation: 41
Well for sudo....modify /etc/sudoers so that you don't have to be prompted for a password for that command and the user that is running as....this is if you don't want to port your application on other systems

Last edited by ddaemonunics; 03-12-2009 at 01:31 AM.
 
Old 03-12-2009, 02:51 AM   #8
jf.argentino
Member
 
Registered: Apr 2008
Location: Toulon (France)
Distribution: FEDORA CORE
Posts: 493

Rep: Reputation: 50
If you have the "/sys" pseudo file system, just get the content of "/sys/class/net/eth0/carrier", 0 means you're unplugged
 
Old 03-13-2009, 12:16 AM   #9
christyyim
Member
 
Registered: Oct 2008
Location: earth
Distribution: Ubuntu 'Gutsy Gibbon'
Posts: 32

Original Poster
Rep: Reputation: 15
yah, thanks jf.argentino. You solved my problem. Thanks.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Detect network cable being plugged in Vgui Slackware 7 01-03-2009 06:22 AM
Cant detect my cable modem ? medned Linux - Newbie 3 07-27-2008 11:11 AM
Mobile not Detect Through USB Cable in RHEL 5 linux786 Linux - Newbie 1 01-03-2008 03:10 AM
Ethernet cable kluelesswithknoppix Linux - Networking 2 01-30-2006 09:07 PM
suse 10.1 Didnt detect cable internet what do I do? CrYpTiC-N1Nj4 SUSE / openSUSE 2 11-20-2005 09:26 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:37 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration