Script for testing ping and telnet

Hello,
Here is a script for testing the ping and telnet based on a file containing a list of servers.
I am looking to improve this script by sending the result like this:
Hostname;IPAddr;PingTest;TelnetTest
Srv1;192.168.2.1;OK;OK
Srv2;192.168.2.2;OK;NOK
Srv3;192.168.2.3;NOK;NOK

Thank you for help!
Host-PortFile.txt
TestTelnet.sh
NirickNetwork EngineerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

woolmilkporcCommented:
Hi,

I tried to leave most parts of your script as they were. I removed the old ouput messages, and I had to turn the two loops into one and thus two functions into one.

I used the "host" command to get the hostname from its IP. The output of this command is different between OSes, so if you get errors or undesired output please post the result of "host xxx.xxx.xxx.xxx".

#!/bin/bash
#bash check ping et telnet OOB.
#set -xv;
#
#clear
SetParam() {
export SRVFILE="Host_PortFile.txt"
export TIME=`date +%d-%m-%Y_%H.%M.%S`
export port=23
export SHELL_LOG="`basename $0`.log"
}

Check_Hosts() {

SetParam
cat $SRVFILE | while read next
do

server=`echo $next | cut -d : -f1`
port=`echo $next | awk -F":" '{print $2}'`
servername=$(host $server 2>/dev/null | awk '{sub("\\.$","",$NF); print $NF}')

OUTPUT="$servername;$server"
ping -i 2 -c 6 $server > /dev/null 2>&1

if [ $? -eq 0 ] ; then
OUTPUT=${OUTPUT}";OK"
else
OUTPUT=${OUTPUT}";NOK"

fi

TELNETCOUNT=`sleep 5 | telnet $server $port 2>/dev/null | grep -v "Connection refused" | grep "Connected to" | grep -v grep | wc -l `

if [ $TELNETCOUNT -eq 1 ] ; then

OUTPUT=${OUTPUT}";OK"
else
OUTPUT=${OUTPUT}";NOK"

fi
echo "$OUTPUT"
done;
}
Main() {
Check_Hosts
}
SetParam
Main | tee -a $SHELL_LOG

Open in new window

0
NirickNetwork EngineerAuthor Commented:
Hi,

Thanks for your response!
The script works, for the name resolution I added getent hosts :

servername=$(getent hosts $server 2>/dev/null | awk '{sub("\\.$","",$NF); print $NF}')

Thanks again!
0
woolmilkporcCommented:
sub("\\.$","",$NF)  in the awk code is meant to remove the trailing dot shown by "hosts".

In order to remove everything after the first dot (i.e. strip the whole domain suffix) use

sub("\\..*$","",$NF)
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

gheistCommented:
nmap network scanner does same and much more.
0
NirickNetwork EngineerAuthor Commented:
Hello,

There is the final script :

#!/bin/bash
#bash check ping et telnet OOB.
#set -xv;
#
#clear
debut=`date +%S`

SetParam() {
export SRVFILE="Host_PortFile2.txt"
export TIME=`date +%d-%m-%Y_%H.%M.%S`
export port=23
export SHELL_LOG="`basename $0`.log"
}

Check_Hosts() {

SetParam
cat $SRVFILE | while read next
do

server=`echo $next | cut -d : -f1`
port=`echo $next | awk -F":" '{print $2}'`
servername=$(getent hosts $server 2>/dev/null | awk '{sub("\\.$","",$NF); print $NF}')

OUTPUT="$servername;$server"
ping -i 3 -c 8 $server > /dev/null 2>&1

if [ $? -eq 0 ] ; then
OUTPUT=${OUTPUT}";\E[32mUP\E[0m"
else
OUTPUT=${OUTPUT}";\E[31mDOWN\E[0m"

fi

TELNETCOUNT=`sleep 5 | telnet $server $port 2>/dev/null | grep -v "Connection refused" | grep "Connected to" | grep -v grep | wc -l `

if [ $TELNETCOUNT -eq 1 ] ; then

OUTPUT=${OUTPUT}";\E[32mUP\E[0m"
else
OUTPUT=${OUTPUT}";\E[31mDOWN\E[0m"

fi
echo -e "$OUTPUT"
done;


fin=`date +%S`
diff=$(($fin-$debut))
 

echo "Test fini en $diff secondes."
}


Main() {
Check_Hosts
}
SetParam
Main | tee -a $SHELL_LOG

Open in new window


I try to calculate the time it takes to run the script by calculating the difference between start time and end time :
diff=$(($fin-$debut))

Open in new window

I also tried the $ SECONDS function but when it exceeds 1min , I have a result of type 0 seconds.

Another question, is it possible to calculate the hosts that are OK ? For example have a result like this: 13 hosts are UP , DOWN 5 hosts are .

Thanks for help !
0
woolmilkporcCommented:
You should base the calculation of the elapsed time on "seconds since epoch" which can be obtained with "date +%s" (lowercase s).

debut=`date +%s`
...
fin=`date +%s`

Next, on which result should "hosts that are OK" be based? Ping or Telnet?
I added a calculation of both (actually four) values.
I had to use a "for" loop instead of a "while" loop to make the counters available on the outside (ugly old bash blur, that's why I prefer using ksh).

By the way, why did you specify such lengthy intervals and sleep times? The script could run a lot faster with e. g.  "ping -i 1 -c  2 ..." and "sleep 1 | telnet ..."!
I implemented these changes to speed up my testing. You can always return to the old values, of course, they're still there in the comment lines 33 and 45.

Here you go:
#!/bin/bash
#bash check ping et telnet OOB.
#set -xv;
#
#clear
debut=`date +%s`
PINGOK=0
PINGFAIL=0
TELNETOK=0
TELNETFAIL=0

SetParam() {
export SRVFILE="Host_PortFile2.txt"
export TIME=`date +%d-%m-%Y_%H.%M.%S`
export port=23
export SHELL_LOG="`basename $0`.log"
}

Check_Hosts() {

SetParam

for next in $(<$SRVFILE)

do

server=`echo $next | cut -d : -f1`
port=`echo $next | awk -F":" '{print $2}'`
servername=$(getent hosts $server 2>/dev/null | awk '{sub("\\.$","",$NF); print $NF}')


OUTPUT="$servername;$server"
#ping -i 3 -c 8 $server > /dev/null 2>&1
ping -i 1 -c 2 $server > /dev/null 2>&1

if [ $? -eq 0 ] ; then
OUTPUT=${OUTPUT}";\E[32mUP\E[0m"
((PINGOK+=1))
else
OUTPUT=${OUTPUT}";\E[31mDOWN\E[0m"
((PINGFAIL+=1))

fi

#TELNETCOUNT=`sleep 5 | telnet $server $port 2>/dev/null | grep -v "Connection refused" | grep "Connected to" | grep -v grep | wc -l `
TELNETCOUNT=`sleep 1 | telnet $server $port 2>/dev/null | grep -v "Connection refused" | grep "Connected to" | grep -v grep | wc -l `

if [ $TELNETCOUNT -eq 1 ] ; then

OUTPUT=${OUTPUT}";\E[32mUP\E[0m"
((TELNETOK+=1))
else
OUTPUT=${OUTPUT}";\E[31mDOWN\E[0m"
((TELNETFAIL+=1))

fi

echo -e "$OUTPUT"
done;


fin=`date +%s`
diff=$(($fin-$debut))


echo "Test fini en  $diff secondes."
echo "Ping réussi   $PINGOK fois."
echo "Ping échoué   $PINGFAIL fois."
echo "Telnet réussi $TELNETOK fois."
echo "Telnet échoué $TELNETFAIL fois."
}


Main() {
Check_Hosts
}
SetParam
Main | tee -a $SHELL_LOG

Open in new window

Please see lines 6-10,23,38,41,51,54,62,67-70 for the changes.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
NirickNetwork EngineerAuthor Commented:
Hello,

Thanks again. The needed result is telnet, so used the last line only ;
echo "$TELNETFAIL hosts OOB down."

The hosts are dialer interfaces. The ping is used only to force up these interfaces.
When ping counter / interval are low, some dialer interfaces stay down, so telnet test fail.

PING 192.168.2.70 (192.168.2.70) 56(84) bytes of data.
64 bytes from 192.168.2.70: icmp_req=6 ttl=252 time=46.2 ms
64 bytes from 192.168.2.70: icmp_req=7 ttl=252 time=46.0 ms
64 bytes from 192.168.2.70: icmp_req=8 ttl=252 time=46.2 ms
64 bytes from 192.168.2.70: icmp_req=9 ttl=252 time=46.1 ms
64 bytes from 192.168.2.70: icmp_req=10 ttl=252 time=46.0 ms
^C
--- 192.168.2.70 ping statistics ---
11 packets transmitted, 5 received, 54% packet loss, time 9998ms
rtt min/avg/max/mdev = 46.047/46.168/46.297/0.217 ms

Open in new window


See packet loss.
I didnt found any other way than putting high counters / interval
0
woolmilkporcCommented:
OK,

if your interfaces behave as described raising counters (and possibly intervals) seems a viable solution.

Besides that, does the script work as expected?
0
NirickNetwork EngineerAuthor Commented:
Hello,

The script works I've implemented in prod env this morning.
Thanks for help !
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Linux

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.