Script to Monitor List of Ports

Hi,

I'm in need of a script that can monitor a list of 12 ports.. If one of the ports isn't in listening mode then email me..

The list of ports are 26401 - 26412..

netstat -an | grep -Ec . . . | read ct

Of course, to really prove they are served, you could telnet to each and see if it connects, prompts, etc., but you might alter your app connect stats at the same time -- classic Heisenberg.

DGPickett,

I can't just pull the full count of them since I need to know exactly which one isn't in "listening" mode..

You can't actively telnet into the ports, although the app listens (this is because of the function of the app). But it is safe to assume that if it's not in LISTEN mode, then it's not functioning.

Many tcp listeners can be made to respond using telnet. I am not talking about logging in, telnet is just a trivial, general tcp client with some optional behaviors. Telnet to a web server port 80 and type 'GET / HTTP/1.0'+CR+CR. If you telnet to most servers, and they do not respond, still you know they are really listening, even if you do not know the magic bytes to make them talk. For instance, if inetd is the real listener, and the app is missing/crashing, the connection may take for an instant and they be broken off.

Make a file of good netstat -an listen lines and comm them to see if any disappeared.

Yup.. I know what you mean.. however, this application doesn't allow for any type of connections through the port, since it's an internal license adapter. It just refuses connection when trying to telnet. The application cannot be modified, but I was told that 100% reliability that LISTEN means it's working (it's what I'm told from the application, so it's all I can agree to).

Here's what I've come up with:

NETSTAT=`netstat -ant | grep LISTEN | awk '{ print $4 }'`
CCTEST="my_email@email.com another_email@email.com"

for PORT in `seq 26401 26412`; do
echo "$NETSTAT" | grep $PORT &> /dev/null
if [ $? -ne 0 ]; then
echo "Port $PORT is down on `hostname`i X.X" | mail -s "Port $PORT is down on `hostname`" $CCTEST
else
echo "Port $PORT is =)"
fi
done

Maybe it expects real fast input, or a specific client port or host, or tickles telnet to reveal itself as not the app.

Yeah, that looks pretty good.

  • Time stamps in email and logs are nice, think date "+%Y-%m-%d %H:%M:%S . . ." in place of echo.
  • I keep the host left on subjects so I detect testing vs production more quickly.
  • You can tee the mail message to /dev/stderr on some UNIX like Solaris, so it also goes into the log.
  • You might want to accumulate a $BAD_LIST and send one mail for all if any.

I added some time stamps.. although the version of AIX I'm on didn't like date "+%Y-%m-%d %H:%M:%S" just to re mediate the problem.. I assigned DATE to the first subset, and TIME to the second.

I've thought of doing a $BAD_LIST .. but this is a temporary thing, and they believe only 1 goes down.. I'll def do that.. if they want (I can incorporate it now, and just change the mail line to include $BAD_LIST instead of $PORT.. ).. but meetings all day..

It's on AIX 5.2, which I'm not good with (I'm a linux guy).. so was a little different for me..