Periodic monitoring connection to certain port of certain ip address

in

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

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <blockquote>
  • Lines and paragraphs break automatically.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>. Beside the tag style "<foo>" it is also possible to use "[foo]".
  • Easily link to terms in various wikis. For help, see <a href="/interwiki/1">interwiki</a>.

More information about formatting options