vendredi 25 décembre 2015

Return value of ping [duplicate]

This question already has an answer here:

I have done a file named hosts.txt which includes some websites, to test the ping on each website with a script. What I want to do with my script is I want to loop through each line that includes different websites, and it should tell if the website is up or down (by measuring the ping command on each)

What my problem is that I don't really know how to get the return value of the ping command, so in case a website is up it should say "'website name' found" or not found. I have been researching, also tried out the ! command and different ways in the if-statement, but none of them seem to work.

My code:

#!/bin/bash
echo
echo "Monitoring hosts from file hosts.txt ..."
echo
echo
egrep -v '^(#|$)' hosts.txt | while read line; do #put the egrep value
#which is the lines in hosts.txt, and loop through each one of them

        if [ ping $line ];then
        echo "$line is up"

        else
        echo "$line is not up"
        fi

done

echo

Aucun commentaire:

Enregistrer un commentaire