Question

I wrote the following script to check my servers:

#!/bin/bash

rm -f /tmp/res-ok.txt     1>/dev/null 2>/dev/null
rm -f /tmp/res-failed.txt 1>/dev/null 2>/dev/null

echo "***********************************************"
echo "*+++++++++++++++++++++++++++++++++++++++++++++*"
echo "*+                                           +*"
echo "*+      PINGING SERVERS: Is UP OR DOWN       +*"
echo "*+                                           +*"
echo "*+++++++++++++++++++++++++++++++++++++++++++++*"
echo "***********************************************"

echo " ================                 ============ "
echo "|   IP ADDRESS   |               |   STATUS   |"
echo " ================                 ============ "

while read ip; do
    ping $ip -s 1 -c 1 1>/dev/null 2>&1
    if [ $? -eq 0 ]; then
        echo "  $ip                      UP"
        echo $ip >> /tmp/res-ok.txt
    elif [ $? -ne 0 ]; then
        echo "  $ip                      DOWN"
        echo $ip >> /tmp/res-failed.txt
    fi
echo "-----------------------------------------------"

done <<____HERE

____HERE

ok=`wc -l /tmp/res-ok.txt | awk '{sum += $1} END {print sum}'`
failed=`wc -l /tmp/res-failed.txt | awk '{sum += $1} END {print sum}'`

echo You have $ok servers UP
echo You have $failed servers DOWN

how can I tell my script that show STATUS column[the second column] in the specific place?[exactly] I think I should control cursor..how? any idea? thank you

Was it helpful?

Solution

If I understand your Problem correctly, your Layout is broken, because you have IP-adresses of different lengths. Depending on how much different they are, you could get a better fitting layout by using tabulators instead of just blanks:

echo -e "  $ip\t\t\t\tDOWN"

... or by piping your output through column:

output="$output `echo -e "\n$ip UP"`"
...
## Outputting your output:
echo -e "$output" | column -t

Result:

172.16.2.4        UP
172.16.2.5        UP
192.168.178.200   UP
192.168.178.254   UP

For the latter to look perfect, you might have to adjust the width of your welcome-header to the resulting width of your output.

You could also add more blank columns in between your fields by adding a seperator:

output="$output `echo -e "\n$ip  - UP"`"

Result:

172.16.2.4       -   UP
172.16.2.5       -   UP
192.168.178.200  -   UP
192.168.178.254  -   UP
192.168.178.254  -   UP

So after those changes your script should look like this:

#!/bin/bash

rm -f /tmp/res-ok.txt     1>/dev/null 2>/dev/null
rm -f /tmp/res-failed.txt 1>/dev/null 2>/dev/null

echo "***********************************************"
echo "*+++++++++++++++++++++++++++++++++++++++++++++*"
echo "*+                                           +*"
echo "*+      PINGING SERVERS: Is UP OR DOWN       +*"
echo "*+                                           +*"
echo "*+++++++++++++++++++++++++++++++++++++++++++++*"
echo "***********************************************"

echo " =============          ============ "
echo "| IP ADDRESS  |        |   STATUS   |"
echo " =============          ============ "

output=""    ## Unset the variable, in case it still contains anything.

while read ip; do
    ping $ip -s 1 -c 1 1>/dev/null 2>&1
    if [ $? -eq 0 ]; then
        output="$output `echo "\n$ip - UP"`"
        echo $ip >> /tmp/res-ok.txt
    elif [ $? -ne 0 ]; then
        output="$output `echo "\n$ip - DOWN"`"
        echo $ip >> /tmp/res-failed.txt
    fi
echo "-----------------------------------------------"

done <<____HERE

____HERE

echo -e "$output" | column -t

ok=`wc -l /tmp/res-ok.txt | awk '{sum += $1} END {print sum}'`
failed=`wc -l /tmp/res-failed.txt | awk '{sum += $1} END {print sum}'`

echo You have $ok servers UP
echo You have $failed servers DOWN

OTHER TIPS

The final answer:

#!/bin/bash

if [ $# -ne 1 ]; then
    echo "USAGE: ./server-status.sh <ip-list>"
    exit 1
fi

rm -f /tmp/whole.txt      1>/dev/null 2>/dev/null
rm -f /tmp/res-ok.txt     1>/dev/null 2>/dev/null
rm -f /tmp/res-failed.txt 1>/dev/null 2>/dev/null

echo "***********************************************"
echo "*+++++++++++++++++++++++++++++++++++++++++++++*"
echo "*+                                           +*"
echo "*+      PINGING SERVERS: Is UP OR DOWN       +*"
echo "*+                                           +*"
echo "*+++++++++++++++++++++++++++++++++++++++++++++*"
echo "***********************************************"

echo " ================                 ============ "
echo "|   IP ADDRESS   |               |   STATUS   |"
echo " ================                 ============ "

output=""
while read ip; do
    ping $ip -s 1 -c 1 -W 1 1>/dev/null 2>&1
    if [ $? -eq 0 ]; then
    output="$output `echo "\n$ip -------------------- UP"`"
        echo $ip >> /tmp/res-ok.txt
    elif [ $? -ne 0 ]; then
    output="$output `echo "\n$ip -------------------- DOWN"`"
        echo $ip >> /tmp/res-failed.txt
    fi
done < $1

echo -e "$output" | column -t

cat /tmp/res-ok.txt >> /tmp/whole.txt
cat /tmp/res-failed.txt >> /tmp/whole.txt

total=`wc -l /tmp/whole.txt | awk '{sum += $1} END {print sum}'`
ok=`wc -l /tmp/res-ok.txt | awk '{sum += $1} END {print sum}'`
failed=`wc -l /tmp/res-failed.txt | awk '{sum += $1} END {print sum}'`

echo "UP:    $ok"
echo "DOWN:  $failed"
echo "TOTAL: $total"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top