Register
It is currently Fri Nov 21, 2014 7:48 pm

Simple ping


All times are UTC - 6 hours


Post new topic Reply to topic  [ 19 posts ] 
Author Message
 PostPosted: Thu Jan 17, 2013 7:25 am   

Joined: Fri May 16, 2008 4:58 am
Posts: 94
I want to be able to ping a host x amount of times and if the loss is over 1% i'd like to run another command so far I have

Code:
ping -c 10 google.com | grep 'loss' | awk -F',' '{ print $3}'


so i'd like
if count > "1"
then
echo "loss"


Top
 Profile  
 PostPosted: Thu Jan 17, 2013 8:30 am   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 567
hi,

Code:
ping -c 10 google.com | awk -F',' '/loss/{sub("%.*","",$3); n+=$3}END{if(n)print loss}'
`grep | awk` makes useless use of grep.


Top
 Profile  
 PostPosted: Thu Jan 17, 2013 9:00 am   

Joined: Fri May 16, 2008 4:58 am
Posts: 94
thanks I tried using a known non working ip instead of a host

Code:
ping -c 4 11.11.11.11 | awk -F',' '/loss/{sub("%.*","",$3); n+=$3}END{if(n)print loss}'


and it just returned a blank line instead of a result.

In my original code I took out the grep

Code:
ping -c 4 google.com | awk -F',' '{ print $3}'


put it returned the result over multiple line rather than a single with grep ie

ping -c 4 google.com | awk -F',' '{ print $3}'







0% packet loss

Each line

instead of
0% packet loss

Would it possible to have it if 0% then echo OK if 1%> then echo NOT OK $AMOUNT loss

I will have a few commands to do like the above so the idea is

COMMAND | grep syntax then echo "OK"
if not
then
echo "NOT OK"


Top
 Profile  
 PostPosted: Thu Jan 17, 2013 10:19 am   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 567
yep, my bad, I forgot to quote loss which is not a variable :(

try this
Code:
ping -c4 11.11.11.11 | awk -F',' '/errors/{split($4,a,"%"); if(a[1])print $4}END{if(!a[1]) print "OK"}'


Top
 Profile  
 PostPosted: Thu Jan 17, 2013 11:08 am   

Joined: Fri May 16, 2008 4:58 am
Posts: 94
that returns OK even though it's a non working ip


Top
 Profile  
 PostPosted: Thu Jan 17, 2013 1:36 pm   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 567
of course, sometimes there is no error
Code:
ping -c4 11.11.11.11 | awk -F',' '{for(i=1;i<=NF;i++){ if($i ~ "loss"){split($i,a,"%"); if(a[1])print $i}}}END{if(!a[1]) print "OK"}'


Top
 Profile  
 PostPosted: Fri Jan 18, 2013 3:28 am   

Joined: Fri May 16, 2008 4:58 am
Posts: 94
awesome thanks

I'm doing a simple services checker and here's what I have so far

Code:
#!/bin/bash

FILEDATE=`date +%d%m%y`
DIR="/tmp/"

clear

{

echo -n "Enter name: "
read tname
echo -n "Enter location: "
read location
echo -n "Enter filename: "
read file

echo ""
echo "Starting"
echo ""

#ping 8.8.8.8 -c 4 | grep -q '100%' && echo -e "Ping test FAILED" ||
ping -c4 8.8.8.8 | awk -F',' '{for(i=1;i<=NF;i++){ if($i ~ "loss"){split($i,a,"%"); if(a[1])print $i}}}END{if(!a[1]) print "PING PASSED"}'

#echo -e "Ping test PASSED"

ping google.com -c 4 > /dev/null 2>&1
echo -e "DNS PASSED"

echo "Results: "$DIR$file

echo ""
} | tee $DIR$file


The results file isn't written when I did manage to get it to work (I forgot how) it also had

enter name: enter location: enter filename:

at the top of it which i'd like to ommit.

Also i'd like the ping results to show as either

Ping test PASSED or
Ping TEST failed x%loss

also doing a speed test via wget

Code:
wget -O /dev/null http://speedtest.tele2.net/100MB.zip 2>&1 | grep '\([0-9.]\+ [KM]B/s\)'


shows no output as desired and returns

2013-01-18 20:40:22 (6.91 MB/s) - `/dev/null' saved [104857600/104857600]

on completion. How do I pipe out evey thing apart from 6.81 MB/s


Top
 Profile  
 PostPosted: Sun Jan 20, 2013 1:10 pm   

Joined: Fri May 16, 2008 4:58 am
Posts: 94
can anyone help with this? would be much appreciated.


Top
 Profile  
 PostPosted: Sun Jan 20, 2013 4:33 pm   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 567
Quote:
The results file isn't written when I did manage to get it to work (I forgot how) it also had

enter name: enter location: enter filename:

at the top of it which i'd like to ommit.
simply putting them out of the curly braces should do the trick.
Quote:
Also i'd like the ping results to show as either

Ping test PASSED or
Ping TEST failed x%loss
simply change awk's print statement.

Quote:
[grep] shows no output as desired
add an --only-matching option to grep.


Top
 Profile  
 PostPosted: Mon Jan 21, 2013 3:02 am   

Joined: Fri May 16, 2008 4:58 am
Posts: 94
Watael wrote:
The results file isn't written when I did manage to get it to work (I forgot how) it also had

enter name: enter location: enter filename:

at the top of it which i'd like to ommit.

simply putting them out of the curly braces should do the trick.


alas no it still showed up even after removing the ""

Quote:
Also i'd like the ping results to show as either

Ping test PASSED or
Ping TEST failed x%loss

simply change awk's print statement.


thanks will look into this

Quote:
[grep] shows no output as desired

add an --only-matching option to grep.


sorry what I meant was no out put is displayed while doing the download which is how I want it however i'd like the result to display 6.81 MB/s


Top
 Profile  
 PostPosted: Mon Jan 21, 2013 5:31 am   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 567
Quote:
alas no it still showed up even after removing the ""
curly braces are { }, not ""
:-/
get the echoes out of them.

Quote:
no out put is displayed while doing the download which is how I want it however i'd like the result to display 6.81 MB/s
you want to see wget's progress bar, is that it? and keep grep's output in a variable ?


Top
 Profile  
 PostPosted: Mon Jan 21, 2013 5:38 am   

Joined: Fri May 16, 2008 4:58 am
Posts: 94
Watael wrote:
alas no it still showed up even after removing the ""
curly braces are { }, not ""
:-/
get the echoes out of them.


sorry monday morning blues sorted now,

Quote:
no out put is displayed while doing the download which is how I want it however i'd like the result to display 6.81 MB/s

you want to see wget's progress bar, is that it? and keep grep's output in a variable ?


It's ok sorted it

Code:
wget -O /dev/null http://speedtest.tele2.net/100MB.zip 2>&1 | grep --only-matching '\([0-9.]\+ [KM]B/s\)'


how do I tweak it so if it's over 5.00 MB/s it'll echo pass and if under it'll echo fail?


Top
 Profile  
 PostPosted: Mon Jan 21, 2013 6:55 am   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 567
Quote:
how do I tweak it so if it's over 5.00 MB/s it'll echo pass and if under it'll echo fail?
it's more or less the same thing than for ping.


Top
 Profile  
 PostPosted: Mon Jan 21, 2013 7:14 am   

Joined: Fri May 16, 2008 4:58 am
Posts: 94
ok i'll have a go at deciphering and see where I get, with the ping one

Code:
ping -c4 11.11.11.11 | awk -F',' '{for(i=1;i<=NF;i++){ if($i ~ "loss"){split($i,a,"%"); if(a[1])print $i}}}END{if(!a[1]) print "Ping                    OK"}'


if it works it now outputs

Ping OK

on fail how do I make it output

Ping FAIL xx% LOSS


Top
 Profile  
 PostPosted: Mon Jan 21, 2013 8:29 am   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 567
Code:
ping -c4 11.11.11.11 | awk -F',' '{for(i=1;i<=NF;i++){ if($i ~ "loss"){split($i,a,"%"); print "Ping FAIL "$i}}}END{if(!a[1]) print "OK"}'


Top
 Profile  
 PostPosted: Mon Jan 21, 2013 8:49 am   

Joined: Fri May 16, 2008 4:58 am
Posts: 94
VERY close just returns fail on 0% loss

# ping -c4 11.11.11.11 | awk -F',' '{for(i=1;i<=NF;i++){ if($i ~ "loss"){split($i,a,"%"); print "Ping FAIL "$i}}}END{if(!a[1]) print "OK"}'
Ping FAIL 100% packet loss

# ping -c4 8.8.8.8 | awk -F',' '{for(i=1;i<=NF;i++){ if($i ~ "loss"){split($i,a,"%"); print "Ping FAIL "$i}}}END{if(!a[1]) print "OK"}'
Ping FAIL 0% packet loss
OK


Top
 Profile  
 PostPosted: Mon Jan 21, 2013 9:40 am   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 567
don't you have a brain to think, eyes to watch
and a keyboard to adapt to your needs ?

are you going to keep on copy/paste with your mouse everything I write?


Top
 Profile  
 PostPosted: Wed Jan 23, 2013 7:19 am   

Joined: Fri May 16, 2008 4:58 am
Posts: 94
I do apologise, I have spent literally hours trying to get it to work but to no avail.


Top
 Profile  
 PostPosted: Wed Jan 23, 2013 2:18 pm   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 567
Code:
$ ping -c4 11.11.11.11 | awk -F',' '{for(i=1;i<=NF;i++){ if($i ~ "loss"){split($i,a,"%"); if(a[1])print "Ping FAIL "$i}}}END{if(!a[1]) print "OK"}'
Ping FAIL  100% packet loss
$ ping -c4 8.8.8.8 | awk -F',' '{for(i=1;i<=NF;i++){ if($i ~ "loss"){split($i,a,"%"); if(a[1])print "Ping FAIL "$i}}}END{if(!a[1]) print "OK"}'
OK


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 19 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 6 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP