如何在 Linux 上使用 Bash 脚本检查 Internet 访问? [英] How can I check Internet access using a Bash script on Linux?

查看:24
本文介绍了如何在 Linux 上使用 Bash 脚本检查 Internet 访问?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的学校,互联网是不可用的(每天晚上 23:00 之后学校会切断互联网连接,让我们睡觉 >..<),然后 ping 永远不会停止,虽然我有使用参数ping -w1 ....

In my school, the Internet is not available (every night after 23:00 the school will kill the Internet connection, to put us to bed >..<), Then the ping will never stop, though I have used the parameter ping -w1 ....

也就是说,当我使用:ping -q -w1 -c1 8.8.8.8 来检查互联网连接是否打开/关闭时,它将在那里没有任何输出并且不会退出,就像我使用单个 cat 一样.

That is, when I use: ping -q -w1 -c1 8.8.8.8 to check if the Internet connection is up/down, it will be there without any output and doesn't exit, just like I am using a single cat.

我不知道为什么会这样,但我认为问题与school-internet-service有关.有什么建议吗?(我认为 wget 可能是一个不错的选择,但我该如何使用它?)

I don't know why it's like this, But I think the problem is related to the school-internet-service. Any suggestion? (I think wget may be a good alternative, but how can I use it?)

推荐答案

使用 wget:

#!/bin/bash

wget -q --tries=10 --timeout=20 --spider http://google.com
if [[ $? -eq 0 ]]; then
        echo "Online"
else
        echo "Offline"
fi

这篇关于如何在 Linux 上使用 Bash 脚本检查 Internet 访问?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆