JRA Posted July 6, 2016 Posted July 6, 2016 (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 July 6, 2016 by JRA
Davit2005 Posted July 6, 2016 Posted July 6, 2016 Isn't there a PowerShell for ping is it Test-connection and then do a action on it i.e. email??
JRA Posted July 6, 2016 Author Posted July 6, 2016 Hi - thanks for that. On Ubuntu though. Looks like I didn't tick that Ubuntu box wotsit right.
Davit2005 Posted July 6, 2016 Posted July 6, 2016 (edited) Jus realised this was on ubuntu Edited July 6, 2016 by Davit2005 Reader error 1
unixman_again Posted July 6, 2016 Posted July 6, 2016 Maybe add a -q option to the ping command? (Don't have my Linux box here today, so I can't test it.) 1
jinnantonnixx Posted July 6, 2016 Posted July 6, 2016 (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 July 6, 2016 by jinnantonnixx 1
JRA Posted July 6, 2016 Author Posted July 6, 2016 Thanks everyone! Jinnantonnixx got it, that works beautiful!
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now