簡體   English   中英

bash 中的數組循環問題

[英]issues looping over array in bash

我在 bash 中編寫了一個非常簡單的端口掃描腳本。對於我的端口參數,我接受單個端口(例如:443)、逗號分隔列表(例如:80,443)或一個范圍(例如:1-1000)。

當我使用單個端口或以逗號分隔的端口列表運行我的腳本時,一切都按預期運行:

~/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.

但是,當我使用范圍運行時,我得到:

~/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.

我分配數組的相關代碼:

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

和循環本身:

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

我不確定這是因為我分配端口陣列的方式,還是因為我在循環中出錯。 我是 bash 腳本編寫的新手,而且我很難弄清楚我做錯了什么。 我在 SO 上讀到過一些循環運行的命令吃掉了腳本其他部分的 output,但我不認為這里是這種情況,因為腳本確實打印到屏幕上,只是不符合預期。

編輯:這是完整的腳本:

#!/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."

處理portarray=(...)分配(當ports=1-10時)

考慮:

$ 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')

注意:來自$(seq...)調用的 output 被處理為帶有嵌入式換行符的單個字符串。

幾個想法:

### 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")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM