Home > Blockchain >  issues looping over array in bash
issues looping over array in bash

Time:03-14

I have written a very simple port scanning script in bash. For my ports argument, i accept a single port (eg: 443), a comma-separated list (eg: 80,443), or a range (eg: 1-1000).

When I run my script with a single port or comma-separated list of ports, everything runs as it should:

~/projects/bash-port-scan# ./bash-port-scan.sh -i xx.xx.xxx.xxx -p 1,80,443 -v
Beginning scan of xx.xx.xxx.xxx
Port 1 closed
Port 80 open
Port 443 open
Scan complete.

~/projects/bash-port-scan# ./bash-port-scan.sh -i xx.xx.xxx.xxx -p 80 -v
Beginning scan of xx.xx.xxx.xxx
Port 80 open
Scan complete.

However, when I run with a range, I get:

~/projects/bash-port-scan# ./bash-port-scan.sh -i xx.xx.xxx.xxx -p 1-10 -v
Beginning scan of xx.xx.xxx.xxx
Port 1
2
3
4
5
6
7
8
9
10 closed
Scan complete.

Relevant code where I assign the array:

portarray=()
if [[ "$ports" == *","* ]]; then
    IFS=','
    read -r -a portarray <<< $ports
    IFS=' '
elif [[ "$ports" == *"-"* ]]; then
    IFS='-' 
    read -r -a range <<< $ports
    IFS=' '
    
    first="${range[0]}"
    last="${range[1]}"
    portarray=($(seq $first 1 $last))
else
    portarray=($ports)
fi

and the loop itself:

empty=""
for p in "${portarray[@]}"; do
    result=$(nc -zvw5 $ip $p 2>&1 | grep open)
    if [ "$result" = "$empty" ]; then
        if [ $verbose -eq 1 ]; then
            str="Port "
            closed=" closed"
            echo "$str$p$closed"
        fi
    else
        str="Port "
        closed=" open"
        echo "$str$p$closed"
    fi
done

I'm not sure if this is because of how I'm assigning my port array, or if it is because of something I have wrong in my loop. I'm relatively new to bash scripting, and I'm having a terrible time figuring out what I have wrong. I've read here on SO about some commands run in loops eating the output of other portions of the script, but I don't believe that to be the case here, as the script does actually print to screen, just not as expected.

EDIT: Here is the full script:

#!/bin/bash

verbose=0
while [ "$1" != "" ]; do
    case "$1" in
        -h | --help )
echo "bash-port-scan.sh v0.1\r\nUsage: ./bash-port-scan.sh -i 127.0.0.1 -p 80,443\r\n./bash-port-scan.sh -i 127.0.0.1 -p 1-1000"; shift;;
        -v | --verbose )
verbose=1; shift;;
        -i | --ip ) 
ip="$2";    shift;;
        -p | --ports )
ports="$2"; shift;; 
    esac
    shift
done

if [[ $ip = "" ]]; then
    echo "Please enter an IP address with -i"
    exit
fi

if [[ $ports = "" ]]; then
    echo "Please enter the port(s) with -p"
    exit
fi

portarray=()
if [[ "$ports" == *","* ]]; then
    IFS=','
    read -r -a portarray <<< $ports
    IFS=' '
elif [[ "$ports" == *"-"* ]]; then
    IFS='-' 
    read -r -a range <<< $ports
    IFS=' '
    
    first="${range[0]}"
    last="${range[1]}"
    portarray=($(seq $first $last))
else
    portarray=($ports)
fi

if [ $verbose -eq 1 ]; then
    echo "Beginning scan of $ip"
fi

shuf -e "${portarray[@]}"

empty=""
for p in "${portarray[@]}"; do
    result=$(nc -zvw5 $ip $p 2>&1 | grep open)
    if [ "$result" = "$empty" ]; then
        if [ $verbose -eq 1 ]; then
            str="Port "
            closed=" closed"
            echo "$str$p$closed"
        fi
    else
        str="Port "
        closed=" open"
        echo "$str$p$closed"
    fi
done

echo "Scan complete."

CodePudding user response:

Addressing just the portarray=(...) assignment (when ports=1-10)

Consider:

$ first=1
$ last=10
$ portarray=($(seq $first 1 $last))
$ typeset -p portarray
declare -a portarray=([0]=$'1\n2\n3\n4\n5\n6\n7\n8\n9\n10')

NOTE: the output from the $(seq ...) call is processed as a single string with embedded linefeeds.

A couple ideas:

### define \n as field separator; apply custom IFS in same line to limit IFS change to just the follow-on array assignment:

$ IFS=$'\n' portarray=($(seq $first 1 $last))
$ typeset -p portarray
declare -a portarray=([0]="1" [1]="2" [2]="3" [3]="4" [4]="5" [5]="6" [6]="7" [7]="8" [8]="9" [9]="10")

### use mapfile to read each *line* into a separate array entry:

$ mapfile -t portarray < <(seq $first 1 $last)
$ typeset -p portarray
declare -a portarray=([0]="1" [1]="2" [2]="3" [3]="4" [4]="5" [5]="6" [6]="7" [7]="8" [8]="9" [9]="10")
  • Related