AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / ubuntu / 问题 / 27954
Accepted
Ashfame
Ashfame
Asked: 2011-02-25 12:42:35 +0800 CST2011-02-25 12:42:35 +0800 CST 2011-02-25 12:42:35 +0800 CST

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

  • 772

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

编辑:似乎有很多因素可以单独检查,好事。我目前的意图是检查我的博客是否已关闭。我已经设置了 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 分钟而不是每分钟)。你怎么看?

command-line
  • 12 12 个回答
  • 380448 Views

12 个回答

  • Voted
  1. Best Answer
    Olli
    2011-02-25T12:47:41+08:002011-02-25T12:47:41+08:00

    检查特定网站是否已启动

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

    其次,wget --spider用于您自己的脚本是一个好主意。当您的计算机停机或 DNS 服务器不工作时,您会得到一些误报。检查返回码是实现这一点的直接方法:

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

    再一次,这种方法也有缺点。如果您的提供商已经缓存了您的 DNS 记录,但 DNS 服务器已关闭,那么即使监控表明一切正常,其他人也无法访问您的站点。host例如,您可以编写简短的解决方法host example.com <your dns server IP>。如果 DNS 服务器没有响应,这将返回错误,即使 OpenDNS 或您自己的提供商的 DNS 服务器工作正常。

    检查互联网是否正常工作

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

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

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

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

    sudo ethtool eth0
    

    从输出(摘录):

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

    但是,这并不能说明您是否真的有连接,而只是说明是否连接了电缆并且另一端是否有东西。

    • 36
  2. ntsecrets
    2016-12-26T15:03:41+08:002016-12-26T15:03:41+08:00

    我刚用

    nm-online
    

    它会暂停,直到网络管理器存在网络连接。工作得很好。你也可以用它做其他事情。

    • 12
  3. bais bais
    2013-10-15T01:18:55+08:002013-10-15T01:18:55+08:00

    我正在使用这种方法...

    if [[ "$(ping -c 1 8.8.8.8 | grep '100% 丢包')" != "" ]]; 然后
        echo "网络不存在"
        1号出口
    别的
        echo "网络存在"
        wget www.site.com
    菲
    
    • 9
  4. BillThor
    2011-02-28T08:20:02+08:002011-02-28T08:20:02+08:00

    检查站点是否已启动通常使用诸如nagios. 这将持续监控站点,并可以通知您中断。

    从命令行检查 Internet 是否已启动时,我执行了多个步骤:

    • 检查 Internet 是否已启动ping google.com(检查 DNS 和已知的可访问站点)。
    • 检查网站是否正在使用wget或w3m获取页面。

    如果 Internet 不上,则向外诊断。

    • 检查网关是否可 ping。(检查 ifconfig 的网关地址。)
    • 检查 DNS 服务器是否可 ping。(检查 /etc/resolv.conf 的地址。)
    • 检查防火墙是否阻塞。(在记录块时检查 /var/log/syslog。)

    如果 Internet 已启动但站点已关闭,请使用出现故障的站点进行w3m http://isup.me/example.com替换。example.com如果您没有安装 w3m 浏览器,请使用 wget、lynx 或任何可用的命令行浏览器。

    • 8
  5. 01BTC10
    2012-04-21T15:34:22+08:002012-04-21T15:34:22+08:00

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

    警报.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
    

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

    • 4
  6. abrahamcalf
    2019-01-28T23:12:35+08:002019-01-28T23:12:35+08:00

    我正在使用这种方法:

    if curl -s --head  --request GET www.google.com | grep "200 OK" > /dev/null ; then
        echo "Internet is present"
    else
        echo "Internet isn't present"
    fi
    

    它将尝试连接到一个知名网站,如果它可以连接到它,那么我们将假设有互联网。

    • 4
  7. Venkat Kotra
    2012-11-19T16:46:51+08:002012-11-19T16:46:51+08:00

    检查互联网是否正常工作

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

    从输出(摘录):

    1 #System is connected to internet
    

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

    • 3
  8. octius4u
    2014-04-23T23:21:11+08:002014-04-23T23:21:11+08:00

    我需要帮助解决同样的问题,我的答案是这样的:

    echo $(nm-online) $? connection problems
    
    • 0
  9. Rafael
    2014-05-08T16:00:48+08:002014-05-08T16:00:48+08:00

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

    1. 一、安装fping

      apt-get install fping
      
    2. 在 /etc/init.d 文件夹中创建一个初始化脚本,内容如下(我称之为 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" [email protected]
      
      #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" [email protected]
          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. 重启

    • 0
  10. pabloab
    2014-11-19T05:31:10+08:002014-11-19T05:31:10+08:00

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

    由于我的 Internet 连接不稳定,我创建了这个脚本(基于这个),它发出柔和的和弦声来呼叫我,并且在 Internet 连接恢复时使用 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;
    
    • 0

相关问题

  • 如何从命令行仅安装安全更新?关于如何管理更新的一些提示

  • 如何从命令行刻录双层 dvd iso

  • 如何从命令行判断机器是否需要重新启动?

  • 文件权限如何工作?文件权限用户和组

  • 如何在 Vim 中启用全彩支持?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何运行 .sh 脚本?

    • 16 个回答
  • Marko Smith

    如何安装 .tar.gz(或 .tar.bz2)文件?

    • 14 个回答
  • Marko Smith

    我需要什么命令来解压缩/提取 .tar.gz 文件?

    • 8 个回答
  • Marko Smith

    如何列出所有已安装的软件包

    • 24 个回答
  • Marko Smith

    无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗?

    • 25 个回答
  • Marko Smith

    如何使用命令行将用户添加为新的 sudoer?

    • 7 个回答
  • Marko Smith

    更改文件夹权限和所有权

    • 9 个回答
  • Martin Hope
    EmmyS 我需要什么命令来解压缩/提取 .tar.gz 文件? 2011-02-09 14:50:41 +0800 CST
  • Martin Hope
    Ivan 如何列出所有已安装的软件包 2010-12-17 18:08:49 +0800 CST
  • Martin Hope
    La Ode Adam Saputra 无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗? 2010-11-30 18:12:48 +0800 CST
  • Martin Hope
    David Barry 如何从命令行确定目录(文件夹)的总大小? 2010-08-06 10:20:23 +0800 CST
  • Martin Hope
    jfoucher “以下软件包已被保留:”为什么以及如何解决? 2010-08-01 13:59:22 +0800 CST
  • Martin Hope
    David Ashford 如何删除 PPA? 2010-07-30 01:09:42 +0800 CST

热门标签

10.10 10.04 gnome networking server command-line package-management software-recommendation sound xorg

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve