Home  >  Q&A  >  body text

网络 - 一台linux的机器, 如何简单的检测他是否能连接到外网?

大家都是用什么方法.

程序是python写的.

高洛峰高洛峰2744 days ago1092

reply all(14)I'll reply

  • PHP中文网

    PHP中文网2017-04-17 13:36:43

    Just use a socket to connect to port 80 of a large website.

    reply
    0
  • 迷茫

    迷茫2017-04-17 13:36:43

    1. ping may be blocked by the gateway. For example, here we have
    2 and 80 ports. Some networks that require authentication, such as ChinaUnicom’s wifi, will block all 80 ports before logging in. DNATThen it redirects to the login page, leading to misjudgment.
    3. 443 port. Generally certified networks will not DNAT this port. Even if it is done, it will cause a certificate error. There are not many domestic HTTPS websites, and Baidu will also have a certificate under normal circumstances. mistake.

    So it is recommended that wget/curl have a HTTP page with fixed content, such as http://example.com/ (this domain name really exists), and then judge the content. If different is detected, the user can also be prompted to "Your network" You may need to log in to use". And even if the judgment is wrong, the user experience will be the same.

    reply
    0
  • 阿神

    阿神2017-04-17 13:36:43

    ping www.baidu.com

    reply
    0
  • 天蓬老师

    天蓬老师2017-04-17 13:36:43

    How to detect whether he is connected to the external network? This problem itself is difficult to solve because you cannot define "connected to the external network".
    So there are only targeted approaches:

    This is what Nintendo 3DS does:
    get the domain name conntest.nintendowifi.net. If it can be opened and returns to This is test.html page, then 3DS will think that it is connected to the Internet.
    The entire process includes domain name resolution and http 80 requests.

    As for different environments, it is different. For example, if one day conntest.nintendowifi.net is blocked or banned, this method will not take effect.
    Some networks prohibit ping, and the ping method does not work.

    reply
    0
  • 迷茫

    迷茫2017-04-17 13:36:43

    Directly execute the command ping or other?

    reply
    0
  • PHPz

    PHPz2017-04-17 13:36:43

    ping command

    reply
    0
  • 迷茫

    迷茫2017-04-17 13:36:43

    For example, ping www.sina.com to check the packet loss rate.

    reply
    0
  • PHP中文网

    PHP中文网2017-04-17 13:36:43

    wget files, check size

    reply
    0
  • 迷茫

    迷茫2017-04-17 13:36:43

    Famous curl ifconfig.me

    reply
    0
  • 天蓬老师

    天蓬老师2017-04-17 13:36:43

    Just like surfing the Internet through a browser, we get the Baidu homepage page to see if there is the word html and the word Baidu:

    wget http://www.baidu.com/
    

    will get the html of an index. If there is success, if there is no success, there will be no network. Haha

    reply
    0
  • Cancelreply