Chapter 31. check_open_ports.sh

#!/bin/sh


EXPECTED_OPEN_PORTS="$*"

ACTUAL_OPEN_PORTS=`/usr/sbin/lsof -i -n -P | /bin/grep LISTEN | /bin/sed -e 's#*:##' | /bin/grep -v 127.0.0.1 | /bin/grep -v ::1 | /bin/awk '{print $8,$1}' | /bin/sort | /usr/bin/uniq | /bin/sort -n  | /usr/bin/xargs echo`


if [ "$ACTUAL_OPEN_PORTS" != "$EXPECTED_OPEN_PORTS" ]
then
	echo Open port list mis-match.  Expected \"${EXPECTED_OPEN_PORTS}\", got \"${ACTUAL_OPEN_PORTS}\".
fi