I'm doing some ping tests.
What does ping -c 1 mean? What can use this command for? Why would I find it in a bash script?
1 Answer
That command means to ping something 1 time (1 count). Without the -c, it will keep pinging the IP address until you tell it to stop. In scripting, I like to do a count of at least 2 in case the first ping fails.
You can use it to check if an IP address is live and then check for the results.
terrance@terrance-ubuntu:~$ ping -c 1 10.0.0.254 PING 10.0.0.254 (10.0.0.254) 56(84) bytes of data. From 10.0.0.100 icmp_seq=1 Destination Host Unreachable --- 10.0.0.254 ping statistics --- 1 packets transmitted, 0 received, +1 errors, 100% packet loss, time 0ms terrance@terrance-ubuntu:~$ echo $? 1 The 1 above means it was not able to ping that address.
terrance@terrance-ubuntu:~$ ping -c 1 10.0.0.253 PING 10.0.0.253 (10.0.0.253) 56(84) bytes of data. 64 bytes from 10.0.0.253: icmp_seq=1 ttl=255 time=0.584 ms --- 10.0.0.253 ping statistics --- 1 packets transmitted, 1 received, 0% packet loss, time 0ms rtt min/avg/max/mdev = 0.584/0.584/0.584/0.000 ms terrance@terrance-ubuntu:~$ echo $? 0 The 0 above means the command exited without errors.
So in a script, you can use it like so:
#!/bin/bash ping -c 2 10.0.0.253 case $? in 0) echo "The host is alive.";; 1) echo "The host is not responding.";; esac Hope this helps!
0