URL检测脚本
URL检测脚本
用途: 使用curl检测url,输出相关指标
提示,复制以下代码,粘贴到notepad++编辑器中,建议使用UTF8编码,同时选择 编辑--文档格式转换--转为Unix(LF),以免脚本在linux运行会报错;
#!/bin/bash
#===============================================================================
# copyright by hwb
# date:2020-11-4
# USAGE: ./get_url_status.sh <url>
# 用途: 使用curl检测url,输出相关指标
#===============================================================================
#传参
url="https://www.baidu.com"
date=`date +"%Y-%m-%d-%H:%M:%S"`
#调用函数库
[ -f /etc/init.d/functions ] && source /etc/init.d/functions
export PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
source /etc/profile
#Require root to run this script.
[ $(id -u) -gt 0 ] && echo "请用root用户执行此脚本!" && exit 1
#用第一个参数指定服务器目录
if [ -n "$1" ];then
url=$1
fi
function get_url_status(){
echo ""
echo -e "\033[33m*************************检测URL状态:${url}*************************\033[0m"
echo ""
echo -e "\033[36m Usage: $0 需检测URL地址[默认为https://www.baidu.com] \033[0m"
echo ""
#测试URL
echo "[${date}] >>> curl测试访问[${url}]的统计数据如下:"
curl -L -w '
HTTP返回码:\t%{http_code}
返回内容大小:\t%{size_download}
重定向次数:\t%{num_redirects}
域名解析时长:\t%{time_namelookup}
建立链接时长:\t%{time_connect}
开始传输时长:\t%{time_starttransfer}
总时长:\t%{time_total}
' -o /dev/null -s "${url}"
wget --spider -q -o /dev/null --tries=1 -T 5 ${url}
if [ $? -eq 0 ] ;then
echo ""
echo -e "\033[32m 服务器访问[${url}]地址测试正常! \033[0m"
else
echo ""
echo -e "\033[31m 服务器访问[${url}]地址测试异常! \033[0m"
fi
echo -e "\033[33m*********************完成${url}地址检测*****************************\033[0m"
echo ""
}
get_url_status
运行结果截图:

脚本备份下载:

共有 0 条评论