#!/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
diff=$(($fin-$debut))
I also tried the $ SECONDS function but when it exceeds 1min , I have a result of type 0 seconds.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
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".
Open in new window