Periodic monitoring connection to certain port of certain ip address
While running a kannel server, I need to make sure certain connections to telcos are up. And here is a simple technique taiyal from ##linux suggested me. It is pretty simple though.
$ netcat -z -w 2 192.168.10.1 5020 -v -n
Netcat is a very simple utility command to connect to a certain ip at certain port. The parameters are as follows.
-z zero-I/O mode [used for scanning]
-w 2 timeout for connects and final net reads
in our case try to connect for just 2 seconds
-v verbose [use twice to be more verbose]
-n numeric-only IP addresses, no DNS
No need for dns resolution
Add this to a cron and redirect the output to log file and we're set.
Here is my crontab
$ crontab -e
# m h dom mon dow command
*/5 * * * * /home/monitor/bin/checksmsc.sh
And here is my checksmsc.sh code
$ cat /home/monitor/bin/checksmsc.sh
#! /bin/bash
datetime=`date`
log=`netcat -z -w 2 192.168.10.1 5020 -vn 2>&1`
echo $datetime $log >> /home/monitor/log/connection.log
Make sure you make the log folder
$ mkdir /home/monitor/log
Output is in the format as
Sat Jan 30 14:25:02 NPT 2010 (UNKNOWN) [192.80.10.1] 5020 (?) open
Besides netcat, you could also use nmap command
$ nmap 192.168.10.1 -p T:5020 -oG filename.txt
Where -oG specifies output in Greppable format and writes out to filename.txt
Simple tcp monitor :)


Comments
Post new comment