Verify if Internet connection is duly active? [SOLVED]

For discussions about programming, programming questions/advice, and projects that don't really have anything to do with Puppy.
Post Reply
Message
Author
User avatar
Argolance
Posts: 3767
Joined: Sun 06 Jan 2008, 22:57
Location: PORT-BRILLET (Mayenne - France)
Contact:

Verify if Internet connection is duly active? [SOLVED]

#1 Post by Argolance »

Hello,
Before executing a script, I need a (simple) command line to know if and be absolutely sure that user's Internet connexion is actually active...
Thank you.

Cordialement.
Last edited by Argolance on Tue 05 Mar 2013, 15:35, edited 1 time in total.

User avatar
L18L
Posts: 3479
Joined: Sat 19 Jun 2010, 18:56
Location: www.eussenheim.de/

Verify if Internet connection is duly active?

#2 Post by L18L »

Code: Select all

 [ "`ifconfig | head -n 1| awk '{print $1}'`" = "lo" ] && echo no || echo oui
:wink:

lo is local loopback
if this is at first place then no other connection exists

---
edited: forget it, take vovchik's
Last edited by L18L on Tue 05 Mar 2013, 15:56, edited 1 time in total.

User avatar
Argolance
Posts: 3767
Joined: Sun 06 Jan 2008, 22:57
Location: PORT-BRILLET (Mayenne - France)
Contact:

#3 Post by Argolance »

Thanks you! :D

User avatar
vovchik
Posts: 1507
Joined: Tue 24 Oct 2006, 00:02
Location: Ukraine

#4 Post by vovchik »

Dear L18L,

That isn't true in my case:

Code: Select all

root$ [~]-> ifconfig            
lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX packets:976 errors:0 dropped:0 overruns:0 frame:0
          TX packets:976 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:71960 (70.2 KiB)  TX bytes:71960 (70.2 KiB)

wlan0     Link encap:Ethernet  HWaddr D8:F8:BA:D8:F8:BA  
          inet addr:192.168.0.16  Bcast:192.168.0.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:93463 errors:0 dropped:0 overruns:0 frame:0
          TX packets:98017 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:53415279 (50.9 MiB)  TX bytes:12026226 (11.4 MiB)
This seems to work for me:

Code: Select all

#!/usr/bin/bash

# Test for network conection
for interface in $(ls /sys/class/net/ | grep -v lo);
do
	if [[ "$(cat /sys/class/net/$interface/operstate)" == "up" ]]; then
		echo "Online: $interface"
		OnLine=1
	fi
done
if ! [ $OnLine ]; then echo "Not Online" > /dev/stderr; exit; fi
or

There are also some nice methods using netstat -punt or netstat -natu and lsof -i.

With kind regards,
vovchik

User avatar
L18L
Posts: 3479
Joined: Sat 19 Jun 2010, 18:56
Location: www.eussenheim.de/

#5 Post by L18L »

@Argolance,
I am sorry
fast solutions are not always right solutions :lol:

@vovchik,
your script works for me too :)
Last edited by L18L on Tue 05 Mar 2013, 16:11, edited 1 time in total.

User avatar
Argolance
Posts: 3767
Joined: Sun 06 Jan 2008, 22:57
Location: PORT-BRILLET (Mayenne - France)
Contact:

#6 Post by Argolance »

@L18L
Your code line worked for me!
I am sorry
Don't!
Thank you to both of you.

Cordialement.

User avatar
L18L
Posts: 3479
Joined: Sat 19 Jun 2010, 18:56
Location: www.eussenheim.de/

Verify if Internet connection is duly active?[otherSOLUTION]

#7 Post by L18L »

Code: Select all

[ $(grep up /sys/class/net/*/operstate) ] && echo online
other values in operstate:
-down
-unknown (for lo)

User avatar
vovchik
Posts: 1507
Joined: Tue 24 Oct 2006, 00:02
Location: Ukraine

#8 Post by vovchik »

Dear L18L,

I like your last one - works nicely and is a one-liner:)

With kind regards,
vovchik

User avatar
Argolance
Posts: 3767
Joined: Sun 06 Jan 2008, 22:57
Location: PORT-BRILLET (Mayenne - France)
Contact:

#9 Post by Argolance »

I like your last one - works nicely and is a one-liner
Yes indeed!

User avatar
Argolance
Posts: 3767
Joined: Sun 06 Jan 2008, 22:57
Location: PORT-BRILLET (Mayenne - France)
Contact:

#10 Post by Argolance »

Hello,
While running a fresh install of Puppy on a friend's PC, I succeeded in setting the wifi Internet connection and was able to go on the Internet with the web browser... But the command used in one of my scripts:

Code: Select all

if [ $(grep down /sys/class/net/*/operstate) ]; then
Xdialog --title "$(eval_gettext '$app Installation') - $(gettext 'Information')" --icon /usr/local/lib/X11/pixmaps/info.png --msgbox "$(eval_gettext 'Your internet connection seems not to be active.
Please, set it up before downloading/installing $app!')" 0 0
else
[...]
... that lets user download applications from a remote server, said the connexion was not active... :shock: :oops:
So it seems this command is not absolutely sure: what I am obviously expecting for!

Is there any other command (using ping :roll: ?) doing this "properly"?

Cordialement.

User avatar
Argolance
Posts: 3767
Joined: Sun 06 Jan 2008, 22:57
Location: PORT-BRILLET (Mayenne - France)
Contact:

#11 Post by Argolance »

Hello,
Found this which works fine:

Code: Select all

#!/bin/sh

curl -D- -o /dev/null -s http://xxxxx.net
if [[ $? == 0 ]]; then
  echo "yes"
else
  echo "no"
fi
Cordialement!

User avatar
L18L
Posts: 3479
Joined: Sat 19 Jun 2010, 18:56
Location: www.eussenheim.de/

#12 Post by L18L »

Found this:

Code: Select all

PING="`ping -c 1 xxxxx.net `"
 [ "${PING:0:4}" = "PING"  ] && echo online
Not every server responds on pings

User avatar
Argolance
Posts: 3767
Joined: Sun 06 Jan 2008, 22:57
Location: PORT-BRILLET (Mayenne - France)
Contact:

#13 Post by Argolance »

OK, thanks!

Post Reply