如何在linux中使用bash脚本检查internet访问?
在我的学校,互联网是不可用的(每天晚上23:0以后,学校会关闭互联网,让我们上床睡觉>。使用超时选项如何在linux中使用bash脚本检查internet访问?,linux,bash,wifi,ping,Linux,Bash,Wifi,Ping,在我的学校,互联网是不可用的(每天晚上23:0以后,学校会关闭互联网,让我们上床睡觉>。使用超时选项-t: ping -q -t 5 -w1 -c1 8.8.8.8 t 使用wget: #!/bin/bash wget -q --tries=10 --timeout=20 --spider http://google.com if [[ $? -eq 0 ]]; then echo "Online" else echo "Offline" fi 安装fpin
-t
:
ping -q -t 5 -w1 -c1 8.8.8.8 t
使用wget:
#!/bin/bash
wget -q --tries=10 --timeout=20 --spider http://google.com
if [[ $? -eq 0 ]]; then
echo "Online"
else
echo "Offline"
fi
安装fping:>问题比ping少
fping google.com | grep alive
例如,使用:
#!/bin/bash
itest=$(fping google.com | grep alive)
while [ "$itest" == "" ]
do
sleep 5
itest=$(fping google.com | grep alive)
done
echo now online
使用上述示例,我编写了以下脚本来记录连接状态: 首先,将以下代码保存到
name.sh
文件中
#!/bin/bash
while true
do
wget -q --tries=10 --timeout=20 -O - http://google.com > /dev/null
if [[ $? -eq 0 ]]; then
echo $(date) "1" | tee -a log.csv
else
echo $(date) "0" | tee -a log.csv
fi
sleep 5
done
然后,在终端中执行
name.sh
文件,然后在同一文件夹的log.csv
中检查日志状态信息。不带wget
#!/bin/bash
echo -e "GET http://google.com HTTP/1.0\n\n" | nc google.com 80 > /dev/null 2>&1
if [ $? -eq 0 ]; then
echo "Online"
else
echo "Offline"
fi
享受;)如果学校实际上关闭了路由器,而不是将所有流量重定向到“你为什么不睡觉”页面,那么就不需要下载整个网页或发送HTTP头。你所要做的就是建立一个连接,检查是否有人在听
nc -z 8.8.8.8 53
这将输出“连接到8.8.8.8端口53[tcp/domain]成功!”,如果有人正在侦听,则返回值0
如果要在shell脚本中使用它:
nc -z 8.8.8.8 53 >/dev/null 2>&1
online=$?
if [ $online -eq 0 ]; then
echo "Online"
else
echo "Offline"
fi
输出将产生smth,如:
bash-3.2$ ./internet_check.sh
UP 2016-05-10T23:23:06BST 4
DOWN 2016-05-10T23:23:25BST 19
UP 2016-05-10T23:23:32BST 7
一行末尾的数字显示前一状态的持续时间,即在单独的bash脚本中向上19秒,向下7秒Reliable oldping
:
把这个放在一个单独的脚本上。它将处理不同的网络情况,如(1)未连接到网络,(2)连接到网络但无法访问互联网(或至少谷歌),以及(3)连接到互联网
您可以稍后使用脚本的退出代码检查连接,例如:
~$ script-name && echo online || echo offline
我决定结合上面的一些内容,以便稍后创建一个显示起伏及其持续时间的绘图:
#!/bin/bash
#
# pinger is a bash shell script that monitors the network
# status every 15 seconds and records if it is up '1' or down '0'
# into the file log.csv from whence it may be plotted.
#
# author: J. W. Wooten, Ph.D.
# since: 11/12/2019
# version: 1.0
#
TIMESTAMP=`date +%s`
while [ 1 ]
do
nc -z -w 5 8.8.8.8 53 >/dev/null 2>&1
online=$?
TIME=`date +%s`
if [ $online -eq 0 ]; then
echo "`date +%Y-%m-%d_%H:%M:%S_%Z` 1 $(($TIME-$TIMESTAMP))" | tee -a log.csv
else
echo "`date +%Y-%m-%d_%H:%M:%S_%Z` 0 $(($TIME-$TIMESTAMP))" | tee -a log.csv
fi
TIMESTAMP=$TIME
sleep 15
done;
每15秒输出一个csv文件。使用Excel或数字,您可以读取文件并创建一个绘图,该绘图将显示internet不可用的时间以及持续时间。如果它改变了你的睡眠时间间隔,那么它就是在花时间尝试连接。希望能够添加发送给我一个文本时,它检测到网络关闭下一步的能力。多亏了以上所有内容。Majal上述解决方案的一个变体就是测试ping的返回代码,如果站点响应,返回0;如果没有响应,返回1;如果网络无法访问,返回2
ping -c 1 -t 5 8.8.8.8 2&>1
rc=$?
[[ $rc -eq 0 ]] && { echo "Connected to the Internet" ; exit 0 ; } \
|| [[ $rc -eq 1 ]] && { echo "No reply from Google DNS" ; exit 1 ; } \
|| [[ $rc -eq 2 ]] && { echo "Network unreachable" ; exit 2 ; }
使用ping的优点是不需要下载任何内容,提高了测试速度。此解决方案的一个问题是,某些网络可能会阻止传出的ping(我的大学有这样做的著名记录)。不过OP似乎不是这样。@umlaeute,在这种情况下,您可以使用
wgethttp://google.com
甚至(更好更简单)curl
它工作得很好。非常感谢你!我的旧版本缺少一个--trys
,因此它不起作用。就我个人而言,我通过使这个wget-q--trys=10--timeout=20-O-http://google.com >/dev/null
。这就丢弃了输出,这意味着文件不会到处乱放,写入权限也不会有任何问题。您确实应该使用--spider
选项,因为它将发送httpHEAD
请求,而不是httpGET
请求。现在,在这种情况下,你要对照google.com进行检查,这是一个相当轻量级的页面,所以它可能是正常的。但是作为一般规则,如果您只是想检查某个东西是否可用而不实际下载它,那么您应该使用HEAD
请求。我已经相应地添加了答案。这有助于这是最快的方法,它ping dns服务器而不是获取谷歌的网站数据。竖起大拇指。在这里使用一个超时可能很好nc-z-w 5 8.8.8.8 53>/dev/null 2>&1欢迎使用堆栈溢出!虽然这段代码可以回答这个问题,但最好包含一些上下文,解释它是如何工作的以及何时使用它。从长远来看,只使用代码的答案是没有用的。在几分钟内有很多起伏。当DropBox在线并且可以上网时。。。查看他们提到的nc
将比ping更好,因为相当多的主机也禁用了ICMP。我不知道如何使用上面的代码获得持续时间。我试过了,每一行的“持续时间”都单调地增长。不是每次通过循环时都必须重置时间戳吗?fping
需要在运行示例之前安装。
#!/bin/bash
#
# pinger is a bash shell script that monitors the network
# status every 15 seconds and records if it is up '1' or down '0'
# into the file log.csv from whence it may be plotted.
#
# author: J. W. Wooten, Ph.D.
# since: 11/12/2019
# version: 1.0
#
TIMESTAMP=`date +%s`
while [ 1 ]
do
nc -z -w 5 8.8.8.8 53 >/dev/null 2>&1
online=$?
TIME=`date +%s`
if [ $online -eq 0 ]; then
echo "`date +%Y-%m-%d_%H:%M:%S_%Z` 1 $(($TIME-$TIMESTAMP))" | tee -a log.csv
else
echo "`date +%Y-%m-%d_%H:%M:%S_%Z` 0 $(($TIME-$TIMESTAMP))" | tee -a log.csv
fi
TIMESTAMP=$TIME
sleep 15
done;
ping -c 1 -t 5 8.8.8.8 2&>1
rc=$?
[[ $rc -eq 0 ]] && { echo "Connected to the Internet" ; exit 0 ; } \
|| [[ $rc -eq 1 ]] && { echo "No reply from Google DNS" ; exit 1 ; } \
|| [[ $rc -eq 2 ]] && { echo "Network unreachable" ; exit 2 ; }