Ping Response from the host name

Hi All,

I have the requirement where am pinging the server and matching the IP address with the existing IP address. Below code is returning me the IP address and my requirement is i have to see that also whether it is pinging or not

PING useipapd01 (172.22.32.87) 56(84) bytes of data.
64 bytes from useipapd01 (172.22.32.87): icmp_seq=1 ttl=64 time=0.028 ms
64 bytes from useipapd01 (172.22.32.87): icmp_seq=1 ttl=64 time=0.028 ms

below code is giving me the IP address of the first line. But i want to get the second line also which will tell me whether it is pinging the server or not.

ip1=$(ping -q -c 1 -t 1 "$host" |grep PING | grep -Eo '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}')

I want to check the second line which should tell me whether it is pinging or not

64 bytes from hostname 

You could try something like:

if ping -c 1 -t 1 10.0.1.13 >/dev/null; then
  echo yes
else
  echo no
fi

Which would be using the return code of the ping command to check whether it was successful or not. The ping command line options are not the same on every platform, so if that does not work, could you specify what OS and version you are using?

1 Like