我有一個 .txt 格式的 IP 地址串列
162.243.217.39
170.175.178.13
235.169.86.84
218.820.241.164
104.89.61.87
254.217.220.124
有必要 ping IP 地址并將那些給出錯誤 2 (stderr) 和作業結果的地址分類到兩個檔案中。
我認為它應該看起來像這樣:
while IFS= read -r ip; do
if ping -c4 $ip == 0 ||ping -c4 $ip == 1
then >> correct.txt
else >> error.txt
done < ip_list.txt
但我不明白如何在語法方面正確地做到這一點
uj5u.com熱心網友回復:
假設:
- 想要從所有對檔案和的
ping呼叫中分別捕獲標準輸出和標準錯誤correct.txterror.txt - 需要測驗/回應
ping呼叫的回傳碼
通常來說,一般來說:
ping .... >> correct.txt 2>> error.txt
注意: 和之間沒有空格2>>
例子:
$ ping -c2 ddd.ddd.com >> correct.txt 2>> error.txt
$ echo $?
1
$ ping -c2 yahoo.com >> correct.txt 2>> error.txt
$ echo $?
0
$ head correct.txt error.txt
==> correct.txt <==
PING yahoo.com (98.137.11.164): 56 data bytes
64 bytes from 98.137.11.164: icmp_seq=0 ttl=48 time=281.624 ms
64 bytes from 98.137.11.164: icmp_seq=1 ttl=48 time=239.202 ms
--- yahoo.com ping statistics ---
2 packets transmitted, 2 packets received, 0% packet loss
round-trip min/avg/max/stddev = 239.202/260.413/281.624/21.211 ms
==> error.txt <==
ping: unknown host
輸出顯示echo $?我們仍然可以訪問回傳代碼,因此如果您需要在腳本中執行其他操作,那么這應該足夠了:
while IFS= read -r ip; do
if ping -c4 $ip >> correct.txt 2>> error.txt
then
do some other stuff
else
do some other stuff
fi
done < ip_list.txt
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/525763.html
標籤:linux重击ip
