Example Contest Script: Port Checker

352570 pts.
Tags:
Contests
IT Knowledge Exchange
IT Scripts
Nintendo 3DS
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]

Answer Wiki

Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

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

Discuss This Question:  

 
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 members answer or reply to this question.

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

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:

To follow this tag...

There was an error processing your information. Please try again later.

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

Thanks! We'll email you when relevant content is added and updated.

Following