This IT script was submitted by Eric Hansen as an example for IT Knowledge Exchange's new contest: Is Your Script 3-Dimensional? To submit your own script, create a new post, and use the CODE button on the far right of the text editor menu. Be sure to tag it IT Scripts. Entries not tagged IT Scripts will not be considered.
Be sure to comment on other entries; your comment could win you a Nintendo 3DS!
Name: Port Checker
Purpose: Check to see if a port is available based on either the port # or program name passed.
Notes: Requires root privileges due to "p" switch being used in netstat
# Script is used to determine if a port is used.
# Usage: portcheck
# Executes netstat -ntlup | grep to do checking.
# Returned text is stored in a variable. If variable is empty,
# port is not in used. Otherwise, port is being used.
# Get the username of the person running script
# Root is required to run netstat -ntlup
if [ "$USER" != "root" ]; then
echo "Root privileges required."
NET=`netstat -ntlup | grep $1`
if [ -z "$NET" ]; then
echo "Port is free"