Search Posts on Binpipe Blog

Shell Script to Check a Port is Open or Closed

The below Shell Script will check if a port is open or closed. Save the below code to a file named checkport and give executable permission to that file.

Usage: checkport <hostname or IP> <Port No.>
#!/bin/bash  nc -z $1 $2 1>/dev/null 2>&1; result=$?;   if [ $result -eq 0 ]; then       echo 'The port is open for tcp connections'   else       echo 'The port is closed'   fi

Using telnet `telnet <host_or_ip> <port>' will do the job, but it doesn't leave room for automation, so this script is better alternative.

No comments:

Post a Comment

Hi, Leave a comment here and one of the binary piper's will reply soon :)