で終わるしたいと思います。ここRESUを得るための一つの方法ですあなたが欲しい。ただし、私はスクリプトのどこにでもfpingを使用しませんでした。 fpingの使用があなたにとって重要なのであれば、私はその点を完全に逃したかもしれません。
#!/bin/bash
HOSTS="/tmp/hosts.txt"
declare -i DELAY=$1 # Amount of time in seconds to wait for a packet
declare -i REPEAT=$2 # Amount of times to retry pinging upon failure
# Read HOSTS line by line
while read -r line; do
c=0
while [[ $c < $REPEAT ]]; do
# If pinging an address does not return the word "0 received", we assume the ping has succeeded
if [[ -z $(ping -q -c $REPEAT -W $DELAY $line | grep "0 received") ]]; then
echo "Attempt[$((c + 1))] $line : Success"
break;
fi
echo "Attempt[$((c + 1))] $line : Failed"
((c++))
done
# If we failed the pinging of an address equal to the REPEAT count, we assume address is down
if [[ $c == $REPEAT ]]; then
echo "$line : Failed" >> /tmp/down.log # Log the failed address
fi
done < $HOSTS
使用方法:./script [遅延] [repeatCountに] - 「遅延」は、私たちがpingからの応答を待つ秒の合計額であるが、「repeatCountに私たちが失敗したときにpingを再試行回数ですアドレスがダウンしていると判断する前に
ここでは、/tmp/hosts.txt
を1行ずつ読み、ping
を使用して各アドレスを評価しています。アドレスへのpingが成功すると、次のアドレスに移動します。アドレスが失敗した場合、ユーザーが指定した回数だけ再試行します。アドレスがすべてのpingに失敗した場合は、/tmp/down.log
にログインします。
pingに失敗したかどうかを確認するための条件は、ユースケースでは正確でない可能性があります。そのため、編集する必要があります。それでも、私はこれが一般的なアイデアを得ることを望みます。
'1.1.1.1:xmt/rcv /%loss = 1/0/100%'の出力を与えるfping -c(value)を使う必要がありますので、grep/awkを 'loss'にする必要があります – Lurch
解決策を修正しました –
ありがとうございました新しい1つdown.logは空白ですか? – Lurch