![트래픽 전달 스크립트](https://linux55.com/image/136741/%ED%8A%B8%EB%9E%98%ED%94%BD%20%EC%A0%84%EB%8B%AC%20%EC%8A%A4%ED%81%AC%EB%A6%BD%ED%8A%B8.png)
프록시 서버가 트래픽을 전달하는지 확인하기 위해 테스트하는 스크립트를 작성하려고 합니다. "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