LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Slackware
User Name
Password
Slackware This Forum is for the discussion of Slackware Linux.

Notices

Reply
 
Search this Thread
Old 10-10-2008, 01:22 PM   #1
jong357
Senior Member
 
Registered: May 2003
Location: Columbus, OH
Distribution: DIYSlackware
Posts: 1,914

Rep: Reputation: 52
Checking for internet connection


Anyone see any problems with this? Sloppy? Hackish? Would fail under unforeseen circumstances?

Code:
#!/bin/sh

internet()
{
   # Google
   ping -c 1 66.102.9.104 > /dev/null
   if [ "$?" -eq 0 ]; then
      INTERNET=yes
   else
      INTERNET=no
   fi
}

internet && echo "Internet connection... $INTERNET"
Would you be annoyed if this function was run 150 times before you got to the end of a build project? Would google server bots (fictional or otherwise) shoot you down after doing this 150 times in a 4-6 hour period?

Trying to make automatic source code fetching more robust in my gnome build but.... Not so sure I like that idea off hand.

Suggestions? Comments?

Thanks!
 
Old 10-10-2008, 01:50 PM   #2
raconteur
Member
 
Registered: Dec 2007
Location: Slightly left of center
Distribution: slackware
Posts: 276
Blog Entries: 2

Rep: Reputation: 44
Quote:
Originally Posted by jong357 View Post
Anyone see any problems with this? Sloppy? Hackish? Would fail under unforeseen circumstances?
[...]
All of the above, I'm afraid -- IMO, of course.
Maybe a better approach would be to query the output of ifconfig?
That way, there's no need to actually contact any server, and it avoids the pitfall of the above code failing or possibly babbling to stderr when the interface isn't active.
 
Old 10-10-2008, 02:04 PM   #3
jong357
Senior Member
 
Registered: May 2003
Location: Columbus, OH
Distribution: DIYSlackware
Posts: 1,914

Original Poster
Rep: Reputation: 52
Quote:
Originally Posted by raconteur View Post
All of the above, I'm afraid -- IMO, of course.
Care to elaborate a little? Such as when this might fail even tho you have an active internet connection? I'm not too worried about it being hackish, as long as it gets the job done.

Quote:
Maybe a better approach would be to query the output of ifconfig?
That way, there's no need to actually contact any server
That was my first thought but what would you query and how? You could have a whole slew of devices in ifconfig, many of which would be invalid for communicating with the outside.

Quote:
and it avoids the pitfall of the above code failing or possibly babbling to stderr when the interface isn't active.
Have you ran the script? I don't see how that could possibly happen off hand.

Code:
root@darkstar:~/Desktop# /etc/rc.d/rc.inet1 stop
root@darkstar:~/Desktop# ifconfig
root@darkstar:~/Desktop# sh test
connect: Network is unreachable
Internet connection... no
root@darkstar:~/Desktop# /etc/rc.d/rc.inet1 start
Polling for DHCP server on interface eth0:
dhcpcd: MAC address = **:**:**:**:**:**
dhcpcd: your IP address = 192.168.1.3
root@darkstar:~/Desktop# sh test
Internet connection... yes
As near as I can tell, the function _should_ perform flawlessly 100% of the time. That's really all I'm worried about.

I can walk to my patch panel and unplug my desktop and the script works. Plug it back in and the script works....

Last edited by jong357; 10-10-2008 at 02:09 PM.
 
Old 10-10-2008, 02:59 PM   #4
raconteur
Member
 
Registered: Dec 2007
Location: Slightly left of center
Distribution: slackware
Posts: 276
Blog Entries: 2

Rep: Reputation: 44
Quote:
Originally Posted by jong357 View Post
Care to elaborate a little? Such as when this might fail even tho you have an active internet connection? I'm not too worried about it being hackish, as long as it gets the job done[...]
Uhmm, you asked for an opinion, I gave you mine.
Your script does babble to stderr (easily corrected if you wish) and it relies on a single Google server being up and available.
 
Old 10-10-2008, 03:00 PM   #5
ErV
Senior Member
 
Registered: Mar 2007
Location: Russia
Distribution: Slackware 12.2
Posts: 1,202
Blog Entries: 3

Rep: Reputation: 62
Quote:
Originally Posted by jong357 View Post
Anyone see any problems with this?
And if ping is blocked by iptables? Or if google moves its server to another address?
Try to do it another way. I don't think you really need this check. If you try to connect to external resources, and operations fails, than this means that there is a problem, like missing internet connection.
 
Old 10-10-2008, 03:01 PM   #6
niels.horn
Senior Member
 
Registered: Mar 2007
Location: Rio de Janeiro - Brazil
Distribution: Slackware64-current
Posts: 1,004

Rep: Reputation: 89
If you allow me to give my opinion. Well, you asked for our opinion actually

I have written hundreds of 'hackish' scripts over the years and if it is something just for me that I do not intend to publish or share on several computers, that's just fine as long as it does the job.

But I did not understand what exactly you are trying to accomplish.
If you are downloading source and building at the same time, this sounds like a bad idea to me.
I would first download everything needed - then check if everything is complete and checks out ok (MD5, whatever) and THEN build it all.

Another idea: why check the internet connection by checking if you can reach Google? And why by IP address?
Possible point of failure: Your DNS goes down so your test will succeed, but you won't be able to contact ftp.xxxxxx.com

An idea might be:
- check your local ppp device if it is connected with ifconfig + grep + awk in some combination (I have a hackish script that does just that for my GSM modem)
- check the status of your ADSL modem (if that is your case) with telnet and filter the result (other hackish script here)

Just some ideas!
 
Old 10-10-2008, 03:58 PM   #7
jong357
Senior Member
 
Registered: May 2003
Location: Columbus, OH
Distribution: DIYSlackware
Posts: 1,914

Original Poster
Rep: Reputation: 52
Thanks guys for your input. raconteur, I wasn't trying to be snippy, just asking for more depth to your explanation is all.

Here's the deal. Each build script is designed to fetch the source code if not found. Well, that was the way they worked until I changed them. Slightly broken ATM.

I added the -N switch to wget to combat corrupted downloads but I also have a contrib/ directory that people might be using if they need to grab the source code from another computer, then take everything home to build on their box. Dialup modems, dorm room internet restrictions and the like dictate a means to grab all the source before you start building. Not everyone has the luxury of high speed internet at home.

Without checking for an active internet connection in each build script, the wget command is ran, and then hangs there forever because of no internet. Adding --dns-timeout to wget is pretty laughable so I've got to come up with some other way to skip downloading. My idea was if $INTERNET = yes then wget.

If someone happened to run the contrib/ directory but then also had an active internet connection during build time, then a little time is wasted querying the server for a timestamp (-N) but otherwise harmless.

Has anyone ever seen google down? I'm banking the answer is no. They are also static and not likely to change ever. possible but not probable.

But point taken about the IP. Not sure why I didn't use domain in the first place. Can't say that I see a problem with stderr output, but sure, I could change it.

I've thought about MD5 in the past but how do you compare hashes when your unwilling to maintain separate MD5 files for almost 200 tarballs. Not all upstream servers keep .md5sum's....

The problem stems from me trying to hand hold is about all it equates to. I'm also not interested in forcing people to run the contrib/ directory and removing wget from the children. You've kinda got to be familiar with my build system. The children have to retain source code fetching for ease of upgrading.

And back to ifconfig checking.... I'm a thoroughbred "hack" so I use the term loosely. I'm after simplicity/elegance/functionality when I script. If I were to do a 'proper' (again used loosely) check with ifconfig, I can see winding up with a 100 line function. I think I'd rather use a club than a scalpel. But then again, my skills with awk aren't up to snuff.

I'm half tempted to go back to my original if ! found then wget conditional.... grrrrr......

Thanks again guys. I do appreciate the comments and all are taken seriously and considered (otherwise I'd have no reason to ask).

Last edited by jong357; 10-10-2008 at 04:02 PM.
 
Old 10-10-2008, 04:12 PM   #8
jong357
Senior Member
 
Registered: May 2003
Location: Columbus, OH
Distribution: DIYSlackware
Posts: 1,914

Original Poster
Rep: Reputation: 52
Quote:
Originally Posted by niels.horn View Post
An idea might be:
- check your local ppp device if it is connected with ifconfig + grep + awk in some combination (I have a hackish script that does just that for my GSM modem)
- check the status of your ADSL modem (if that is your case) with telnet and filter the result (other hackish script here)

Just some ideas!
Don't suppose you'd mind posting some relevant snippets would you? You could also PM me as well I suppose if you consider the term 'hack' a derogatory one...
 
Old 10-10-2008, 04:23 PM   #9
niels.horn
Senior Member
 
Registered: Mar 2007
Location: Rio de Janeiro - Brazil
Distribution: Slackware64-current
Posts: 1,004

Rep: Reputation: 89
Don't worry, I'll post them with a "no-guarantee-at-all" or "YMMV" disclaimer.
I'm still in the office, but I'll post them later tonight or during the weekend.
 
Old 10-10-2008, 05:20 PM   #10
ErV
Senior Member
 
Registered: Mar 2007
Location: Russia
Distribution: Slackware 12.2
Posts: 1,202
Blog Entries: 3

Rep: Reputation: 62
Quote:
Originally Posted by jong357 View Post
Has anyone ever seen google down?
Well, I saw a thread of a person unable to connect google at certain hours.

Quote:
Originally Posted by jong357 View Post
They are also static and not likely to change ever. possible but not probable.
In situation you described I'd recommend to ping DNS servers.
 
Old 10-10-2008, 05:47 PM   #11
keefaz
Senior Member
 
Registered: Mar 2004
Distribution: Slackware
Posts: 4,535

Rep: Reputation: 119Reputation: 119
Maybe do a DNS query ?
Code:
internet()
{
   # Google
   GOOGLE_IP=$(dig google.com +short)
   if [ -z "$GOOGLE_IP" ]; then
      INTERNET=no
   else
      INTERNET=yes
   fi
}
 
Old 10-10-2008, 06:49 PM   #12
jong357
Senior Member
 
Registered: May 2003
Location: Columbus, OH
Distribution: DIYSlackware
Posts: 1,914

Original Poster
Rep: Reputation: 52
Code:
root@darkstar:~# echo $GOOGLE_IP
; <<>> DiG 9.4.2 <<>> google.com +short ;; global options:
printcmd ;; connection timed out; no servers could be reached
root@darkstar:~# echo $INTERNET
yes
That's with my cat5 yanked ofcourse. I think I'm still leaning towards:

Code:
internet()
{
   ping -c 1 www.google.com > /dev/null
   [[ "$?" -eq 0 ]] && INTERNET=yes
}
yes is all I care about so I shortened it up a bit.

What's the advantage to using dig instead of ping? Does pinging google.com still pose a problem?

You'll have to excuse me guys.... Networking has never been my forte' which is why I'm here...

Last edited by jong357; 10-10-2008 at 06:55 PM.
 
Old 10-11-2008, 03:09 AM   #13
keefaz
Senior Member
 
Registered: Mar 2004
Distribution: Slackware
Posts: 4,535

Rep: Reputation: 119Reputation: 119
You can even short it a little more
Code:
ping -c 1 www.google.com > /dev/null && INTERNET=yes
Sorry for the dig error output in $GOOGLE_IP, maybe one fix would be:
Code:
GOOGLE_IP=$(dig google.com +short)
if [[ "$GOOGLE_IP" =~ "no servers" ]] || [ -z "$GOOGLE_IP" ]; then
    INTERNET=no
else
    INTERNET=yes
fi
 
Old 10-11-2008, 05:05 AM   #14
niels.horn
Senior Member
 
Registered: Mar 2007
Location: Rio de Janeiro - Brazil
Distribution: Slackware64-current
Posts: 1,004

Rep: Reputation: 89
Checking dial-up connection

As promised, here is a snippet of some code that I use to connect my modem to my ISP: (vivozap)

Code:
MAXWAIT=20

# Call vivozap using '/etc/ppp/peers/vivozap' definitions:
echo -n "Connecting to VivoZap...                "
pppd call vivozap

# Give pppd some time to connect...
sl=0
while [ $sl -lt $MAXWAIT ] ; do
	sl=$(($sl+1)); sleep 1
	if [ ! -z "`ifconfig | grep ppp0`" ] ; then
		break;
	fi
done
if [ $sl -eq $MAXWAIT ]; then
	echo "[fail]"
	exit 1;
else
	echo "[ok]";
fi
As soon as the connection has been made, the ppp0 adapter is created.
If you use pppd, you can check if the ppp0 adapter (still) exists and, if not, jump to a script like mine that (re)connects.

The code to check if your modem is connected could be something like:
Code:
# Check connection:
if [ -z "`ifconfig | grep ppp0`" ] ; then
	INTERNET=No
else
	INTERNET=Yes
fi
Ah, I almost forgot: YMMV !

Last edited by niels.horn; 10-11-2008 at 05:06 AM.
 
Old 10-11-2008, 01:35 PM   #15
jong357
Senior Member
 
Registered: May 2003
Location: Columbus, OH
Distribution: DIYSlackware
Posts: 1,914

Original Poster
Rep: Reputation: 52
Thanks all for the suggestions. In the middle of implementing my original idea with suggestions given here to improve it. We'll see if it comes back to bite me.

Greping for every available device imaginable in ifconfig seems like a kludge of monumental proportions to me and still doesn't guarantee comm. with the outside. Admittedly, I haven't looked into it too much to begin with.
 
  


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
Checking if a TCP/UDP connection is actually local blank87 Linux - Networking 8 07-17-2008 08:44 AM
(k)Ubuntu checking Internet connection Merlyn Ubuntu 2 11-09-2006 06:33 AM
checking if a connection is closed using select(); Thinking Programming 7 12-13-2005 03:19 PM
C - checking internet connection szparag Programming 4 10-27-2004 03:07 PM
Checking if you are on the internet RedRabbit Linux - General 1 01-20-2004 08:21 AM


All times are GMT -5. The time now is 12:38 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration