Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I actually wanted to post the below as an answer to this question. But am not sure if this is right approach or it's a way of screwing up things badly.

We can actually check if the network cable is connected to the machine, or not, by running the command,

grep "" /sys/class/net/eth0/carrier

When the network cable is plugged in, the output would be 1 for the above command and when it is not plugged in, the output would be 0 for the above command. I tested the above part in my system and it worked fine as expected.

Now the question lies in how to include this condition on startup. We can include this in the network startup file, which can check this condition before bringing up the interfaces. In RHEL box the file /etc/sysconfig/network-scripts/ifcfg-eth0 is used to bring up the network interfaces.

So, If I change the file to look like,

cable=$(grep "" /sys/class/net/eth0/carrier)
if $cable -eq 1 then
   #rest of the /etc/sysconfig/network-scripts/ifcfg-eth0 as it is. 
else
   break

However, before proposing this method, or even trying on my machine, my question is: When the services are being brought up will the /sys/class/net/eth0/carrier be ready and available? Assuming this approach works, if I decide to use wireless network will there be a problem?

share|improve this question

1 Answer 1

up vote 1 down vote accepted

Unfortunately, the interface has to be up for the above suggested method to work. I made the changes in my file as I suggested in the question and ended up receiving the error as "Invalid argument". So, I believe the interface should be up to query if the wired connection is available or not. I also found more useful information for this answer here.

cat /sys/class/net/ethX is by far the easiest method.

The interface has to be up though, else you will get an invalid argument error.

So first:

ifconfig ethX up

Then:

cat /sys/class/net/ethX
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.