I am trying to test if proxy is working correctly using this script. Problem is that it no matter what ends by echo "Proxy is working" . Any ideas please ?
export http_proxy=' OUTPUT_FILE=/tmp/$$.txt wget -nv --proxy-user=test --proxy-password=test google.com > ${OUTPUT_FILE} 2>&1 grep -q '<H1>You cant use internet</H1>' ${OUTPUT_FILE} if [ "$?" -eq '0' ] then echo "Proxy isnt working. " | mail -s "Proxy isnt working" -r "No-reply<>" else echo "Proxy is working" fi rm -f /tmp/$$.txt 13 Answers
I have solved it this way :
export http_proxy=' URL=' wget -q --proxy-user=test --proxy-password=test --spider $URL if [ $? = 1 ] then STATUS= echo "Proxy isn't working" else STATUS="Proxy is working." fi echo $STATUS Although somewhat old question, but it may be still have some value to provide another answer. So here is mine:
set_proxies() { export http_proxy= export HTTP_PROXY=${http_proxy} export https_proxy= export HTTPS_PROXY=${https_proxy} export ftp_proxy= export FTP_PROXY=${ftp_proxy} export socks_proxy=socks://proxy.example.com:9999 export SOCKS_PROXY=${socks_proxy} export no_proxy=localhost,127.0.0.1,.docker.io,192.168.9.100 export NO_PROXY=${no_proxy} export ALL_PROXY_NO_FALLBACK=1 export all_proxy=socks5://proxy.example.com:9999 } URL=' curl -s -m 2 $URL > /dev/null if [ $? == 0 ] then STATUS="No Corporate Proxy" else set_proxies STATUS="Behind Corporate proxy" fi echo $STATUS The problem is because of the quotes around $? in the if check:
if [ "$?" -eq '0' ] When the grep didn't find the string you were looking for, the return code was 1. So this if check equates to:
if [ "1" -eq '0' ] which is always false, hence you get "Proxy is working".
Use
if [ $? -eq 0 ] instead.
0