A Script to Portscan with Netcat
I was bored tonight and looking to learn a bit of bash, thus here is my first “real” script that does something. It will take an IP address or domain name and scan all of the well-known (0-1023) ports and test if they are open. I know I know, nmap is wildly more appropriate for this, but it was for educational purposes. To its credit it is simple, lightweight, and quite fast. Perhaps I will put it to use sometime down the road.
#!/bin/bash
# Simple Port Scan 0.1
# http://crims0n.com
# make sure netcat is on the system
command -v nc >/dev/null 2>&1 || { echo >&2 "Netcat is required to utilize this script, please install it then try again."; exit 1; }
# test to be sure a parameter was passed
if [ -z "$1" ]
then
clear
echo Simple Port Scan 0.1
echo ----------------------------------------------------------
echo Use this script to scan a host for any open well known
echo ports open. You will need to pass an IP address as an
echo argument. You can pass a hostame as well, however scanning
echo will be much slower, so it is recommended that you use
echo the host command to learn the IP first.
echo ----------------------------------------------------------
exit 1
fi
# create a log file
touch "$1"_log
echo Open Ports on host: > "$1"_log
# search through the well known ports and test if they are open
for (( c=1; c<=1023; c++ ))
do
# use netcat to search
nc -z "$1" "$c"
# if we get a hit, append the port to log file
if [ $? -eq 0 ]
then
echo "$c" >> "$1"_log
fi
done


