Showing posts with label exit. Show all posts
Showing posts with label exit. Show all posts

Tuesday, 11 February 2020

host connectivity check using Linux scripts using exit code


cat connect_check.sh
#!/bin/bash
HOST="google.com"
ping -c 4 $HOST
if [ "$?" -eq "1" ]                        #exit code test condition
then
echo " $HOST is reachable "
else
echo " $HOST unreachable "
fi




Output

./connect_check.sh
PING google.com (172.217.17.78) 56(84) bytes of data.
From 10.188.34.2 icmp_seq=1 Destination Host Unreachable
From 10.188.34.2 icmp_seq=2 Destination Host Unreachable
From 10.188.34.2 icmp_seq=3 Destination Host Unreachable
From 10.188.34.2 icmp_seq=4 Destination Host Unreachable

--- google.com ping statistics ---
4 packets transmitted, 0 received, +4 errors, 100% packet loss, time 2998ms

 google.com is reachable