【问题标题】:probabely "loop" and "output" doesn't work properly in my bash script可能“循环”和“输出”在我的 bash 脚本中无法正常工作
【发布时间】:2014-02-07 08:33:52
【问题描述】:

我写了以下脚本:

#!/bin/bash

echo "Reading data - headers - both"

if [ $# -ne 3 ]; then
    echo "Usage: ./nmap <port-range> <ip-list> <d || h || b>"
    exit 1
fi

rm -f /tmp/right.txt 1>/dev/null 2>/dev/null
rm -f /tmp/wrong.txt 1>/dev/null 2>/dev/null

output=""
if [ $3 == h ]; then
    while read -r -u3 port; do
    while read -r -u4 ip; do
#       echo -en "\n$ip $port: "
        OUT=$( nmap -p "$port" --script=http-headers.nse "$ip" | awk 'NR>=7 && NR<=10')
     #   [[ $OUT == *Apache* ]] && $(echo -en "$ip  $port\n" >> /tmp/right.txt) || $(echo -en "$ip  $port\n" >> /tmp/wrong.txt)
        [[ $OUT == *Apache* ]] && output="$output `echo -en "\n$ip -------------------- $port "`" && echo -e "$output" | column -t >> /tmp/right.txt || output="$output `echo -en "\n$ip -------------------- $port "`" && echo -e "$output" | column -t >> /tmp/wrong.txt
    done 4< "$2"
    done 3< "$1"

    echo -e "$output" | column -t

elif [ $3 == d ]; then
    echo data
elif [ $3 == b ]; then 
    echo both
fi

我希望我的输出有两个文件:

cat right.txt
ip1 ..... port1
ip2 ..... port1
ip2 ..... port2
ip3 ..... port3
.
.
.

cat wrong.txt
ip1 ..... port1
ip2 ..... port1
ip2 ..... port2
ip3 ..... port3
.
.
.

但它不能正常工作......

有什么想法吗?

提前谢谢你

【问题讨论】:

  • 我会编辑我的帖子并输入我的真实输出
  • 试试[[ $OUT == *Apache* ]] &amp;&amp; output="$output echo -en "\n$ip -------------------- $port "" &amp;&amp; echo -e "$output" | column -t &gt; tee /tmp/right.txt /tmp/wrong.txt &gt; /dev/null
  • @JKB pardon..我会试试看..但如果您编辑我的脚本并在答案部分修改它,我也会非常感谢...
  • 我已经更新了你的脚本对你有用吗?
  • @JKB 我去查一下

标签: linux bash loops awk nmap


【解决方案1】:

请找到更新的答案,因为我为您修改了宝马的答案,请检查它。

   #!/bin/bash

    echo "Reading data - headers - both"

    if [ $# -ne 3 ]; then
        echo "Usage: ./nmap <port-range> <ip-list> <d || h || b>"
        exit 1
    fi

    join -j 2 $2 $1 > temp.txt

    headers() 
    {
         while read -r ip port
         do
            printf "ip: %s  port:%d \n" $ip $port
            OUT=$(nmap -p "$port" --script=http-headers.nse "$ip" | tac | awk -F: 'NR<=13&&/Apache/{print $2; exit}')   
            if [[ "$OUT" == *Apache* ]]; then
                echo $ip $port >> /tmp/right.txt
            else
                echo $ip $port >> /tmp/wrong.txt
            fi
         done < temp.txt
    } 

    case $3 in 
      "h") headers ;;
      "d") echo data;;
      "b") echo both;;
      "*") echo "wrong input"
           exit;;
    esac

【讨论】:

  • @JKB 我们真的很亲近但是对错还是不对:(
  • @MortezaLSC 你能给出我新编辑的答案吗..我刚刚编辑过
  • @JKB 我把输出放在 answerpart.see please
  • @MortezaLSC 上面的脚本为我工作..也在我的答案中更新了输出
  • @MortezaLSC 请找到我修改后的宝马的答案。
【解决方案2】:

你的短路逻辑有缺陷。 true &amp;&amp; false || true &amp;&amp; true 将执行所有四个语句。

不清楚为什么您认为echo 的输出状态无论如何都表明除了成功之外的任何内容。

这更接近你的意思吗?

output="$output `echo -en "\n$ip -------------------- $port "`"

[[ $OUT == *Apache* ]] && file=/tmp/right.txt || file=/tmp/wrong.txt

echo -e "$output" | column -t >>"$file"

这仍然是错误的,因为它会多次回显累积的output,但至少它应该向您展示需要更改的内容(以及如何重构代码以避免重复)。

我猜你实际上想要类似的东西

[[ $OUT == *Apache* ]] && file=/tmp/right.txt || file=/tmp/wrong.txt
output="$output `echo -en "\n$ip -------------------- $port " | tee -a "$file"`"

除了这不会通过column -t 运行文件中的副本。但是您可以稍后再做,或者在此处添加它并稍后避免(无论如何,您似乎最终都会为所有输出实例运行它)。

【讨论】:

  • 你能修改我的脚本吗..我不明白对不起
猜你喜欢
  • 2023-03-04
  • 2011-06-13
  • 2014-05-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多