Port scanner script
by vfraanco from LinuxQuestions.org on (#4V9ZT)
Could someone give me a light on this script, I do not know how to do would like a help how to start it
a) The script should receive two parameters, the first can be a port using the -n option or a file containing several ports described each on a line using the -f option and the second should always be a file containing an ip address named a host per line representing the hosts to be tested using the -f option
example: portscanner.sh -n 80 -h hostfile.txt
portscanner.sh -f portfile.txt -h hostfile.txt
b) It must be checked whether the correct number of parameters have been entered. You should also check whether the -n option if it is in use is accompanied by a value between 1 and 65535 and whether the -f and -h options are accompanied by a file. If not, the script should be terminated.
c) port response verification can be performed with the command: nc -zvw1 host port. whenever the port is active the response to this command contains the word succeeded
example: result = $ (nc -zvw1 www.google.com 443 2> & 1 | grep succeded)
d) the script should create a file named scan_report.txt, that file should indicate which ports are active, inactive or invalid
already did that
#! / bin / bash
#VARIABLES
num = $ 2
#SCRIPT
if [$ # -eq 4]
then
if [$ 1 = "-n"] || [$ 1 = "-f"] || [$ 1 = "-h"]
then
case "$ 1" in
"-n")
# portscanner.h -n port -h hostfile
if [$ (echo $ 2 | grep -E "^ [0-9] $ | ^ [1-9] [0-9] $ | ^ [1-9] [0-9] [0-9] $ | ^ [1-9] [0-9] [0-9] [0-9] $ | ^ [1-9] [0-9] [0-9] [0-9] [0-9] $ ")]
then
if [$ 2 -g 1] && [$ 2 -le 65535] && [$ 3 = "-h"] && [-f $ 4]
then
for host in $ (cat $ 4)
of
$ (nc -zvw1 $ host $ 2 | grep "succeeded")
done
else
exit
fi
else
exit
fi ;;
"-f")
# portscanner.h -f portfile -h hostfile
if [-f $ 2] && [$ 3 = "-h"] && [-f $ 4]
then
echo "It still has to be done!"
else
exit
fi ;;
"-H" )
case "$ 3" in
"-n")
# portscanner.h -h hostfile -n port
if [$ (echo $ 4 | grep -E "^ [0-9] $ | ^ [1-9] [0-9] $ | ^ [1-9] [0-9] [0-9] $ | ^ [1-9] [0-9] [0-9] [0-9] $ | ^ [1-9] [0-9] [0-9] [0-9] [0-9] $ ")]
then
if [-f $ 2] && [$ 4 -g and 1] && [$ 4 -le 65535]
then
echo "It still has to be done!"
else
exit
fi
else
exit
fi ;;
"-f")
# portscanner.h -h hostfile -f portfile
if [-f $ 2] && [-f $ 4]
then
echo "It still has to be done!"
else
exit
fi ;;
*) exit ;;
esac ;;
*) exit ;;
esac
else
exit
fi
fi


a) The script should receive two parameters, the first can be a port using the -n option or a file containing several ports described each on a line using the -f option and the second should always be a file containing an ip address named a host per line representing the hosts to be tested using the -f option
example: portscanner.sh -n 80 -h hostfile.txt
portscanner.sh -f portfile.txt -h hostfile.txt
b) It must be checked whether the correct number of parameters have been entered. You should also check whether the -n option if it is in use is accompanied by a value between 1 and 65535 and whether the -f and -h options are accompanied by a file. If not, the script should be terminated.
c) port response verification can be performed with the command: nc -zvw1 host port. whenever the port is active the response to this command contains the word succeeded
example: result = $ (nc -zvw1 www.google.com 443 2> & 1 | grep succeded)
d) the script should create a file named scan_report.txt, that file should indicate which ports are active, inactive or invalid
already did that
#! / bin / bash
#VARIABLES
num = $ 2
#SCRIPT
if [$ # -eq 4]
then
if [$ 1 = "-n"] || [$ 1 = "-f"] || [$ 1 = "-h"]
then
case "$ 1" in
"-n")
# portscanner.h -n port -h hostfile
if [$ (echo $ 2 | grep -E "^ [0-9] $ | ^ [1-9] [0-9] $ | ^ [1-9] [0-9] [0-9] $ | ^ [1-9] [0-9] [0-9] [0-9] $ | ^ [1-9] [0-9] [0-9] [0-9] [0-9] $ ")]
then
if [$ 2 -g 1] && [$ 2 -le 65535] && [$ 3 = "-h"] && [-f $ 4]
then
for host in $ (cat $ 4)
of
$ (nc -zvw1 $ host $ 2 | grep "succeeded")
done
else
exit
fi
else
exit
fi ;;
"-f")
# portscanner.h -f portfile -h hostfile
if [-f $ 2] && [$ 3 = "-h"] && [-f $ 4]
then
echo "It still has to be done!"
else
exit
fi ;;
"-H" )
case "$ 3" in
"-n")
# portscanner.h -h hostfile -n port
if [$ (echo $ 4 | grep -E "^ [0-9] $ | ^ [1-9] [0-9] $ | ^ [1-9] [0-9] [0-9] $ | ^ [1-9] [0-9] [0-9] [0-9] $ | ^ [1-9] [0-9] [0-9] [0-9] [0-9] $ ")]
then
if [-f $ 2] && [$ 4 -g and 1] && [$ 4 -le 65535]
then
echo "It still has to be done!"
else
exit
fi
else
exit
fi ;;
"-f")
# portscanner.h -h hostfile -f portfile
if [-f $ 2] && [-f $ 4]
then
echo "It still has to be done!"
else
exit
fi ;;
*) exit ;;
esac ;;
*) exit ;;
esac
else
exit
fi
fi