Sunday, November 1, 2020

Conditional Loop - For Loop

The following is the for loop to get ping result from host 192.168.56.101 to 192.168.56.104.

#!/bin/bash
touch ~/Documents/bash_script/ping_result
for i in {104..101}
do
    ping 192.168.56.$i >>~/Documents/bash_script/ping_result && echo 192.168.56.$i is up
done

Important notes:
If the remote host is pingable, the echo $? will return 0.
If the remote host is not pingable, the echo $? will return 1.




This script will generate a new file "ping_result" and all the ping result will be stored in this file.









Let's find out why only the pingable host will result in echo 192.168.56.$i is up.


No comments:

Post a Comment