如何在linux中使用bash脚本检查互联网访问?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17291233/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to check internet access using bash script in linux?
提问by Libin Wen
In my school, the internet is not available(every night after 23:0 the school will kill the internet, to put us in bed >..<), then the ping will never stop, though I have used the parameter ping -w1 ...
.
在我的学校,互联网不可用(每天晚上23:0之后学校会杀死互联网,让我们睡觉>..<),然后ping永远不会停止,尽管我使用了参数ping -w1 ...
。
That is, when I use: ping -q -w1 -c1 8.8.8.8
to check if the internet is up/down, It will be there without any output and doesn't exit, just like I am using a single cat
.
也就是说,当我使用:ping -q -w1 -c1 8.8.8.8
检查互联网是否启动/关闭时,它将在那里没有任何输出并且不会退出,就像我使用单个cat
.
Can you understand my question??? 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 to use?)
你能理解我的问题吗???我不知道为什么会这样,但我认为问题与school-internet-service 有关。有什么建议吗?(我认为wget
可能是一个不错的选择,但如何使用?)
采纳答案by Atropo
Using wget:
使用 wget:
#!/bin/bash
wget -q --tries=10 --timeout=20 --spider http://google.com
if [[ $? -eq 0 ]]; then
echo "Online"
else
echo "Offline"
fi
回答by suspectus
Use the timeout option -t
:
使用超时选项-t
:
ping -q -t 5 -w1 -c1 8.8.8.8 t
回答by Lizen Kohlhaas
Install fping: > less problem then ping.
安装 fping:> 比 ping 问题少。
fping google.com | grep alive
to use for example like:
使用例如:
#!/bin/bash
itest=$(fping google.com | grep alive)
while [ "$itest" == "" ]
do
sleep 5
itest=$(fping google.com | grep alive)
done
echo now online
回答by Cristóbal Ganter
Using the example above, I wrote this script to log the state of your connection: https://gist.github.com/cganterh/ffc2fffa8263857cbece
使用上面的例子,我写了这个脚本来记录你的连接状态:https: //gist.github.com/cganterh/ffc2fffa8263857cbece
First, save the following code into a name.sh
file.
首先,将以下代码保存到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
Then, execute name.sh
file in terminal, then check the log state information in log.csv
of the same folder.
然后,name.sh
在终端中执行文件,然后检查log.csv
同一文件夹中的日志状态信息。
回答by user3439968
Without wget
没有 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
Enjoy ;)
享受 ;)
回答by Andrew
If the school actually turns off their router instead of redirecting all traffic to a "why aren't you in bed" page, then there's no need to download an entire web page or send HTTP headers. All you have to do is just make a connection and check if someone's listening.
如果学校实际上关闭了他们的路由器而不是将所有流量重定向到“你为什么不在床上”页面,那么就不需要下载整个网页或发送 HTTP 标头。您所要做的就是建立连接并检查是否有人在听。
nc -z 8.8.8.8 53
This will output "Connection to 8.8.8.8 port 53 [tcp/domain] succeeded!" and return a value of 0 if someone's listening.
这将输出“连接到 8.8.8.8 端口 53 [tcp/domain] 成功!” 如果有人在听,则返回值 0。
If you want to use it in a shell script:
如果要在 shell 脚本中使用它:
nc -z 8.8.8.8 53 >/dev/null 2>&1
online=$?
if [ $online -eq 0 ]; then
echo "Online"
else
echo "Offline"
fi
回答by Cyril R
#!/bin/bash
INTERNET_STATUS="UNKNOWN"
TIMESTAMP=`date +%s`
while [ 1 ]
do
ping -c 1 -W 0.7 8.8.4.4 > /dev/null 2>&1
if [ $? -eq 0 ] ; then
if [ "$INTERNET_STATUS" != "UP" ]; then
echo "UP `date +%Y-%m-%dT%H:%M:%S%Z` $((`date +%s`-$TIMESTAMP))";
INTERNET_STATUS="UP"
fi
else
if [ "$INTERNET_STATUS" = "UP" ]; then
echo "DOWN `date +%Y-%m-%dT%H:%M:%S%Z` $((`date +%s`-$TIMESTAMP))";
INTERNET_STATUS="DOWN"
fi
fi
sleep 1
done;
the output will produce smth like:
输出将产生 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
the number in the end of a line shows duration of previous state, i.e. 19 up, 7 secs down
一行末尾的数字表示前一状态的持续时间,即 19 上,7 秒下
回答by Majal
Reliable old ping
in a separate bash script:
ping
在单独的 bash 脚本中可靠旧:
#!/bin/bash
ipaddr='8.8.8.8' # Google's public DNS server
[[ -z `ping -c1 $ipaddr |& grep -o 'Network is unreachable'` ]] || exit 1
[[ -z `ping -c3 $ipaddr |& grep -o '100% packet loss'` ]] && exit 0 || exit 1
Put this on a separate script. It will handle different network situationsas (1) not being connected to a network, (2) connected to the network but cannot access the internet (or at least Google), and (3) connected to the internet.
把它放在一个单独的脚本上。它将处理不同的网络情况,例如(1)未连接到网络,(2)连接到网络但无法访问互联网(或至少是谷歌),以及(3)连接到互联网。
You may later use the exit code
of the script to check connectivity, e.g.
您可以稍后使用exit code
脚本的 来检查连接性,例如
~$ script-name && echo online || echo offline
回答by John Wooten
I decided to combine a few of the above so I could later create a plot showing ups, downs, and their durations:
我决定结合上面的一些,以便我以后可以创建一个显示起伏和持续时间的图:
#!/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;
this outputs to a csv file every 15 seconds. Using Excel or Numbers, you can read the file and create a plot which will show when internet was not available and also the duration. If it changes from your sleep interval, then it is spending time trying to connect. Hope to add the ability to send me a text when it detects network is down next. Thanks to all above.
这每 15 秒输出到一个 csv 文件。使用 Excel 或 Numbers,您可以读取文件并创建一个图表,该图表将显示互联网何时不可用以及持续时间。如果它从您的睡眠间隔发生变化,那么它正在花时间尝试连接。希望添加在接下来检测到网络中断时向我发送文本的功能。感谢以上所有。