トラフィック転送スクリプト

トラフィック転送スクリプト

プロキシサーバーがトラフィックを転送していることを確認するためにテストするスクリプトを作成しようとしています。 「httpie」ツールを実装するために以下のコードを書いていますが、まだ問題があります。誰かがこのコードを見て、何が問題なのかを教えてもらえますか?コードはすべてのIPがトラフィックを転送していることを確認するためにすべてのプロキシIPアドレスを確認する必要があります。送信する必要があります。この特定のエラーが返されました。

#!/bin/bash
proxy_targets="http://10.1.1.4:3128 http://10.1.1.5:3128 http://10.1.1.6:3128"

failed_hosts=""

for i in $proxy_targets
do

if http --check-status --ignore-stdin --timeout=2.5 --proxy=http:$i HEAD www.msftncsi.com/ncsi.txt &> /dev/null; then
    echo 'OK!'
else
    case $? in
        2) echo 'Request timed out!'| mail -s "The following WSA has failed to pass traffic with the following error" [email protected]
rvard.edu  ;;
        3) echo 'Unexpected HTTP 3xx Redirection!'| mail -s "The following WSA has failed to pass traffic with the following error" [email protected]  ;;
        4) echo 'HTTP 4xx Client Error!'| mail -s "The following WSA has failed to pass traffic with the following error" [email protected]  ;;
        5) echo 'HTTP 5xx Server Error!'| mail -s "The following WSA has failed to pass traffic with the following error" [email protected]  ;;
        6) echo 'Exceeded --max-redirects=<n> redirects!'| mail -s "The following WSA has failed to pass traffic with the following error" [email protected]  ;;
        *) echo 'Other Error!'| mail -s "The following WSA has failed to pass traffic with the following error" [email protected] ;;
    esac
fi
done;

ベストアンサー1

IPが保存されているので、i次のように電子メールに追加できます。

#!/bin/bash

proxy_targets=( 'http://10.1.1.4:3128' 'http://10.1.1.5:3128' 'http://10.1.1.6:3128' )
failed_hosts=

for i in "${proxy_targets[@]}"
do
    exit_code=$(http --check-status --ignore-stdin --timeout=2.5 "--proxy=$i" HEAD www.msftncsi.com/ncsi.txt &> /dev/null; echo $?)
    if ((exit_code==0))
    then
        echo 'OK!'
    else
        ip=${i#http://}     # Removes http:// from variable
        ip=${ip%:[0-9]*}    # Removes port from the end
        case $exit_code in
            2)  echo 'Request timed out!' | \
                mail -s "The following WSA has failed to pass traffic: ${ip}, with the following error: $exit_code" [email protected]  
            ;;
            3)  echo 'Unexpected HTTP 3xx Redirection!' | \
                mail -s "The following WSA has failed to pass traffic: ${ip}, with the following error: $exit_code" [email protected]  
            ;;
            4)  echo 'HTTP 4xx Client Error!' | \
                mail -s "The following WSA has failed to pass traffic: ${ip}, with the following error: $exit_code" [email protected]  
            ;;
            5)  echo 'HTTP 5xx Server Error!' | \
                mail -s "The following WSA has failed to pass traffic: ${ip}, with the following error: $exit_code" [email protected]  
            ;;
            6)  echo 'Exceeded --max-redirects=<n> redirects!' | \
                mail -s "The following WSA has failed to pass traffic: ${ip}, with the following error: $exit_code" [email protected]  
            ;;
            *)  echo 'Other Error!' | \
                mail -s "The following WSA has failed to pass traffic: ${ip}, with the following error: $exit_code" [email protected] 
            ;;
        esac
    fi
done
  • proxy_targets私は配列を置いた。
  • --proxy=$i重複するhttp://内容を引用して削除しました。
  • 変数の割り当て後も保持されるように、終了コードを変数に保存します。
  • ipパラメータ拡張を使用して、現在のアドレスからhttp://とポートを削除する必要があります。
  • メールの件名にip&を追加しました。exit_code

おすすめ記事