0

So i am trying to ping some URL in a list.txt and see if is valid or not code here:

url_list=./listawebmin.txt
contador=0
contadorerror=0
contadorsuccess=0

while read siteurl
do   
    echo "Se va a realizar ping a $siteurl"
    if ping -c3 $siteurl
        then
            echo "---$siteurl SUCCESS---" &>> success.txt
            let contadorsuccess=contadorsuccess+1
        else
            echo "---$siteurl ERROR---" &>> error.txt
            let contadorerror=contadorerror+1          
    fi
    let contador=contador+1
done < $url_list

echo "Total de urls : $contador"
echo "Total de urls SUCCESS: $contadorsuccess"
echo "Total de urls ERROR: $contadorerror"
echo "Servicio Terminado mira error.txt"

The thing is that when i try to pass ping a $something variable that should be for example something=www.google.com in this case, ping does not respond as intended. Saying something like name or severces unidentify (in spanish: : Nombre o servicio desconocido) So the question is how do i make it so it recognise $siteurl ? Is there any better way to verify if a web is alive?

EDIT: Example of listawebmin.txt:

www.helenarohner.com
www.100porcienmexico.es
www.24fab.com
www.masllorensestudi.com
www.3i-ingenieria.com
www.360corporate.com
www.7camiciemadrid.es
www.centrodereuniones.com
www.ab-internacional.com
www.aba-abogadas.com
www.google.com

It is a simple txt

aggarcia
  • 5
  • 6
  • Can you show an extract of listawebmin.txt? Also be careful with line endings. Make sure that there the no Windows line endings. – Raman Sailopal Mar 26 '21 at 11:38
  • @aggarcia : Please remove the _bash_ tag, since this seems to be a sh-script. – user1934428 Mar 26 '21 at 11:57
  • @user1934428 removed – aggarcia Mar 26 '21 at 11:58
  • @aggarcia : What is the intended behaviour after you get this ping error message? Do you want to try `curl` at this point, or simply go on to the next item in the list? – user1934428 Mar 26 '21 at 12:02
  • @user1934428 i just want the errors to go in a error.txt and the success to go in a sucess.txt file, i have read that ping return 0 if pinged OK meaning the website is alive and runnig – aggarcia Mar 26 '21 at 12:07
  • @aggarcia : No quite. See the [ping man page](https://linux.die.net/man/8/ping) for a description of the exit codes. As for storing the error message - why don't you catch it and store it into a file? My guess (the man page doesn't say this) that ping writes its error messages to stderr. – user1934428 Mar 26 '21 at 12:30
  • @aggarcia Do you mean checking if a website is alive? You need to request a page to check that a site is alive, ping just tells you if the machine backing the site is there, AND only if the network between you and it allow you to ping it (many corporates don't allow it). try `curl` or `wget` - you'll have to try both http:// and https:// (until you get a success). – Mr R Mar 26 '21 at 14:11
  • @MrR Perfect i am going to read exatly what those commands do, yeah objective is checking if a website is alive, was using ping because in the 'man ping' it says 'If ping does not receive any reply packets at all it will exit with code 1. If a packet count and deadline are both specified, and fewer than count packets are received by the time the deadline has arrived, it will also exit with code 1. On other error it exits with code 2. Otherwise it exits with code 0. This makes it possible to use the exit code to see if a host is alive or not.' – aggarcia Mar 29 '21 at 16:23

3 Answers3

0

Here you go try this.

#!/bin/sh

url_list=./listawebmin.txt
contador=0
contadorerror=0
contadorsuccess=0

for siteurl in $(cat $url_list)
do   
    echo "Se va a realizar ping a $siteurl"
    if ping -c3 $siteurl
        then
            echo "---$siteurl SUCCESS---" &>> success.txt
            let contadorsuccess=contadorsuccess+1
        else
            echo "---$siteurl ERROR---" &>> error.txt
            let contadorerror=contadorerror+1          
    fi
    let contador=contador+1
done

echo "Total de urls : $contador"
echo "Total de urls SUCCESS: $contadorsuccess"
echo "Total de urls ERROR: $contadorerror"
echo "Servicio Terminado mira error.txt"
KATHAN PATEL
  • 103
  • 6
  • It kind of work but for some reason just with the last URL, i posted a the url example list – aggarcia Mar 26 '21 at 12:05
  • its worked fine whats the issue? Total de urls : 11 Total de urls SUCCESS: 4 Total de urls ERROR: 7 Servicio Terminado mira error.txt ``` – KATHAN PATEL Mar 26 '21 at 12:41
  • I dont know for some reason when i ping for example www.aba-abogadas.com i get stuck and gives me error but this happend with everyone of them except if i put google.com on it – aggarcia Mar 26 '21 at 12:46
0

If these are web sites, I would recommend testing if they are listening on port 80. This would rule out any problems with ping.

You can use netcat to test if port 80 is open:

#!/bin/bash

host_list=./listawebmin.txt
contador=0
contadorerror=0
contadorsuccess=0

while read -r host; do
    if nc -zw2 "$host" 80 &>/dev/null; then
        echo "---$host SUCCESS---" >> success.txt
        (( contadorsuccess++ ))
    else
        echo "---$host ERROR---" >> error.txt
        (( contadorerror++ ))
    fi
    (( contador++ ))
done < "$host_list"

echo "Total de hosts : $contador"
echo "Total de hosts SUCCESS: $contadorsuccess"
echo "Total de hosts ERROR: $contadorerror"
echo "Servicio Terminado mira error.txt"
Cole Tierney
  • 9,571
  • 1
  • 27
  • 35
0

Thanks everyone for answering i found out that wget was the best option, thanks for the ideas!

url_list=./listaweb2.txt
contador=0
contadorerror=0
contadorsuccess=0
while read siteurl
do
    echo "Se va a realizar wget a $siteurl"
    wget --timeout=3 --tries=1 --spider $siteurl
    if [ $? -eq 0 ]; 
        then
            echo "---$siteurl is UP---" &>> ./results/success.txt
            contadorsuccess=$(expr $contadorsuccess + 1)
    else
        echo "---$siteurl is DOWN---" &>> ./results/error.txt
        contadorerror=$(expr $contadorerror + 1)
    fi
    contador=$(expr $contador + 1)
done < $url_list
echo "Total de urls : $contador" &>> ./results/resume.txt
echo "Total de urls SUCCESS: $contadorsuccess" &>> ./results/resume.txt
echo "Total de urls ERROR: $contadorerror" &>> ./results/resume.txt
echo "Total de urls UP and running: $contadorsuccess" &>> ./results/success.txt
echo "Total de urls DOWN: $contadorerror" &>> ./results/error.txt
echo "Servicio Terminado mira error.txt"

This was the final result of the script

www.helenarohner.com
www.100porcienmexico.es
www.24fab.com
www.masllorensestudi.com
www.3i-ingenieria.com
www.360corporate.com
www.7camiciemadrid.es
www.centrodereuniones.com
www.ab-internacional.com
www.aba-abogadas.com

This is a content example on the listaweb2.txt

PD: if you have a better solution to solve this problem dont doubt to post it even if this gets 10y old i might read it

aggarcia
  • 5
  • 6