I.T. Security and Linux Administration

May 28 2010   6:47PM GMT

Port Checker

Eric Hansen Eric Hansen Profile: Eric Hansen

A while ago, I got bored and decided to venture into the realm of scripting.  Managing server after server, typing “netstat -ntlup | grep…” got annoying after a while, so I decided to write this.

The script requires the runner/user to be root.  Which is flawed, I know, but this works for me…if you want/need it modified, then I’ll do my best to help.


# Script is used to determine if a port is used.
# Usage: portcheck <port number>
# Executes netstat -ntlup | grep <port> 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."
	NET=`netstat -ntlup | grep $1`

	if [ -z "$NET" ]; then
		echo "Port is free"
		echo $NET

This can check against either a port or program name (which is why root privileges is demanded).

1  Comment on this Post

There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when other members comment.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
  • IT Knowledge Exchange’s Newest Contest! - ITKE Community Blog
    [...] 1. Script Name: Port Checker [...]
    0 pointsBadges:

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

Share this item with your network: