#!/bin/bash

# (C) 2001 by Geir Torstein Kristiansen <gtk@linux.online.no>
# This script is released under the terms of the GPL

# Extremely simple tcp port scanner written in bash2 as a proof of concept.
# Relies on the new tcp redirection capabilities of bash2.

# Disclaimer: I take no responsibility for what you do with bash2map.sh

if [ -z $1 ]; then
	echo "Usage: [ip|hostname]"
	exit 1
fi

echo -e "bash2map.sh 1.0\n"
echo -e "Port\t\tState\t\tService"

(for ((p=0; p < 1024; p++)); do 
 echo "0" >/dev/tcp/$1/$p \
 && service=$( awk -v p=$p '{if ($2 ~p) print $1 }'</etc/services | head -n1 ) \
 && echo -e "$p/tcp\t\topen\t\t$service" >&1;
done) 2>/dev/null

