用于传递流量的脚本

用于传递流量的脚本

我正在尝试编写一个脚本来测试我们的代理服务器,看看它们是否正在传递流量。我在下面编写了这段代码来实现“httpie”工具,但仍然存在问题。有人可以看一下并让我知道这段代码有什么问题吗?该代码应该检查所有代理 IP 地址,以确保所有 IP 都在传递流量,如果每个代理返回 200,则代码应该退出,但如果出现问题,代码应该向我发送一封电子邮件,说明具体的 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到邮件主题。

相关内容