Linux shell script to determine network flow
Introduction
When writing shell scripts, some features need to ensure that the network can access the server can perform down, so then you need to have a function to determine the state of the network server
We can curl
be accessed www.baidu.com, in order to determine whether the server network status can be unblocked
Network status determination
#!/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
Network status normal script execution results:
网络畅通,你可以上网冲浪!
Network state is not normal script execution results:
网络不畅通,请检查网络设置!