How to Write a Program in C to detect the Ethernet Cable?

This is a discussion on How to Write a Program in C to detect the Ethernet Cable? within the C Programming forums, part of the General Programming Boards category; I wish to write a C program to detect whether the Ethernet cable is plugged or unplugged. I found out ...

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    2

    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:
    [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");
                }[/
    CODE
    However, now my project wish to not use the NetworkManager (where the "nm-tool" command comes from) because I have to set a static IP address permanently for eth0 and once I set the static IP, nm-tool cannot function. 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.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I'm very convinced that attempting to do this in C is the wrong thing. There is a high likelihood that there is an existing published mechanism for detecting whether the cable is connected or not.

    Edit: I believe (from some googling) that /sys/devices/... will have the information you need (where .... is the appropriate path to your network device). The information in /sys/devices is in text-form, so tools like grep will find what you want.

    --
    Mats
    Last edited by matsp; 03-11-2009 at 04:19 AM.
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program to write to memory map addresses
    By modest in forum C Programming
    Replies: 15
    Last Post: 06-03-2008, 03:20 PM
  2. Newbie needs help..
    By xpress urself in forum C++ Programming
    Replies: 3
    Last Post: 07-26-2007, 07:22 PM
  3. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 10:17 PM
  4. Help !!! need to write program asap
    By Adamakl in forum C Programming
    Replies: 3
    Last Post: 11-12-2006, 10:03 AM
  5. Challenge to write a program
    By Twisted.alice in forum A Brief History of Cprogramming.com
    Replies: 40
    Last Post: 05-15-2003, 12:00 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21