【问题标题】:How to disable error message sent by command "ping"?如何禁用命令“ping”发送的错误消息?
【发布时间】:2019-08-06 16:07:43
【问题描述】:

我使用 bash 脚本编写了一个简单的 ping 扫描器。我还使用 grep 命令过滤掉我想要的结果。问题是,无论我尝试什么 grep 命令,控制台都会不断打印出错误消息:“ping: recvmsg: No route to host”。我尝试将输出写入文件,文件内没有错误消息,但它们仍然出现在控制台上。我想知道是什么导致控制台打印出这样的错误消息以及如何禁用它,谢谢。

这是我写的脚本。

#!/bin/bash
for ip in $(seq 1 254); do
#ping -c 1 10.11.1.$ip | grep -v "recvmsg" | grep "bytes from" | cut -d " " -f 4 | cut -d ":" -f 1 &
ping -c 1 10.11.1.$ip | grep -v "recvmsg" |grep -v "ping" |  grep "bytes from"  | cut -d " " -f 4 | cut -d ":" -f 1| sort -d >> report &
done
wait

这是错误信息

ping: recvmsg: No route to host

【问题讨论】:

    标签: bash shell


    【解决方案1】:

    您可以将重定向器用于 stderr(标准错误),您只需将其放在命令末尾 2> error.log

    #!/bin/bash
    for ip in $(seq 1 254); do
    #ping -c 1 10.11.1.$ip | grep -v "recvmsg" | grep "bytes from" | cut -d " " -f 4 | cut -d ":" -f 1 &
    ping -c 1 10.11.1.$ip 2> error.log | grep -v "recvmsg" |grep -v "ping" |  grep "bytes from"  | cut -d " " -f 4 | cut -d ":" -f 1| sort -d >> report   &
    done
    wait
    

    【讨论】:

    • 使用 shell 大括号扩展 {1..254} 而不是 seq 命令。
    猜你喜欢
    • 2019-10-17
    • 1970-01-01
    • 1970-01-01
    • 2012-04-13
    • 1970-01-01
    • 2021-01-26
    • 2015-11-29
    • 1970-01-01
    相关资源
    最近更新 更多