簡體   English   中英

從Bash腳本未知主機Ping

[英]Ping from bash-script unknown host

我有一個充滿主機名的文件-每行一個。 現在,我想檢查這些主機名是否存在(如果不存在,請最終從文件中刪除它們)。 我已經在第一部分失敗了:

#!/bin/bash
while read host; do
  ping -c1 "$host"
done <hosts

只給我

ping: unknown host google.com

(將google.com放在文件中進行測試)我也嘗試刪除了引號-無效。 但是,從終端運行命令時,我得到的是:

 $ ping -c1 "google.com"
   PING google.com (173.194.112.100) 56(84) bytes of data.
   ...

這里有什么問題?

您的hosts文件很可能是DOS行尾格式(CR-LF行尾),因此read使用google.com\\r read fills變量。

最簡單的方法是將文件轉換為使用dos2unix hosts UNIX行結尾。

我試圖做同樣的事情,最終得到了這個腳本,您可能會發現它很好並且有用。 仍在努力嘗試識別“ ping:未知主機hostname.co.hk​​”,但這沒關系。

    #!/bin/bash 
    cat list | while read domains; do
                reply=$(ping -c 1 $domains| grep -E '(from)' | wc -l)
                if [ $reply == 1 ]; then
                        DOMAIN=$(ping -c 1 $domains |grep 64 |awk '{print$4}')
                        IP=$(ping -c 1 $domains | grep -E -o "([0-9]{1,3}[\.]){3}[0-9]{1,3}" | head -1)
                        echo "$domains -> $DOMAIN -> $IP" >> ping.log;echo "$domains -> $DOMAIN -> $IP"
                else
                        echo "-----------------ping failed, verify domain name"
                fi 
done 
cat ping.log

暫無
暫無

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

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