penetration programming Modify the .sh script so that it also works interactively. Specifically, if no hostname is given on the command line, the program should interactively ask for a hostname, starting port, and ending port with three separate prompts, and carry out its scan using those values. After scanning finishes, the program should loop to receive another set of values, stopping only when the user enters a blank host name. If this feature is implemented properly, it will also allow you to run the script in ’batch’ mode, by “piping in” a plain text file with the hostname on the first line, start port on the second, stop port on the third, and repeating for as many hosts as you wish to scan. For example, if a file named hosts_to_scan.txt contains a list of hosts and ports in the proper format, the program should now work as follows: cat hosts_to_scan.txt | ./portscanner.sh The timeout argument should still work in this case as well: cat hosts_to_scan.txt | ./portscanner.sh -t 3 .sh #!/bin/bash #Basic bash port scanner # CHECKING CONDITION FOR -t OPTION if [ "$1" == '-t' ]; then # IF -t IS USED time=$2 host=$3 startport=$4 stopport=$5 else # IF -t IS NOT USED time=2 host=$1 startport=$2 stopport=$3 fi function pingcheck { pingresult=$(ping -c 1 $host | grep bytes | wc -l) if [ "$pingresult" -gt 1 ]; then echo else echo "$host is down, quitting" exit fi } function portcheck { # IF TIMEOUT VALUE OF LESS THAN 1 if [ "$time" -lt 1 ] then echo "Timeout value must be greater than 0, quitting" exit # IF TIMEOUT VALUE IF NOT EQUAL TO 2 elif [ "$time" -ne 2 ] then echo "Note: Timeout changed to $time." fi for ((counter=$startport; counter<=$stopport; counter++)) do if timeout $time bash -c "echo >/dev/tcp/$host/$counter" then echo "$counter open" else echo "$counter closed" fi done } #first, check that the host is alive pingcheck # next, loop through the ports portcheck
penetration programming
Modify the .sh script so that it also works interactively. Specifically, if no hostname is given on the command line, the program should interactively ask for a hostname, starting port, and ending port with three separate prompts, and carry out its scan using those values. After scanning finishes, the program should loop to receive another set of values, stopping only when the user enters a blank host name.
If this feature is implemented properly, it will also allow you to run the script in ’batch’ mode, by “piping in” a plain text file with the hostname on the first line, start port on the second, stop port on the third, and repeating for as many hosts as you wish to scan.
For example, if a file named hosts_to_scan.txt contains a list of hosts and ports in the proper format, the program should now work as follows:
cat hosts_to_scan.txt | ./portscanner.sh
The timeout argument should still work in this case as well:
cat hosts_to_scan.txt | ./portscanner.sh -t 3
.sh
#!/bin/bash
#Basic bash port scanner
# CHECKING CONDITION FOR -t OPTION
if [ "$1" == '-t' ]; then
# IF -t IS USED
time=$2
host=$3
startport=$4
stopport=$5
else
# IF -t IS NOT USED
time=2
host=$1
startport=$2
stopport=$3
fi
function pingcheck
{
pingresult=$(ping -c 1 $host | grep bytes | wc -l)
if [ "$pingresult" -gt 1 ]; then
echo
else
echo "$host is down, quitting"
exit
fi
}
function portcheck
{
# IF TIMEOUT VALUE OF LESS THAN 1
if [ "$time" -lt 1 ]
then
echo "Timeout value must be greater than 0, quitting"
exit
# IF TIMEOUT VALUE IF NOT EQUAL TO 2
elif [ "$time" -ne 2 ]
then
echo "Note: Timeout changed to $time."
fi
for ((counter=$startport; counter<=$stopport; counter++))
do
if timeout $time bash -c "echo >/dev/tcp/$host/$counter"
then
echo "$counter open"
else
echo "$counter closed"
fi
done
}
#first, check that the host is alive
pingcheck
# next, loop through the ports
portcheck
![](/static/compass_v2/shared-icons/check-mark.png)
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 4 images
![Blurred answer](/static/compass_v2/solution-images/blurred-answer.jpg)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![C How to Program (8th Edition)](https://www.bartleby.com/isbn_cover_images/9780133976892/9780133976892_smallCoverImage.gif)
![Database Systems: Design, Implementation, & Manag…](https://www.bartleby.com/isbn_cover_images/9781337627900/9781337627900_smallCoverImage.gif)
![Programmable Logic Controllers](https://www.bartleby.com/isbn_cover_images/9780073373843/9780073373843_smallCoverImage.gif)