2013-01-16 15 views
0

好的,有點奇怪/具體的問題,但我有一個命令在linux上ping服務器3次,並返回成功的回覆Linux命令在服務器上生成文件,如果ping服務器關閉,並且不同的文件,如果它是up

ping -c 3 google.com | grep 'received' | awk -F',' '{ print $2 }' | awk '{ print $1 }'

我要擴展這個所以它創建了一個名爲服務器上的文件「server_is_up.txt(如果服務器是向上(即 - 數爲3),或寫入文件」 server_is_down .txt「(如果服務器宕機,顯然)

試過這個但它沒有工作:

ping -c 3 google.com | grep 'received' | awk -F',' '{ print $2 }' | awk '{ if ($1 == 0) { > host_is_down.txt } else { > host_is_up.txt } }'

我敢肯定,這是顯而易見的,但不能想放在哪裏「> host_is_down.txt」的一部分。

感謝

回答

3

試試這個:

ping -c 3 google.com | grep '0 received' && touch host_is_down.txt || touch host_is_up.txt 
+0

這將返回主機彌補了一切,因爲「獲得」任何平安返回,如果有一個迴應與否並不重要:「3包傳輸,0收到,100%數據包丟失,時間2016ms「 – BT643

+0

您的答案沒有直接工作,但稍微修改它就像這樣工作:D謝謝 - ping -c 3 google.com | grep'0 received'&& touch host_is_down.txt ||觸摸host_is_up.txt – BT643

+0

Doh!對不起,答案已修正。 – ams

相關問題