0

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
Tomb_Raider_Legend
  • 431
  • 13
  • 29
  • That duplicate is accurate but perhaps there is a better duplicate? Both http://stackoverflow.com/questions/90418/exit-shell-script-based-on-process-exit-code and http://stackoverflow.com/questions/9261397/bash-get-process-id-and-exit-code seem a bit closer to this question. And if there was to be a canonical answer, I'd vote for this one: http://stackoverflow.com/questions/5163144/what-are-the-special-dollar-sign-shell-variables – Paul Hicks Dec 26 '15 at 01:43

2 Answers2

1

You need to use the $? special variable.

For example:

ping $line
pingResponse = $?

if [ $pingResponse -eq 0 ];then
    echo "$line is up"
else
    echo "$line is not up"
fi
Paul Hicks
  • 13,289
  • 5
  • 51
  • 78
  • since if I type ping $line in the loop, it of course loops through the lines first, do you have any idea how to stop it from looping and go straight into the if-statements? I tried ping -c 1 $line and it works but it shows the ping together with the text "line is up", but I only want the text to show up. I also tried ping -c 0 $line but it did not work. – Tomb_Raider_Legend Dec 26 '15 at 01:28
  • Just put the `ping` *and* the `if` within the loop. – Paul Hicks Dec 26 '15 at 01:32
  • I do already have them in the loop (If I haven't misunderstood) . 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 ping $line pingResponse=$? #sets the return value to pingRespons if [ $true ];then echo "$line --> is up" yet it runs the ping $line first which it shouldn't do. – Tomb_Raider_Legend Dec 26 '15 at 02:13
  • Why wouldn't it ping first? If you don't do that, then `$pingResponse` will have the value of the ping to the previous value of `$line`. I think the current logic is correct. If it's not, then you'll have to explain your problem in a new question, I think. – Paul Hicks Dec 26 '15 at 04:36
0

you can test for boolean like:

[[ `ping  $line` ]]

.

   #!/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
repzero
  • 8,254
  • 2
  • 18
  • 40
  • However, this doesn't work on all the websites I have in my hosts.txt . It shows "line is up" on them all. Migh be something wrong with the while-loop – Tomb_Raider_Legend Dec 26 '15 at 02:37
  • I tried it with a couple of incorrect sites like "www.67yuoi.com" and works as expected.......hmmm..... – repzero Dec 26 '15 at 02:43
  • My mistake, I used ' instead of ` in the statement, however now when I tried it like that, the output just stops after showing the first line "Monitoring hosts from file hosts.txt ..." . I started with bash scripting today therefore I am not that experienced but trying my best to solve problems. – Tomb_Raider_Legend Dec 26 '15 at 03:00
  • I would advice if you use ping -c 5 where -c represents the number of times to ping..if you don't specify this option it will keep pinging a website infinitely and you instead of exiting so that you can determine if it was successfull..... – repzero Dec 26 '15 at 03:06
  • I tried with -c 1 and it works, however for those websites that are not existing it also prints an error message with it "ping: unknown host www.67yuoi.com ". But I think the code is fine since it does it's job. One last thing, I appreciate if you could explain about how the boolean works by editing your first comment or answering this comment. Thank you! – Tomb_Raider_Legend Dec 26 '15 at 14:52