Check a network listener port to see if it is running, log an error if it does not accept connections.
#!/usr/bin/ksh93.att
#################################################################
#### !!! Download the latest version of ksh93 to run this script.
#################################################################
function usagemsg_ckport_k93 {
print -u 2 -- "
Program: ckport_k93
Determine if a network listener is running on a specified
port or service name.
Usage: ${1##*/} [-?] [-vV] [-n nodeName] [-p portNumber]
[-t type] [-c class] [-l logFile]
Where:
-n nodeName = Node name of LISTEN'er to test
-p portNumber = Network port number to test for LISTEN'er
-t type = Log Message type: ERROR, WARN (Default:ERROR)
-c class = Log Message class: O, S, U (Default:O)
-l logFile = Log file name (Default:/var/log/ckport.log)
-a tcpudp = IP Protocol type: TCP or UDP (Default:tcp)
-v = Verbose Mode
-V = Very Verbose Mode
Author: Dana French (dfrench@mtxia.com)
Copyright 2009 by Dana French: All Rights Reserved
\"AutoContent\" enabled
"
}
################################################################
####
#### Description:
####
#### Assumptions:
####
#### Dependencies:
####
#### This function requires a recent version of ksh93. The version
#### shipped with AIX is INSUFFICIENT to run this script. Download
#### the latest ksh93 AIX binary from http://www.kornshell.com
####
#### If you download the latest ksh93 AIX binary, place it on your
#### system at the location /usr/bin/ksh93.att. Do not overwrite
#### the existing /usr/bin/ksh93. Then modify the shebang line
#### to use #!/usr/bin/ksh93.att
####
#### Products:
####
#### This function records error messages to a default or
#### user specified log file. The return code from this
#### function can be used to indicate success or failure to
#### resolve the user specified hostname.
####
#### Configured Usage:
####
#### This function may be executed from the command line or
#### included in a script function library.
####
#### Details:
####
################################################################
function ckport_k93 {
typeset VERSION="1.0"
typeset TRUE="0"
typeset FALSE="1"
typeset VERBOSE="${FALSE}"
typeset VERYVERB="${FALSE}"
typeset PORTNUM=""
typeset NODENAME=""
typeset TORF=""
typeset -l UORT="tcp"
typeset DATETIME=$( date +"%m/%d/%Y %H:%M:%S" )
typeset SYSTEM="$( hostname )"
typeset TYPE="${TYPE:-ERROR}"
typeset CLASS="${CLASS:-O}"
typeset SOURCE
typeset DESCRIPTION
typeset MSG
typeset LOGFILE="/var/log/ckport.log"
while getopts ":vVp#n:t:c:l:a:" OPTION
do
case "${OPTION}" in
'p') PORTNUM="${OPTARG}";;
'n') NODENAME="${OPTARG}";;
't') TYPE="${OPTARG}";;
'c') CLASS="${OPTARG}";;
'l') LOGFILE="${OPTARG}";;
'a') UORT="${OPTARG}";;
'v') VERBOSE="${TRUE}";;
'V') VERYVERB="${TRUE}";;
[?:#]) usagemsg_ckport_k93 "${0}" && return 1 ;;
esac
done
shift $(( ${OPTIND} - 1 ))
(( VERYVERB == TRUE )) && set -x
(( VERBOSE == TRUE )) && print -u 2 -- "# Version................................: ${VERSION}"
################################################################
trap "usagemsg_ckport_k93 ${0}" EXIT
if [[ "_${NODENAME}" == "_" ]] || [[ "_${PORTNUM}" == "_" ]]
then
(( VERBOSE == TRUE )) && print -u 2 -- "# ERROR: Both the port number and node name must be specified"
return 1
fi
if ! [[ "_${TYPE}" == _@(ERROR|WARN) ]]
then
(( VERBOSE == TRUE )) && print -u 2 -- "# ERROR: Invalid log message type"
return 1
fi
if ! [[ "_${CLASS}" == _@(O|S|U) ]]
then
(( VERBOSE == TRUE )) && print -u 2 -- "# ERROR: Invalid log message class"
return 1
fi
if ! [[ -d "${LOGFILE%/*}" ]]
then
(( VERBOSE == TRUE )) && print -u 2 -- "# ERROR: Invalid log file directory: ${LOGFILE%/*}"
return 1
fi
if ! [[ "_${UORT}" == _@(tcp|udp) ]]
then
(( VERBOSE == TRUE )) && print -u 2 -- "# ERROR: Invalid IP protocol type"
return 1
fi
trap "-" EXIT
################################################################
####
#### If verbose mode was specified by the user on the command
#### line, output the configuration information to the
#### standard error stream STDERR.
[[ "_${PORTNUM}" == "_" ]] && TORF="Not Specified" || TORF="${PORTNUM}"
(( VERBOSE == TRUE )) && print -u 2 -- "# Network Port Number....................: ${TORF}"
[[ "_${NODENAME}" == "_" ]] && TORF="Not Specified" || TORF="${NODENAME}"
(( VERBOSE == TRUE )) && print -u 2 -- "# Network Service Name...................: ${TORF}"
(( VERBOSE == TRUE )) && print -u 2 -- "# Network Service Name...................: ${TORF}"
(( VERBOSE == TRUE )) && print -u 2 -- "# Log message type.......................: ${TYPE}"
(( VERBOSE == TRUE )) && print -u 2 -- "# Log message class......................: ${CLASS}"
(( VERBOSE == TRUE )) && print -u 2 -- "# IP protocol typeLog message class......: ${UORT}"
################################################################
SOURCE="${NODENAME}"
DESCRIPTION="Port test failed for ${NODENAME}:${PORTNUM}. Node is not LISTENing"
MSG="${DATETIME} ${SYSTEM} ${SOURCE} ${TYPE} ${CLASS} ${DESCRIPTION}"
if ( exec 3>/dev/${UORT}/${NODENAME}/${PORTNUM} )
then
(( VERBOSE == TRUE )) && print -- "# ${NODENAME}:${PORTNUM} is LISTENing"
return 0
else
(( VERBOSE == TRUE )) && print -- "# ${NODENAME}:${PORTNUM} is not LISTENing"
print -- "${MSG}" >> "${LOGFILE}"
return 4
fi
exec 3>&-
return 5
}
################################################################
ckport_k93 "${@}"