Bash 解析命令的输出,并存储在var中

Bash 解析命令的输出,并存储在var中,bash,Bash,我有一个命令要运行: echo "Performing unicornscan on:" $target unicornscan -Ivv $target:1-65535 | tee enum/uniscan-alltcp.txt 这将产生如下输出: TCP open 192.168.0.102:443 ttl 128 connected 192.168.103.227:54487 -> 192.168.0.102:161 TCP open 192.168.0.102:161 ttl

我有一个命令要运行:

echo "Performing unicornscan on:" $target
unicornscan -Ivv $target:1-65535 | tee enum/uniscan-alltcp.txt
这将产生如下输出:

TCP open 192.168.0.102:443  ttl 128
connected 192.168.103.227:54487 -> 192.168.0.102:161
TCP open 192.168.0.102:161  ttl 128
connected 192.168.103.227:47765 -> 192.168.0.102:80
TCP open 192.168.0.102:80  ttl 128
connected 192.168.103.227:4267 -> 192.168.0.102:1884
TCP open 192.168.0.102:139  ttl 128
sender statistics 963.9 pps with 65536 packets sent total
listener statistics 131180 packets recieved 0 packets droped and 0 interface drops
TCP open                http[   80]     from 192.168.0.102  ttl 128
TCP open         netbios-ssn[  139]     from 192.168.0.102  ttl 128
TCP open                snmp[  161]     from 192.168.0.102  ttl 128
TCP open               https[  443]     from 192.168.0.102  ttl 128
TCP open        microsoft-ds[  445]     from 192.168.0.102  ttl 128 
如何解析此输出以获取逗号分隔列表中的所有ip地址,如:
80139161443445


谢谢

我写了一个脚本,可以解决您的问题。这将打印所有输出

遵循以下步骤:

  • 在文本编辑器中打开
    answer.bash
    (或任何您想要命名脚本的名称)

  • 键入或粘贴以下内容:

  • 脚本:

    #!/bin/bash
    
    # Store the command in a variable for easy changes
    # Note: The command is not actually executed until the while loop
    COMMAND="unicornscan -Ivv $target:1-65535 | tee enum/uniscan-alltcp.txt"
    
    PORTS=()
    
    # For each line:
    while read line; do
            # The third token is either IP or protocol name with '['
            token=`echo $line | awk '{print $3}'`
            last_char_idx=$((${#token}-1))
            last_char=${token:$last_char_idx:1}
            # Case 1: It is the protocol name
            if [[ "$last_char" = "[" ]]; then
                    # This is a protocol. Therefore, port is token 4
                    port=`echo $line | awk '{print $4}'`
                    # Shave off the last character
                    port=${port::-1}
            else
                    # token is ip:port. Awk out the port
                    port=`echo $token | awk -F: '{print $2}'`
            fi
            PORTS+=("$port")
    done < <($COMMAND | egrep "^TCP open")
    # egrep is used to skip any lines that don't begin with TCP open
    # 'tee' should still send full output to file
    
    # Print all the ports we found
    for p in "${PORTS[@]}"; do
            echo -n "$p, "
    done
    echo
    
    您可以将此脚本的输出通过管道传输到文件或变量中,或者将其存储在脚本内部的变量中

    请让我知道它是如何工作的,如果你有问题。另外,对于过度使用subshell,我提前表示歉意。

    请尝试以下方法:

    #!/bin/bash
    
    declare -a ary
    while read -r line; do
        if [[ "$line" =~ \[\ *([0-9]+)\] ]]; then
            ary+=("${BASH_REMATCH[1]}")
        fi
    done < "file.txt"
    
    ifs_bak="$IFS"  # back up IFS
    IFS=,; list="${ary[*]}"
    IFS="$ifs_bak"  # retrieve IFS
    
    echo "$list"
    
    #/bin/bash
    声明-一份声明
    而read-r行;做
    如果[[“$line”=~\[\*([0-9]+)\]];然后
    ary+=(“${BASH_重新匹配[1]}”)
    fi
    完成<“file.txt”
    ifs_-bak=“$ifs”#备份ifs
    如果S=,;list=“${ary[*]}”
    IFS=“$IFS_bak”#检索IFS
    回显“$list”
    
    • 正则表达式提取一个介于
      [
      ]
      之间的数值,并将
      ${BASH_REMATCH[1]}
      赋值给它
    • 然后将这些值存储在数组
      ari
    • 最后,
      ari
      以逗号分隔的值打印

    您想获取IP地址还是端口?在上面的示例中,
    80139161443445
    似乎是端口。使用
    sed
    将行与
    TCP open.*\[/code>匹配,并获取
    [/code>和
    ]
    之间的数字。
    #!/bin/bash
    
    declare -a ary
    while read -r line; do
        if [[ "$line" =~ \[\ *([0-9]+)\] ]]; then
            ary+=("${BASH_REMATCH[1]}")
        fi
    done < "file.txt"
    
    ifs_bak="$IFS"  # back up IFS
    IFS=,; list="${ary[*]}"
    IFS="$ifs_bak"  # retrieve IFS
    
    echo "$list"