Ping trace telnet monitoring script

Hello, it is my first post :slight_smile:
I need to implement some monitoring tools in a script, ping telnet traceroute

I found this one to ping, it works,

#!/bin/sh
for i in `cat /tmp/PingStatus.txt`
do
  ping -c 5 -w 5 -n $i | grep -q "bytes from" && echo "$i is OK"  || opcmsg object=PING a=OS msg_grp=OpC severity=critical msg_text="PING  BROKEN on $line"
done

but I would also need to check traceroute and telnet on different ports 80,21,9000... towards different servers,routers. How can I implement it ?

thanks a lot!

Telnet:

echo "b" | telnet -e "b" $i $port

You might want to consider wrapping some code round it to allow a shorter timeout than the default (something like 2 minutes from memory?).
Rough code to give you the idea:

timeout=15  #seconds
echo "b" | telnet -e "b" $i $port &
pid=$!
count=0
while /usr/ucb/ps -aux $pid > /dev/null && [ "$count" -lt "$timeout" ]
do
  sleep 1
  count=`expr $count + 1`
done

Sorry too difficult to me, need something simple right now :frowning:

ping -c 5 -w 5 -n 192.168.1.1 | grep -q "bytes from" && echo "Gateway , Ping is OK"
traceroute 10.22.1.6 | grep -q "10.22.1.6" && echo "PROXY, Traceroute is OK"
telnet 10.22.1.77 80 | grep -q "Connected" && echo "WEB por is OK"

---------- Post updated at 09:31 AM ---------- Previous update was at 08:55 AM ----------

Can you please give me something to start with? I've been reading some shell script tutorial but I'm even not sure which way to monitor those services (using if, while..)

---------- Post updated at 10:17 AM ---------- Previous update was at 09:31 AM ----------

#!/bin/bash
WEB="www.google.com"

ping -c 5 -w 5 -n ${WEB} | if [ grep -q "bytes from" ]
then
   echo ${WEB} " is up"
else
   echo ${WEB} " is DOWN"
fi

how can I save the condition if [ grep -q "bytes from" ] in a variable?

Well the telnet one I gave above (the single line) should work as-is:

echo "b" | telnet -e "b" $i $port

Where $i is you target host/ip and $port is the port number you need to try.

The echo "b" and the -e "b" parts are telling telnet to immediately quit once it gets a connection. This means you can use your above logic with it:

echo "b" | telnet -e "b" $i $port && echo "${i}:${port} is OK"

For your last question with ping and the if statement, I'm not actually sure what you are trying to do to be honest,but perhaps you want something like this?

#!/bin/sh
# You don't need bash for something this simple, sh is just fine and works everywhere
WEB="www.google.com"

if ping -c 5 -w 5 -n ${WEB} | grep -q "bytes from"
then
  echo ${WEB} " is up"
else
  echo ${WEB} " is DOWN"
fi
1 Like

thanks for your reply,

WEB="www.google.it"
GW="192.168.1.1"
PORT="80"


if ping -c 5 -w 5 -n ${GW} | grep -q "bytes from"
then
  echo ${GW} " is OK"
else
  echo ${GW} " is DOWN"
fi

if telnet -e "b" ${WEB} ${PORT} | grep -q "connected to"
then
  echo ${WEB}:${PORT} " is OK"
else
  echo ${WEB}:${PORT} " is DOWN"
fi
marmellata@marmellata-desktop:~/testping$ sh test.sh 
192.168.1.1  is OK
..
..
..

it stops on telnet command

Kind regards

---------- Post updated at 08:52 AM ---------- Previous update was at 08:43 AM ----------

I've been also trying to implement traceroute command so that if I can reach PROXY I get an answer or if I dont reach PROXY but can rach PROXY1 I get another answer. Doesnt work, how can I do it ?

PROXY="10.99.4.1"
PROXY1="10.40.4.1"

if traceroute ${PROXY} | grep -q "10.99.4.1"
then
  echo ${PROXY} " is OK"
elif grep -q "10.40.4.1" !"10.99.4.1"
then
  echo ${PROXY} " is DOWN but "${PROXY1} " is UP"  

You've dropped the echo "b" part of the telnet line, it's important as you've set the escape char to b with the -e "b" flag, but you still need to send it to telnet for it to do anything.