方式一
http://www.13sy.com/server/linux/2022/0428/2239.html
#!/bin/bash
#检测网络链接畅通
function network()
{
#超时时间
local timeout=1
#目标网站
local target=www.baidu.com
#获取响应状态码
local ret_code=`curl -I -s --connect-timeout ${timeout} ${target} -w %{http_code} | tail -n1`
if [ "x$ret_code" = "x200" ]; then
#网络畅通
return 1
else
#网络不畅通
return 0
fi
return 0
}
network
if [ $? -eq 0 ];then
echo "网络不畅通,请检查网络设置!"
exit -1
fi
echo "网络畅通,你可以上网冲浪!"
exit 0
方式二
#!/bin/bash
HOST="www.google.com" # 可以更改为任何你想要检查的主机
if ping -c 3 $HOST > /dev/null; then
echo "网络连接正常"
else
echo "网络连接失败"
fi
0 条评论