Jump to content

Recommended Posts

Posted (edited)

Hello all, this one's doing me in a bit.

 

Looking to redirect the output of ping command. Knocked up a little line of bash that'll ping a website and, if it can do so, show one message and if not show another:

 

ping -c1 www.crashplan.com && echo "Current connectivity to CrashPlan: OK" || "BACKUP CONNECTIVITY FAILURE - check network cable, lights on network card/s, router and all switching are on and working." | tail -n1

 

But despite the tail it spits out the following:

 

PING www.crashplan.com (216.17.8.19) 56(84) bytes of data.64 bytes from central.crashplan.com (216.17.8.19): icmp_seq=1 ttl=53 time=113 ms


--- www.crashplan.com ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 113.238/113.238/113.238/0.000 ms
Current connectivity to CrashPlan: OK

 

All I want as output is that final line that indicates success or not (which tail should be doing I'd think.)

 

Anyone know what to do?

 

Thanks for any pointers. :)

Edited by JRA
Posted
Hi - thanks for that. On Ubuntu though. :) Looks like I didn't tick that Ubuntu box wotsit right.
Posted (edited)

Try this. It's a bit more robust and the output is clean.

Supresses StdIO output and tests the result of the ping in a CASE construct. Looks for 100% packet loss and 0% packet loss to determine the result.

 

case `ping -qnc 1 www.crashplan.com 2>&1` in
   *'100% packet loss'*) echo "BACKUP CONNECTIVITY FAILURE - check network cable, lights on network card/s, router and all switching are on and working."
   exit 1
   ;;
   *' 0% packet loss'*) echo "Current connectivity to CrashPlan: OK"
   exit 1
   ;;
   *) echo "Problem with script"    # should never see this.
   ;;
   esac

Edited by jinnantonnixx
  • Thanks 1

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...