whats wrong with my internet connection checker script?

2010-12-26 Thread S Mathias
$ true && true || echo hi
$ true && false || echo hi
hi
$ false && true || echo hi
hi
$ false && false || echo hi
hi
$ ping -W 1 -c 4 google.com >& /dev/null | grep -q "100% packet loss" && ping 
-W 1 -c 4 www.yahoo.com >& /dev/null | grep -q "100% packet loss" || echo "no 
internet connection"
no internet connection
$
ping -W 1 -c 4 google.com >& /dev/null | grep "100% packet loss"
$
ping -W 1 -c 4 www.yahoo.com >& /dev/null | grep "100% packet loss"
$



...both sides "false", because they have no output, because google.com and 
www.yahoo.com is reachable.
how come it writes "no internet connection"? [at the longest line]
i just want a "oneliner" that checks if theres "internet connection" or no. :\

where did i screw up? 8)


  


-- 
To UNSUBSCRIBE, email to debian-user-requ...@lists.debian.org 
with a subject of "unsubscribe". Trouble? Contact listmas...@lists.debian.org
Archive: http://lists.debian.org/471466.28989...@web121403.mail.ne1.yahoo.com



whats wrong with my internet connection checker script?

2010-12-26 Thread S Mathias
perfect! thank you Oliver Grawert! :) made my day :) it's working! :


ping -W 1 -c 4 google.com >& /dev/null && ping -W 1 -c 4 www.yahoo.com >& 
/dev/null || echo "no internet connection"



  


-- 
To UNSUBSCRIBE, email to debian-user-requ...@lists.debian.org 
with a subject of "unsubscribe". Trouble? Contact listmas...@lists.debian.org
Archive: http://lists.debian.org/913669.63663...@web121402.mail.ne1.yahoo.com



Re: whats wrong with my internet connection checker script?

2010-12-26 Thread Volkan YAZICI
On Sun, 26 Dec 2010, S Mathias  writes:
> $ ping -W 1 -c 4 google.com >& /dev/null | grep -q "100% packet loss" && ping 
> -W 1 -c 4 www.yahoo.com >& /dev/null | grep -q "100% packet loss" || echo "no 
> internet connection"
> no internet connection
>
> ...both sides "false", because they have no output, because google.com and 
> www.yahoo.com is reachable.
> how come it writes "no internet connection"? [at the longest line]
> i just want a "oneliner" that checks if theres "internet connection" or no. :\
>
> where did i screw up? 8)

Since both returns false, you should echo "yes", not "no".


Cheers.


-- 
To UNSUBSCRIBE, email to debian-user-requ...@lists.debian.org 
with a subject of "unsubscribe". Trouble? Contact listmas...@lists.debian.org
Archive: http://lists.debian.org/87ei94jh0o@alamut.ozu.edu.tr



Re: whats wrong with my internet connection checker script?

2010-12-26 Thread ceduard0
2010/12/26 S Mathias :
> $ true && true || echo hi
> $ true && false || echo hi
> hi
> $ false && true || echo hi
> hi
> $ false && false || echo hi
> hi
Hi I was  trying  with this logic of the internet connection checker script.
If $ true && true || echo hi   <<< All right

if  $ true && false || echo hi
or $ false && true || echo hi
or $ false && false || echo hi   the connection is broken

This my implementation of the checker script:
ping -W 1 -c 4  google.com && ping -W 1 -c 4  yahoo.com || echo "The
connection is broken"

This when the Internet connection is broken:
cedua...@madmax:~/workspace/ngl/Debug$ ping -W 1 -c 4  google.com &&
ping -W 1 -c 4  yahoo.com || echo "broken"
ping: unknown host google.com
broken
cedua...@madmax:~/workspace/ngl/Debug$ ping -W 1 -c 4  google.com &&
ping -W 1 -c 4  yahoo.com || echo "broken"
ping: unknown host google.com
broken
cedua...@madmax:~/workspace/ngl/Debug$ ping -W 1 -c 4  google.com &&
ping -W 1 -c 4  yahoo.com || echo "broken"
ping: unknown host google.com
broken
cedua...@madmax:~/workspace/ngl/Debug$ ping -W 1 -c 4  google.com &&
ping -W 1 -c 4  yahoo.com || echo "broken"
ping: unknown host google.com
broken

This when the Internet connection are right
cedua...@madmax:~/workspace/ngl/Debug$ ping -W 1 -c 4  google.com &&
ping -W 1 -c 4  yahoo.com || echo "broken"
PING google.com (74.125.65.104) 56(84) bytes of data.
64 bytes from gx-in-f104.1e100.net (74.125.65.104): icmp_req=1 ttl=55
time=91.0 ms
64 bytes from gx-in-f104.1e100.net (74.125.65.104): icmp_req=2 ttl=55
time=81.5 ms
64 bytes from gx-in-f104.1e100.net (74.125.65.104): icmp_req=3 ttl=55
time=85.8 ms
64 bytes from gx-in-f104.1e100.net (74.125.65.104): icmp_req=4 ttl=55
time=80.0 ms

--- google.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3004ms
rtt min/avg/max/mdev = 80.048/84.627/91.077/4.303 ms
PING yahoo.com (72.30.2.43) 56(84) bytes of data.
64 bytes from ir1.fp.vip.sk1.yahoo.com (72.30.2.43): icmp_req=1 ttl=53
time=151 ms
64 bytes from ir1.fp.vip.sk1.yahoo.com (72.30.2.43): icmp_req=2 ttl=53
time=148 ms
64 bytes from ir1.fp.vip.sk1.yahoo.com (72.30.2.43): icmp_req=3 ttl=53
time=145 ms
64 bytes from ir1.fp.vip.sk1.yahoo.com (72.30.2.43): icmp_req=4 ttl=53
time=140 ms

--- yahoo.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3001ms
rtt min/avg/max/mdev = 140.337/146.438/151.636/4.140 ms


> $ ping -W 1 -c 4 google.com >& /dev/null | grep -q "100% packet loss" && ping 
> -W 1 -c 4 www.yahoo.com >& /dev/null | grep -q "100% packet loss" || echo "no 
> internet connection"
> no internet connection
> $
> ping -W 1 -c 4 google.com >& /dev/null | grep "100% packet loss"
> $
> ping -W 1 -c 4 www.yahoo.com >& /dev/null | grep "100% packet loss"
> $
>
>
>
> ...both sides "false", because they have no output, because google.com and 
> www.yahoo.com is reachable.
> how come it writes "no internet connection"? [at the longest line]
> i just want a "oneliner" that checks if theres "internet connection" or no. :\
>
> where did i screw up? 8)
>
>
>
>
>
> --
> To UNSUBSCRIBE, email to debian-user-requ...@lists.debian.org
> with a subject of "unsubscribe". Trouble? Contact listmas...@lists.debian.org
> Archive: http://lists.debian.org/471466.28989...@web121403.mail.ne1.yahoo.com
>
>



-- 
ceduard0


-- 
To UNSUBSCRIBE, email to debian-user-requ...@lists.debian.org 
with a subject of "unsubscribe". Trouble? Contact listmas...@lists.debian.org
Archive: 
http://lists.debian.org/aanlktimow1s5wmkyyol6is-zvf443ddls1xvkkews...@mail.gmail.com



Re: whats wrong with my internet connection checker script?

2010-12-26 Thread Chris Davies
S Mathias  wrote:
> ping -W 1 -c 4 google.com >& /dev/null | grep "100% packet loss"
> ping -W 1 -c 4 www.yahoo.com >& /dev/null | grep "100% packet loss"

> both sides "false", because they have no output, because google.com
> and www.yahoo.com is reachable.

Or because your local system is so offline that it's got no way of
resolving those names to IP addresses.

I'd recommend you turn your pattern match around to look for any sort
of success, rather than one specific instance of failure.

Chris


-- 
To UNSUBSCRIBE, email to debian-user-requ...@lists.debian.org 
with a subject of "unsubscribe". Trouble? Contact listmas...@lists.debian.org
Archive: http://lists.debian.org/mc1lu7xmlr@news.roaima.co.uk



Re: whats wrong with my internet connection checker script?

2010-12-26 Thread Bob Proulx
S Mathias wrote:
> $ true && true || echo hi

Both true commands invoked.  This satisfied the left hand side of the
or and so echo was not invoked.

> $ true && false || echo hi
> hi

Both true and false invoked, and then the echo.

> $ false && true || echo hi
> hi

Only the first false invoked, and then the echo.

> $ false && false || echo hi
> hi

Only the first false invoked, and then the echo.

> $ ping -W 1 -c 4 google.com >& /dev/null | grep -q "100% packet loss" && ping 
> -W 1 -c 4 www.yahoo.com >& /dev/null | grep -q "100% packet loss" || echo "no 
> internet connection"
> no internet connection

Only the first ping|grep invoked.  This returned false.  The grep did
not match and therefore did not return success.  The grep returned a
failure that it did not match.  I think that is not what you expected.
Since the network connection was up and online the grep 100% failed
and returned false.

This is exactly the same as you had tested above:

> $ false && false || echo hi
> hi

Exactly the same and so exactly the same result.

> $ ping -W 1 -c 4 google.com >& /dev/null | grep "100% packet loss"

  $ echo $?
  1

> $ ping -W 1 -c 4 www.yahoo.com >& /dev/null | grep "100% packet loss"
> $

  $ echo $?
  1

> ...both sides "false", because they have no output, because google.com and 
> www.yahoo.com is reachable.
> how come it writes "no internet connection"? [at the longest line]

The first test is false and so the OR side with the echo is invoked.

Note that the second ping|grep is not invoked.  Only the first one.
Being false the OR side is invoked next.

> i just want a "oneliner" that checks if theres "internet connection" or no. :\

You would need to invert the grep exit code.  But not using grep as
you found in your later post is a better solution.

> where did I screw up? 8)

You used ">& /dev/null" to redirect both stdout and stderr to
/dev/null.  This means that there isn't any input to grep and
therefore grep can never succeed with a match.

Side Note: You are using ">& word" which is not typical.  The manual
states that using "&> word" is preferred over ">& word".  I believe
this to reduce confusion over the "2>&1" syntax.  When in doubt follow
the documentation.

Personally I never use that bash specific syntax and stick with the
standard ">/dev/null 2>&1" syntax.  And if the standard syntax had
been used in this case I think it would have made the issue obvious at
a glance.

Bob


signature.asc
Description: Digital signature


Re: whats wrong with my internet connection checker script?

2010-12-26 Thread Bob Proulx
S Mathias wrote:
> perfect! thank you Oliver Grawert! :) made my day :) it's working! :
> 
> ping -W 1 -c 4 google.com >& /dev/null && ping -W 1 -c 4 www.yahoo.com >& 
> /dev/null || echo "no internet connection"

The ping documentation says:

   -c count
  Stop after sending count ECHO_REQUEST packets.  With
  deadline option, ping waits for count ECHO_REPLY
  packets, until the timeout expires.

   -w deadline
  Specify a timeout, in seconds, before ping exits
  regardless of how many packets have been sent or
  received.  In this case ping does not stop after count
  packet are sent, it waits either for deadline expire or
  until count probes are answered or for some error
  notification from network.

   [...]
   If ping does not receive any reply packets at all it will exit
   with code 1.  If a packet count and deadline are both specified,
   and fewer than count packets are received by the time the
   deadline has arrived, it will also exit with code 1.  On other
   error it exits with code 2.   Otherwise it exits with code
   0.  This makes it possible to use the exit code to see if a host
   is alive or not.

Therefore I think the correct way to use the return code of ping is to
specify both -c and -w options.  I don't think you need the -W option.

  $ ping -c 4 -w 4 google.com &> /dev/null && ping -c 4 -w 4 www.yahoo.com &> 
/dev/null || echo "no internet connection"

Bob


signature.asc
Description: Digital signature


Re: whats wrong with my internet connection checker script?

2010-12-27 Thread Joe
On Sun, 26 Dec 2010 23:57:42 +
Chris Davies  wrote:

> S Mathias  wrote:
> > ping -W 1 -c 4 google.com >& /dev/null | grep "100% packet loss"
> > ping -W 1 -c 4 www.yahoo.com >& /dev/null | grep "100% packet loss"
> 
> > both sides "false", because they have no output, because google.com
> > and www.yahoo.com is reachable.
> 
> Or because your local system is so offline that it's got no way of
> resolving those names to IP addresses.
> 
> I'd recommend you turn your pattern match around to look for any sort
> of success, rather than one specific instance of failure.
> 

I would recommend going further than that: I've often seen a router
pass pings and replies by DNS but not web pages or some other protocols.
My current scripts check for specific strings from up to six websites,
rotating the order each time so one site doesn't get too much traffic.
There is a log kept which I check every month or two to confirm the
sites still contain the strings.

It may seem a bit overengineered, but I made progressively more and more
complex scripts until they worked reliably and without false positives,
and I'm afraid this was the point I reached. They haven't changed for
some years now, nor has the number increased beyond six.

Here's a fragment from script rw1:

logfile="/var/log/routerboot"
echo -e -n `date`  >> $logfile
echo -n " -rw1- "  >> $logfile
count=0
count=$((count+`curl -s www.google.com | grep -c content-type`))
echo -n $count >> $logfile
if [ $count -eq 0 ]
then
  count=$((count+`curl -s www.google.co.uk | grep -c content-type`))
  echo -n $count >> $logfile
  if [ $count -eq 0 ]
  then
.. etc, culminating in router reboot code ...

-- 
Joe


-- 
To UNSUBSCRIBE, email to debian-user-requ...@lists.debian.org 
with a subject of "unsubscribe". Trouble? Contact listmas...@lists.debian.org
Archive: http://lists.debian.org/20101227125405.1a46b...@jresid.jretrading.com



Re: whats wrong with my internet connection checker script?

2010-12-27 Thread Camaleón
On Sun, 26 Dec 2010 14:30:36 -0800, S Mathias wrote:

(...)

> ping -W 1 -c 4 google.com >& /dev/null | grep "100% packet loss" $
> ping -W 1 -c 4 www.yahoo.com >& /dev/null | grep "100% packet loss" $
> 
> 
> 
> ...both sides "false", because they have no output, because google.com
> and www.yahoo.com is reachable. how come it writes "no internet
> connection"? [at the longest line] i just want a "oneliner" that checks
> if theres "internet connection" or no. :\
> 
> where did i screw up? 8)

Just a side note. 

A "ping" test is not a proof that your connection is "alive". There can 
be situations (outside your scope) in that your connection goes through a 
firewall or router that is configured to reject pings and your test will 
fail but that is not a sympthom that your DSL link is down.

Whether possible, you better monitor your dsl router and query about its 
connection status (there are some models that allow sending internal logs 
to your syslog and you can then make an alert -i.e., send an e-mail- when 
DSL status goes down. Another devices can trigger these alerts -system/
link down- automatically).

Greetings,

-- 
Camaleón


-- 
To UNSUBSCRIBE, email to debian-user-requ...@lists.debian.org 
with a subject of "unsubscribe". Trouble? Contact listmas...@lists.debian.org
Archive: http://lists.debian.org/pan.2010.12.27.16.13...@gmail.com



Re: whats wrong with my internet connection checker script?

2010-12-27 Thread shawn wilson
On Dec 27, 2010 7:54 AM, "Joe"  wrote:
>
> On Sun, 26 Dec 2010 23:57:42 +
> Chris Davies  wrote:
>
> > S Mathias  wrote:
> > > ping -W 1 -c 4 google.com >& /dev/null | grep "100% packet loss"
> > > ping -W 1 -c 4 www.yahoo.com >& /dev/null | grep "100% packet loss"
> >
> > > both sides "false", because they have no output, because google.com
> > > and www.yahoo.com is reachable.
> >
> > Or because your local system is so offline that it's got no way of
> > resolving those names to IP addresses.
> >
> > I'd recommend you turn your pattern match around to look for any sort
> > of success, rather than one specific instance of failure.
> >
>
> I would recommend going further than that: I've often seen a router
> pass pings and replies by DNS but not web pages or some other protocols.
> My current scripts check for specific strings from up to six websites,
> rotating the order each time so one site doesn't get too much traffic.
> There is a log kept which I check every month or two to confirm the
> sites still contain the strings.
>
> It may seem a bit overengineered, but I made progressively more and more
> complex scripts until they worked reliably and without false positives,
> and I'm afraid this was the point I reached. They haven't changed for
> some years now, nor has the number increased beyond six.
>
> Here's a fragment from script rw1:
>
> logfile="/var/log/routerboot"
> echo -e -n `date`  >> $logfile
> echo -n " -rw1- "  >> $logfile
> count=0
> count=$((count+`curl -s www.google.com | grep -c content-type`))
> echo -n $count >> $logfile
> if [ $count -eq 0 ]
> then
>  count=$((count+`curl -s www.google.co.uk | grep -c content-type`))
>  echo -n $count >> $logfile
>  if [ $count -eq 0 ]
>  then
> .. etc, culminating in router reboot code ...

First, your correct that ping tests aren't the end all be all and if you
can't access the web, you've got issues one way or the other. However, if
you're going to go as far as to start testing different protocols on a line,
you might as well base your script on scapy framework and call it a day
(complete low level control of the build up and breakdown of a protocol).
Also, you can ask nmap to do a full handshake.


Re: Re: whats wrong with my internet connection checker script?

2010-12-27 Thread Jasper Noë

$ ping -c 4 -w 4 google.com &> /dev/null && ping -c 4 -w 4 www.yahoo.com &> /dev/null || 
echo "no internet connection"


But, but, ... if the ping to google succeeds, and the ping to yahoo 
fails, it prints "no internet".


$ ping -c 4 -w 4 google.com &> /dev/null || ping -c 4 -w 4 www.yahoo.com 
&> /dev/null || echo "no internet connection"


Better ?

--Jasper Noë


--
To UNSUBSCRIBE, email to debian-user-requ...@lists.debian.org 
with a subject of "unsubscribe". Trouble? Contact listmas...@lists.debian.org

Archive: http://lists.debian.org/4d18b1a2.5000...@xs4all.nl



Re: Re: whats wrong with my internet connection checker script?

2010-12-27 Thread Bob Proulx
Jasper Noë wrote:
> >$ ping -c 4 -w 4 google.com &> /dev/null && ping -c 4 -w 4 www.yahoo.com &> 
> >/dev/null || echo "no internet connection"
> 
> But, but, ... if the ping to google succeeds, and the ping to yahoo
> fails, it prints "no internet".
> 
> $ ping -c 4 -w 4 google.com &> /dev/null || ping -c 4 -w 4 www.yahoo.com &> 
> /dev/null || echo "no internet connection"

I think the intent of S Mathias's original was that both sites needed
to be up and online or a failure would be reported.  I think the
intent was that if either one or the other didn't work then report a
failure.  Because of the included four test cases that showed all four
of the possibilities.

Your test here changes that behavior.  As to whether this is a good
idea to do it one way or the other way is a different question.  As to
whether it should be the other way around with a success being if
either ping succeeds that I don't know.  I am not sure what this test
is being used for.  But it is a different test.

Bob


signature.asc
Description: Digital signature