Linux - NetworkingThis forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
This sounds like a dumb question, but I'm writing some Perl scripts that need to know whether networking is on. Is there an easy and quick way to do that, and do that in a general way that will work for many different Linux machines?
I agree that this would work, but the problem is that you'd have to wait a few seconds for a ping to return. Is there a way to determine if networking is working in a much faster way? Simply check the state of some environment variable, or parse ipconfig output, or /proc/net files?
Also, how can I determine, in an automated way, the IP address of the Gateway?
I agree that this would work, but the problem is that you'd have to wait a few seconds for a ping to return.
Code:
tred@lt:~$ ping -c 1 gateway
PING gateway (10.0.0.2) 56(84) bytes of data.
64 bytes from gateway: icmp_seq=1 ttl=255 time=2.31 ms
That's about 1% of the time it takes you to blink. So, what's the problem?
Quote:
Also, how can I determine, in an automated way, the IP address of the Gateway?
That would probably be distro-specific.
Perhaps if you told us a little more about what you are trying to achieve, and why, we could point you in better direction(s) to help you find your answer(s).
Okay, 2 ms is definitely fast enough. That will work. I just need to figure out how to automatically determine my gateway IP on a RedHat/Fedora distribution.
Did you manually determine your gateway IP and put that in /etc/hosts, or something similar? Is that how ping knows what "gateway" is for you?
Actually, it's only 2 ms if the network is running, and the gateway is 2 ms away. If your network cable is disconnected, then the ping command hangs for about 5 seconds.
Your previous posts answers how to determine the gateway IP. Thank you.
But the problem is that this method takes about 5 seconds to determine whether or not the network is up (in the case where the network isn't up). Try it. Disconnect you ethernet cable, then run "ping -c 1 gateway". I'd like a method that determines fairly quickly, as in tenths of a second, whether or not the network interface is on, and the machine can talk to the rest of the world.
Try reading the original question. These are Perl scripts that will be running the ping command, not human beings. They will wait until ping finishes even if they don't receive the response from the GW within a second.
The alternative would be to fork another process and then check the output of that process repeatedly, and terminate after a few tenths of a second. That's certainly possible, but it seems like a lot of effort just to determine whether or not the network is connected.
If you want to find out if computer REALLY can use network, script needs to ping GW and waits for result only within 1~1,5 seconds, by this result script can determine if a network is or isn't available.
If one want to just make sure that ethernet cable is connected to something, you can try to check output of "ethtool" and "Link detected: yes/no". Ethtool should be in most linux distributions.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.