0

Im trying to get all ip address from file called ipserver.txt and check if the ip address is down or up. In the file ipserver.txt I have ip address that looks like this:

# My text file ipserver.txt
host1 10.0.0.1
host2 192.168.10.23
host3 192.168.0.1
host4 192.168.23.10

# My script
date
cat ipserver.txt | while read output
do
    ping -c 1 "$output" > /dev/null

    if [ $? -eq 0 ]
    then
            # echo -e "\n\033[4;31mHOSTNAME\033[0m  \t\t\033[1;4;31mIP\033[0m" 
            echo "$output is up"    
            # printf $output >> resultat 
    else
            echo "$output is down"
    fi
done

# cat resultat

What Im trying to output is a table of all ouputs like this:

HOSTNAME    IP                  UP/DOWN
--------------------------------------------
host1       10.0.0.1            UP
host2       192.168.10.23       DOWN
host3       192.168.0.1         UP
host4       192.168.23.10       DOWN
--------------------------------------------

In my script Im trying to redirect the $output to resultat, but dont now if Im thinking right. In text file ipserver.txt I can check the ip address from the file if I dont write host1, host2, host3 and host4. Can somebody give me a tip on how to resolve my script, so I can get the output that i want

Bruno Alves
  • 51
  • 1
  • 1
  • 7

2 Answers2

1

This will do what you want:

#!/bin/bash
printf "HOSTNAME\tIP\t\t\tUP/DOWN\n"
echo "----------------------------------------------------------"

while read -r host ip
  do ping -c 1 "$ip" > /dev/null 2>&1 && printf "$host\t\t$ip\t\tUP\n" || printf "$host\t\t$ip\t\tDOWN\n"
done < ipserver.txt

echo "----------------------------------------------------------"

Probably best to put that in a script. If you want to send the results to a file, redirect the entire output. i.e. if you called it test-servers and made it executable:

./test-servers >> resultat
arco444
  • 22,002
  • 12
  • 63
  • 67
  • Hi! Nice code, but the problem is that now the output always show that the ip address i s DOWN, when some of the ip address is supposed to be UP – Bruno Alves Jan 13 '15 at 13:35
0

I think you're looking for printf, does the following work for you?

date
i=1
cat ipserver.txt | while read output
do
    extr=${output#"host$i"}
    ping -c 1 "$extr" > /dev/null

    if [ $? -eq 0 ]
    then
        printf "%-16s %-16s %s\n" "host$i" $extr "UP"
    else
        printf "%-16s %-16s %s\n" "host$i" $extr "DOWN"
    fi
    ((i++))
done
buydadip
  • 8,890
  • 22
  • 79
  • 154
  • Hi! Nice code, but the problem is that now the output always show that the ip address i s UP, when some of the ip address is supposed to be DOWN – Bruno Alves Jan 13 '15 at 18:38
  • @V.V.T how about now? I edited it. I think the problem was the position of `${output#"host$i"}` – buydadip Jan 13 '15 at 20:45
  • Now the output is always DOWN – Bruno Alves Jan 13 '15 at 21:44
  • Now the output is always DOWN – Bruno Alves Jan 13 '15 at 22:15
  • 1
    in that case, are you sure the problem is not with the `ping`? – buydadip Jan 13 '15 at 22:15
  • @V.V.T this is my last attempt, I think the error might be in the ping, also I changed `$ip` because it might get mixed up with `$i` – buydadip Jan 13 '15 at 22:18
  • Ok, now Its working. But I had to change the text file ipserver.txt to only contain IP address, and not like I wanted host1 and the IP address. I also added: red='\033[0;31m' green='\033[0;32m' NC='\033[0m' printf "HOSTNAME\tIP\t\t\t${green}UP${NC}/${red}DOWN${NC}\n" echo "----------------------------------------------------------" – Bruno Alves Jan 13 '15 at 23:26