我有一个bash脚本,用于检查CHECKURL变量是否有响应。如果url无效或不存在,则脚本立即退出并回显消息"strong";无效URL">
我有一个问题,网址https://valid-url-sample.com是一个有效的url,但是我的IP在负载均衡器上被拒绝,因为它只响应来自特定IP的443请求。结果是脚本一直在运行,直到它要求我控制+c为止。我希望该脚本能够处理这种情况,并响应"有效但不可访问">,我还在wget命令上添加了超时,但仍然没有成功。有什么想法吗?
脚本
#!/bin/bash
TARGET=$1
CHECKURL=$(wget -S -t 1 --timeout=600 --spider https://${TARGET} --no-check-certificate 2>&1 | awk '/HTTP// {print $2}')
if [[ ${CHECKURL} ]]; then
echo "VALID URL"
else
if [[ $? != 0 ]]; then
echo "NOT A VALID URL"
else
echo "VALID BUT NOT REACHABLE"
fi
fi
示例脚本执行
[root@ip-192-69-69-69 ~]# ./script.sh google.com
VALID URL
您可能想要使用这样的日志文件:。
#!/bin/bash
TARGET="$1"
if wget -S -t 1 --timeout=600 --spider https://"${TARGET}" --no-check-certificate > log.txt 2>&1; then
echo "VALID URL"
else
error="$(awk 'BEGIN { IGNORECASE=1 } /( failed| error| bad|unable | invalid| unrecognized)/ { err=$0 } END { if(match(err,/http://: Invalid host name/)) err=""; printf("%s",err) }' log.txt)"
# Connection reset by peer, Connection timed out, Network is unreachable.
if printf '%s' "$error" | grep -qiE '( peer| timed| unreachable)'; then
echo "VALID BUT UNREACHABLE"
# ERROR 403: Access denied/Forbidden.
elif printf '%s' "$error" | grep -q ' 403'; then
echo "FORBIDDEN"
# Not Found.
elif printf '%s' "$error" | grep -q ' 404'; then
echo "NOT FOUND"
# Fatal error.
else
echo "FATAL ERROR"
echo "$error"
fi
exit 1 # Exit the script
fi
# Delete the temporary log file.
# rm -f log.txt