Saturday, September 6, 2014

[Quick Tips] : Linux bash script that pings multiple IP addresses from a file

Linux bash script that pings multiple IP addresses from a file

#!/bin/bash
IPLIST="path_to_the_Ip_list_file"


for ip in $(cat $IPLIST)

do
    ping $ip -c 1 -t 1 &> /dev/null
    if [ $? -ne 0 ]; then

        echo $ip ping faild;

        else

        echo $ip ping passed;

    fi

done
Alternate Method: 
How to write a script to ping the servers and store output in some file.It has to take input from some file called IPADDR.txt and do the ping of all IP addresses one by one and should store the output in one file.

My source file has format like this
Name Ipaddress
ABCD x.x.x.x
EFGH x.x.x.x
IJKL x.x.x.x
.... ....... 
.... .......

It should ping each and every IP mentioned in the destination file which is in above format.
cat IPADDR.txt|xargs ping

or:

for i in $(cat IPADDR.txt); do
ping ${i}


Alternate Method: 
The following script is used to ping multiple hosts.
#!/bin/bash
for i in 192.168.0.{1..10}
do
   ping -c 1 -t 1 "$i" >/dev/null 2>&1 &&
   echo "Ping Status of $i : Success" ||
   echo "Ping Status of $i : Failed"
done
Suppose, if we have more host names in the text file, then we can use the below script.
1
2
3
4
5
6
while read hostname
do
ping -c 1 -t 1 "$hostname" > /dev/null 2>&1 && 
echo "Ping Status of $hostname : Success" || 
echo "Ping Status of $hostname : Failed" 
done < host.txt
done.......................

No comments: