如何在控制台中检查Internet连接?

有没有一种简单的方法可以从控制台检查Internet连接? 我试图在shell脚本中玩。 我似乎有一个想法是wget --spider http://www.google.co.in/并检查HTTP响应代码以解释Internet连接是否正常工作。 但我认为必须有一个简单的方法,而不需要检查永远不会崩溃的网站;)

编辑:似乎可以有很多因素可以单独检查,好事。 我目前的意图是检查我的博客是否已关闭。 我已经设置了cron来检查它每分钟。 为此,我正在检查wget –spider的HTTP响应代码到我的博客。 如果它不是200,它通知我(我相信这将比仅仅ping它更好,因为该网站可能负载很重,可能会超时或响应很晚)。 昨天,我的互联网出现了一些问题。 局域网连接正常,但我无法访问任何网站。 所以我继续收到通知,因为脚本在wget响应中找不到200。 现在我想确保它在我有互联网连接时显示通知。

因此,检查DNS和LAN连接对我来说有点矫枉过正,因为我没有那么具体的需要来弄清楚它是什么问题。 所以你建议我怎么做?

这是我的脚本,以继续检查我的博客的停机时间:

 #!/bin/bash # Sending the output of the wget in a variable and not what wget fetches RESULT=`wget --spider http://blog.ashfame.com 2>&1` FLAG=0 # Traverse the string considering it as an array of words for x in $RESULT; do if [ "$x" = '200' ]; then FLAG=1 # This means all good fi done if [ $FLAG -eq '0' ]; then # A good point is to check if the internet is working or not # Check if we have internet connectivity by some other site RESULT=`wget --spider http://www.facebook.com 2>&1` for x in $RESULT; do if [ "$x" = '200' ]; then FLAG=1 # This means we do have internet connectivity and the blog is actually down fi done if [ $FLAG -eq '1' ]; then DISPLAY=:0 notify-send -t 2000 -i /home/ashfame/Dropbox/Ubuntu/icons/network-idle.png "Downtime Alert!" "http://blog.ashfame.com/ is down." fi fi exit 

这样我只需要在我的博客响应代码出现问题时检查互联网连接。 它有点沉重(因为我没有使用ping)但不应该给出任何误报。 对? 另外,我怎么能每次随机将ping到其他网站,比如facebook,google,yahoo等。另外(我试图避免任何I / O)我可以写一个日志文件,通过它我可以检查停机检查的数量和然后跳过进一步的检查,直到现场停工或导致更长的检查(10分钟而不是每分钟)。 你怎么看?

检查特定网站是否已启动

首先,提供多种良好的在线监测服务。 为了选择一个,Pingdom包括用于监控一个目标的免费帐户。 (免责声明:我以任何方式与Pingdom有关联)。

其次,将wget --spider用于自己的脚本是个好主意。 当您的计算机关闭或DNS服务器不工作时,您会得到一些误报。 检查返回代码是实现此目的的简单方法:

 wget --spider --quiet http://example.com if [ "$?" != 0 ]; then echo "Website failed!" | mail -s "Website down" your_email@provider.tld fi 

再次,这种方法存在缺点。 如果您的提供商已缓存您的DNS记录,但DNS服务器已关闭,则其他人无法访问您的网站,即使监控显示一切正常。 您可以使用host编写简短的解决方法,例如host example.com 。 如果DNS服务器没有响应,即使OpenDNS或您自己的提供商的DNS服务器正常工作,也会返回错误。

检查互联网是否正常工作

在每种情况下都没有简单的方法来处理这个问题。

例如,您可以在多个众所周知的站点(例如www.google.com,facebook.com和ping.funet.fi)上运行ping -c1并检查返回代码以确定是否可以访问任何目标。 您可以使用变量$?自动检查返回代码$? 。 参数-c1将ping数据包的数量限制为一个。

当存在重定向所有ping和HTTP请求的登录网关时,您可能会遇到某些公共wifis的问题。 如果是这样,即使您无法访问任何其他站点,也可能会获得ping响应和非错误HTTP状态代码。

如果要检查电缆状态,可以使用

 sudo ethtool eth0 

从输出(摘录):

 Speed: 1000Mb/s Duplex: Full Port: Twisted Pair Link detected: yes 

然而,这并不能说明你是否真的有连接,只是电缆是否已连接而另一端是否有连接。

我正在使用这种方法……

 if [[“$(ping -c 1 8.8.8.8 | grep'100%packet loss')”!=“”]]; 然后
    回声“互联网不存在”
     1号出口
其他
    回声“互联网存在”
     wget www.site.com
科幻

我刚刚用过

 nm-online 

暂停直到网络管理员出现网络连接。 工作得很好。 你也可以做其他事情。

通常使用像nagios这样的监视工具来检查站点是否已启动。 这将持续监控站点,并可以通知您停机。

当检查Internet是否从命令行运行时,我运行了许多步骤:

  • 检查互联网是ping google.com (检查DNS和已知的可访问站点)。
  • 检查网站是否使用wgetw3m来获取页面。

如果互联网没有向外诊断。

  • 检查网关是可ping的。 (检查网关地址的ifconfig。)
  • 检查DNS服务器是否可ping。 (检查/etc/resolv.conf中的地址。)
  • 检查防火墙是否阻塞。 (检查/ var / log / syslog作为我的日志块。)

如果互联网已启动,但网站已关闭,请检查w3m http://isup.me/example.comexample.com替换为显示的网站。 如果您没有安装w3m浏览器,请使用wget,lynx或您可用的命令行浏览器。

这是我用来测试互联网连接的shell srcript:

alarm.sh

 #! /bin/bash if curl --silent --head http://www.google.com/ | egrep "20[0-9] Found|30[0-9] Found" >/dev/null then echo Internet status: OK else echo Internet status: ERROR mpg321 alarm.mp3 &> /dev/null fi sleep 60 clear ./alarm.sh 

您需要安装curl和mpg321

 sudo apt-get install curl mpg321 

如果您需要声音警报function,则需要在同一文件夹中将.mp3声音文件重命名为alarm.mp3。 最后根据您的需要配置网站URL和egrep。

检查互联网是否正常工作

 sudo nm-tool | grep "State: connected" | wc -l 

从输出(摘录):

 1 #System is connected to internet 

然而,正如其他人所说,这并不能说明你是否真的有连接。 例如。 您可以连接到路由器,路由器不一定连接到互联网。

我需要同一个问题的帮助,我这样得到了答案:

 echo $(nm-online) $? connection problems 

检查互联网是否正常工作并非易事。 ping是ICMP,因此即使Web代理已关闭,它也可能正常工作。 如果您使用IP进行测试,则DNS会发生类似情况。

由于我的互联网连接不稳定,我创建了这个脚本( 基于此脚本),它会发出轻柔的和弦声音来呼叫我,并在互联网连接恢复时使用Ubuntu通知:

 #!/bin/bash hash play 2>&- || { echo >&2 "I require «play» but it's not installed. Try apt-get install sox. Aborting."; exit 1; } WGET="`which wget`" URL="http://www.google.com" delay=3; noConnectionMessage="No connection, trying again in $delay seconds..."; connectionMessage="WE HAVE INTERNET! :) Trying again in $delay seconds..."; echo "INTERNET TESTER: Type Ctrl+C to quit"; rm --force /tmp/index.site function playSound { time=1; # Time played if [ `sox --version| grep -oP "v[0-9.]+" | sed "s/[v.]//g"` -lt 1431 ]; then #analize sox version, if greater than v14.3.1 can use pluck play -q -n synth $time sine; else play -q -n synth $time pluck $1; #for i in G4 G4 G4 E4;do play -n synth 0.1 pluck $i repeat 2;done # You can play with something like this also :) (first three notes from Beethoven 5th symphony) fi } while [ 1 -eq 1 ]; do $WGET -q --tries=10 --timeout=2 $URL -O /tmp/index.site &> /dev/null if [ ! -s /tmp/index.site ];then echo $noConnectionMessage; #playSound E2 sleep 1; else #~ zenity --warning --text "ADDRESS is back" notify-send -i "notification-network-wireless-full" "Connection state:" "$connectionMessage"; echo $connectionMessage; playSound E3 fi sleep $delay; done exit 0; 

我正在寻找一个可以连续测试我的互联网连接的脚本,只要我的服务器启动就会启动,我就这样做了:

  1. 首先,安装fping

     apt-get install fping 
  2. 使用以下内容在/etc/init.d文件夹中创建一个init脚本(我称之为testcon)

     #!/bin/bash PIDFILE=/var/run/testcon.pid . /lib/lsb/init-functions case "$1" in start) log_daemon_msg "Starting internet connection tester" /etc/init.d/testcond & echo $! > $PIDFILE log_end_msg $? ;; stop) log_daemon_msg "Stopping internet connection tester" PID=$(cat $PIDFILE) kill -9 "$PID" log_end_msg $? ;; *) echo "Usage: /etc/init.d/testcon {start|stop}" exit 1 ;; esac exit 0 
  3. 使用以下内容在/etc/init.d文件夹中创建脚本(我称之为testcond)

     #echo Computer starting or testing daemon init while [ "$itest" == "" ] do #wait 5 seconds sleep 5 itest=$(fping 8.8.8.8 | grep alive) done date | mail -s "Server is up and Internet is online" your_email@gmail.com #loop forever while [ "1" == "1" ] do itest=$(fping 8.8.8.8 | grep alive) #if internet is down if [ "$itest" == "" ] then #echo Internet is down #log time it was found down current_time=$(date) echo "Internet was found down @ $current_time" >> /mnt/data/Server/internet_log.txt #loop until it is back while [ "$itest" == "" ] do #wait 60 seconds sleep 60 itest=$(fping 8.8.8.8 | grep alive) # test the internet done #when it is back current_time=$(date) echo "Internet is back @ $current_time" >> /mnt/data/Server/internet_log.txt body=$(tail -2 /mnt/data/Server/internet_log.txt) echo "$body" | mail -s "Internet is back online" your_email@gmail.com fi #echo Internet is online #wait 60 seconds sleep 60 done 
  4. 然后我运行以下命令添加到启动:

     sudo update-rc.d testcon defaults sudo update-rc.d testcon enable 
  5. 重启

我的Broadcom BCM94331CD芯片面临着随机丢弃的Wi-Fi连接。 如果Internet Wi-Fi连接已启动并运行,我设计了以下bash脚本(无限循环)检查每秒。 这里提出的其他命令行将在关闭之前等待至少5到10秒,状态即ping 。 与此同时,我也在寻找一个明确的解决方案。

从askubuntu的结果来看,当我使用firmware-b43-installerb43-fwcutter ,我将不得不清除bcmwl-kernel-source包。

感谢大家的建议(上图)。

此脚本需要添加到启动程序中,并且不需要super user权限也可以运行。

 #!/bin/bash # # Check if Wi-Fi is disabled eg 'out of range' # If so quickly re-enable Wi-Fi # Due to a problem with Broadcom proprietary driver in package firmware-b43-installer # Script is part of Start-Up programs # Stored in /usr/local/bin/ # Infinite loop checking every second if Wi-Fi is up and running while true; do # https://askubuntu.com/questions/27954/how-can-i-check-internet-connectivity-in-a-console if [[ $(nmcli -f STATE -tg) != 'connected' ]]; then # Disable and Re-Enable Wi-Fi as the Wi-Fi is now 'out of range' (disconnected) nmcli radio wifi off nmcli radio wifi on fi sleep 1 done