Linux Training

Linux training for private, public & voluntary sector.

0800 024 8425

City LinUX sample scripts - portmon


portmon - monitors the availability of a port on a remote host.


portmon [ -c <config_file> ] [ -d ] [ -l ] [ -r <rcpt_email_addr> ] [ -v ] < clientid >| -V


portmon is a Bourne shell script which should work equally well on all versions of UNIX, Linux and Mac OS X.


portmon uses nmap to check that a specified port is open on a remote host.

NB . A short client id is normally given in preference to a host name on the command line. This is used to identify the configuration stanza in the configuration file which will have the required hostname or IP address. If no configuration file is found or if the client id is not found in the configuration file portmon will assume that clientid=hostname .

The script is designed to run silently in the background simply logging when a port is noticed to be down and when recovery is noted. When the script is terminated with SIGHUP or SIGTERM a trap ensures that the end of the monitoring session is also logged.


-c    Use the specified configuration file in preference to the default "/usr/local/etc/" . If no configuration file is found the script will attempt to soldier on with the information it has from the command line.

-l    Use the local syslog facility to record the date and time that the monitoring starts and ends. If the port goes down the time and date that port is first noted to be down is recorded. If the port becomes available again this is also logged.

-r <email_address> Send alerts to the specified user address. This will be used in preference to the recipient specified in the configuration file and is useful when testing the script. If no recipient is specified either on the command line nor in the configuration file portmon will send alerts to the invoking user's local mailbox.

NB. This functionality is not yet available.

-d    Start in debug mode. The -x    option is set within the shell.

-v    Set verbose mode. Ordinarily portmon operates silently unless show stopping problems are detected. In verbose mode portmon reports starting, stopping and port availability to screen.

-V    Print the portmon version and exit.


portmon -l -v merlin &

Check that the port specified in the default configuration file, "/usr/local/" , is available on the host merlin. Whenever it is noted that the port goes down or recovers this is also logged using the local syslog facility.

Verbose mode will be used causing portmon to report the port's availability to standard error.

portmon &

The script will check the configuration file for the client id "". If no entry is found, as seems likely, "" will be assumed to be a hostname and nmap will be run with the " -Pn " option. By default port 80 will be specified.


The script is quite crude having very little error checking. The consequences of unexpected input are untested. Where multiple ports are checked if any are open this is reported as success. This needs dealing with urgently.


chkdf, chkftpd, chkfw, clean, secscan.


Clifford W Fulford, City Linux. Contact or +44 (0)709 229 5385.

The layout and associated style sheets for this page are taken from the World Wide Web Consortium and used here under the W3C software licence.