Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
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.
I'm writing a perl script, and in it I want to see if I'm connected to the internet (I'm using a cable modem).
I tried ping (setting the -w option set to 1, or the -W option set to 1, or the -t option set to 1) but if I'm not connected, it hangs (I don't know how long it hangs, but it's over a half minute before I kill it. If I'm not connected, I don't want it hanging for more than a second) Ideally, I'd like a command which would come back within a second saying I'm not connected.
Also tried ftp, wget, whois... they all hang for a long time if not connected.
Are using a numeric ip address? If you are not, ping will hang much longer (though it will still return) because it first has to resolve the host name.
But even with a numeric address, it isn't going to return instantly, and you wouldn't want it to, because networks can be slow.
I'd "ping -c1" my (numeric) gateway address first. If that fails, you are definitely going nowhere. If that's on your lan, then "-w 1" is fine, but I would not use -w for anything past a local router because temporarily slow does not mean not connected.
If it succeeds, you still don't really know that you have connectivity, so I'd next try the closest thing I know - like my isp's dns or mail server. Depending on how seriously you define "connectivity", I'd then move on to checking things farther away or the things I already know I need to talk to.
Further, ping doesn't absolutely say you have any high level connectivity, so if you REALLY need to know, then you need to do more, like a lynx dump for a web page, a test ftp connection, or whatever tells you that by gum you really do have full connectivity.
I won't go into why I needed to see if I was connected, quickly, but here's how I solved it, in case anyone's interested.
In my script, I grabbed the RX bytes from ifconfig for my eth0 interface.
I then forked a 'ping -c 1' process.
After a second, I checked the RX bytes to see if they had changed.
You can also check the return status of the ping command. It will return 0 if it was able to ping the remote host, and 1 otherwise. This is probably more reliable, because just the face that your ethernet interface received a packet doesn't necessarily mean that you're connected (the received packet xcould've been from your gateway, for instance).
You can also check the return status of the ping command.
But if I'm not connected, the return status won't be available for at least a half minute. I wanted a solution which would return 'not connected' within a second.
You are right, though. My solution wouldn't work if I was going through a gateway. But I'm not, so it works for me
Last edited by jlangelier; 12-19-2004 at 10:21 PM.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.