Ping show alive or dead in Linux(script)
If I remember correctly , in Solaris , ping will return “alive” or “dead” by default. But in linux no such feature/option anymore , we must using script or other tools to do this ,below is a simple bash script to show remote host or ip address is “alive” or “dead”
1.Create a new file with your favorite editor like vim /gvim /nano, here we use gvim and name the script as ping.sh
gvim ping.sh
2.Put below code into this file and save it
#!/bin/bash if ping -c 1 -W 1 "$1" &>/dev/null; then echo "$1 is alive" else echo "$1 is dead" fi
“&” lets ping to run in background , “>/dev/null” suppress some outputs of ping.Note: there is no space between “&” and “>”
3.Add execution access for this file
chmod +x ping.sh
4.Now you can use script ping.sh to check remote host is alive or dead
[j@localhost ~]$ cat hosts.txt 192.168.50.1 www.google.com 192.168.50.2 www.yutuble.com www.yahoo.com [j@localhost ~]$ for host in `cat hosts.txt`;do /home/j/ping.sh $host;done 192.168.50.1 is alive www.google.com is dead 192.168.50.2 is dead www.yutuble.com is dead www.yahoo.com is alive [j@localhost ~]$