amming The .sh script can be run with either 0, 2, 3, or 5 command-line arguments. As a basic sanity check, Your program should test at the beginning that the number of arguments given is one of these. If not, the user has entered something incorrectly, and the script should output the error message Usage: ./portscanner.sh [-t timeout] [host startport stopport], and immediately exit.

A+ Guide To It Technical Support
10th Edition
ISBN:9780357108291
Author:ANDREWS, Jean.
Publisher:ANDREWS, Jean.
Chapter18: Macos, Linux, And Scripting
Section: Chapter Questions
Problem 12TC
icon
Related questions
Question

Penetration programming

The .sh script can be run with either 0, 2, 3, or 5 command-line arguments. As a basic sanity check, Your program should test at the beginning that the number of arguments given is one of these. If not, the user has entered something incorrectly, and the script should output the error message

Usage: ./portscanner.sh [-t timeout] [host startport stopport],

and immediately exit.

.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

 

Expert Solution
steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Linux
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
A+ Guide To It Technical Support
A+ Guide To It Technical Support
Computer Science
ISBN:
9780357108291
Author:
ANDREWS, Jean.
Publisher:
Cengage,
LINUX+ AND LPIC-1 GDE.TO LINUX CERTIF.
LINUX+ AND LPIC-1 GDE.TO LINUX CERTIF.
Computer Science
ISBN:
9781337569798
Author:
ECKERT
Publisher:
CENGAGE L