网络 - 一台linux的机器, 如何简单的检测他是否能连接到外网?
高洛峰
高洛峰 2017-04-17 13:34:43
0
14
1085

大家都是用什么方法.

程序是python写的.

高洛峰
高洛峰

拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...

reply all(14)
洪涛

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

迷茫

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.

阿神

ping www.baidu.com

Peter_Zhu

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.

迷茫

Directly execute the command ping or other?

PHPzhong

ping command

迷茫

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

Ty80

wget files, check size

迷茫

Famous curl ifconfig.me

Peter_Zhu

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

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!