LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 12-19-2004, 12:03 AM   #1
jlangelier
Member
 
Registered: Jun 2003
Location: Denver, CO
Distribution: Debian
Posts: 95

Rep: Reputation: 15
Connectivity check, quickly, in a script


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.

Thanks in advance.
 
Old 12-19-2004, 12:29 AM   #2
nixcraft
Member
 
Registered: Nov 2004
Location: BIOS
Distribution: RHEL3.0, FreeBSD 5.x, Debian 3.x, Soaris x86 v10
Posts: 379

Rep: Reputation: 30
Use ping as follows:

ping -c 1 YOUR-HOST.COM
 
Old 12-19-2004, 12:37 AM   #3
jlangelier
Member
 
Registered: Jun 2003
Location: Denver, CO
Distribution: Debian
Posts: 95

Original Poster
Rep: Reputation: 15
Quote:
Use ping as follows:

ping -c 1 YOUR-HOST.COM
That hang on my machine as well
 
Old 12-19-2004, 08:15 AM   #4
pcunix
Member
 
Registered: Dec 2004
Location: MA
Distribution: Various
Posts: 149

Rep: Reputation: 23
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.


--
Tony Lawrence
http://aplawrence.com
 
Old 12-19-2004, 10:20 PM   #5
jlangelier
Member
 
Registered: Jun 2003
Location: Denver, CO
Distribution: Debian
Posts: 95

Original Poster
Rep: Reputation: 15
Thanks, pcunix and nixcraft, for your replys.

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.

Ahh perl. The duct tape of the Internet.

Thanks again
 
Old 12-19-2004, 10:38 PM   #6
btmiller
Senior Member
 
Registered: May 2004
Location: In the DC 'burbs
Distribution: Arch, Scientific Linux, Debian, Ubuntu
Posts: 4,275

Rep: Reputation: 370Reputation: 370Reputation: 370Reputation: 370
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).
 
Old 12-19-2004, 11:19 PM   #7
jlangelier
Member
 
Registered: Jun 2003
Location: Denver, CO
Distribution: Debian
Posts: 95

Original Poster
Rep: Reputation: 15
Quote:
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 11:21 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Script check Echo Kilo Programming 4 12-13-2004 04:55 AM
Script check Echo Kilo Programming 1 12-12-2004 01:55 PM
Script to check groups quack322 Programming 1 08-15-2004 04:16 AM
Ip check script, help me? wiggywag Programming 3 01-13-2004 02:58 AM
how to check libraries you have quickly? yocompia Slackware 3 08-21-2003 05:41 PM


All times are GMT -5. The time now is 10:44 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration