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:
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?
[CODE]f1 = popen( "nm-tool | grep Link", "r" );
while ( fgets( cmd_line, sizeof cmd_line, f1 ))
printf( "%s", cmd_line );
pclose( f1 );
if( strstr( cmd_line, "no" ) == NULL )
printf( "LAN cable is plugged\n");
Any advice is welcomed and appreciated. Thanks for help.