ファイル確認IPの変更

ファイル確認IPの変更

scpを介して自分の外部IPアドレスを含むサーバー上のファイルを更新する必要があります。ファイルを更新する前に、実際のIPアドレスが含まれていることを確認したいと思います。それ以外の場合は、サーバーで更新しないでください。

私のIPをチェックするcronスクリプトはしばしばガベージエラーの結果を引き起こします。

私の目標をどのように達成できますか?

ベストアンサー1

現在使用している場合

curl icanhazip.com > ip-location1.txt

その後、オプションを追加すると、-fネットワークサーバーがエラーを返し、出力がない場合はエラーをcurl返す可能性があるため、出力がIPアドレスかHTMLエラーメッセージであるかを判断する必要はありません。 。

curl -f icanhazip.com > ip-location1.txt

より複雑に再試行機能を追加できます。

for i in 1 2 3   # if you want more retries, add more numbers here
do
    curl -f icanhazip.com > ip-location1.txt
    if [ $? -eq 0 ] && [ -s ip-location1.txt ]
    then
        break
    fi
    # if we get here, the current attempt failed
    sleep 5  # be nice and wait a bit before retrying instead of spamming the service
done
if [ ! -s ip-location1.txt ]
then
    echo "i cannot haz ip."
    # do whatever you want to do in case of all the retries fail
fi

おすすめ記事