From 251b861df594bc401a1dd6ef0fc0d269d9492949 Mon Sep 17 00:00:00 2001 From: mboelen Date: Wed, 30 Mar 2016 13:45:34 +0200 Subject: [PATCH] Changed function name to better reflect goal and UDP support --- include/functions | 59 ++++++++++++++++++++++++----------------------- 1 file changed, 30 insertions(+), 29 deletions(-) diff --git a/include/functions b/include/functions index fbafe064..7cbe476d 100644 --- a/include/functions +++ b/include/functions @@ -42,7 +42,6 @@ # FileIsEmpty Check if a file is empty # FileIsReadable Check if a file is readable or directory accessible # GetHostID Retrieve an unique ID for this host -# IsPortListening Check if machine is listening on specified protocol and port # IsRunning Check if a process is running # InsertSection Insert a section block # InsertPluginSection Insert a section block for plugins @@ -52,6 +51,7 @@ # IsWorldWritable Check if a file is world writable # LogText Log text strings to logfile, prefixed with date/time # ParseNginx Parse nginx configuration lines +# PortIsListening Check if machine is listening on specified protocol and port # Progress Show progress on screen # RandomString Show a random string # RemovePIDFile Remove PID file @@ -119,34 +119,6 @@ } - ################################################################################ - # Name : IsPortListening() - # Description : Check if machine is listening on specified protocol and port - # Returns : exit code 0 (listening) or 1 (not listening) - ################################################################################ - - IsPortListening() - { - if [ "${LSOFBINARY}" = "" ]; then - return 255 - else - if [ $# -eq 2 ] && [ $1 = "TCP" -o $1 = "UDP" ]; then - LogText "Test: find service listening on $1:$2" - FIND=`lsof -i${1} -s${1}:LISTEN | grep "${2}" | wc -l` - if [ ! "${FIND}" = "" ]; then - LogText "Result: found service listening on $1:$2" - return 0 - else - LogText "Result: did not find service listening on $1:$2" - return 1 - fi - else - return 255 - ReportException ${TEST_NO} "Error in function call to IsPortListening" - fi - fi - } - ################################################################################ # Name : CheckItem() # Description : Check if a specific item exists in the report @@ -1170,6 +1142,35 @@ done } + ################################################################################ + # Name : PortIsListening() + # Description : Check if machine is listening on specified protocol and port + # Returns : exit code 0 (listening) or 1 (not listening) + # Usage : if PortIsListening "TCP" 22; then echo "Port is listening"; fi + ################################################################################ + + PortIsListening() + { + if [ "${LSOFBINARY}" = "" ]; then + return 255 + else + if [ $# -eq 2 ] && [ $1 = "TCP" -o $1 = "UDP" ]; then + LogText "Test: find service listening on $1:$2" + if [ $1 = "TCP" ]; then FIND=`${LSOFBINARY} -i${1} -s${1}:LISTEN -P -n | grep ":${2} "`; else FIND=`${LSOFBINARY} -i${1} -P -n | grep ":${2} "`; fi + if [ ! "${FIND}" = "" ]; then + LogText "Result: found service listening on port $2 ($1)" + return 0 + else + LogText "Result: did not find service listening on port $2 ($1)" + return 1 + fi + else + return 255 + ReportException ${TEST_NO} "Error in function call to PortIsListening" + fi + fi + } + ################################################################################ # Name : Progress() # Description : Displays progress on screen with dots