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
Language: Bash
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
[/pre]
#!/bin/bash
# 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
USER=`id -un`
# Root is required to run netstat -ntlup
if [ "$USER" != "root" ]; then
echo "Root privileges required."
else
NET=`netstat -ntlup | grep $1`
if [ -z "$NET" ]; then
echo "Port is free"
else
echo $NET
fi
fi[/port][/port][/pre]
[/pre]
[/pre]
ASKED:
Sep 21, 2011 2:04 PM GMT
UPDATED:
March 31, 2012 3:47:40 PM GMT