curl --connect-timeout 判断国内外网络windows 批处理

时间:2023-03-10 00:49:29
curl --connect-timeout 判断国内外网络windows 批处理

1.下载编译curl

curl 下载地址:http://curl.haxx.se/download.html ,下载后解压到一个目录,使用vs开发者工具里的 “Visual Studio 命令提示(2010)” 打开命令行,

切换到源码目录 F:\curl-7.46.0\winbuild\,使用命令 nmake/f Makefile.vc mode=static 编译一下,在 F:\curl-7.46.0\builds\libcurl-vc-x86-release-static-ipv6-sspi-winssl\bin 目录下生成curl.exe 文件。

打开命令行切换到curl.exe目录,查看版本 curl -V

curl 7.46.0 (i386-pc-win32) libcurl/7.46.0 WinSSL WinIDN
Protocols: dict file ftp ftps gopher http https imap imaps ldap pop3 pop3s rtsp
smb smbs smtp smtps telnet tftp
Features: AsynchDNS IDN IPv6 Largefile SSPI Kerberos SPNEGO NTLM SSL

如果在使用curl时出现 curl: (1) Protocol https not supported or disabled in libcurl错误,首先检查一下用的curl.exe是否支持https协议.

2.知乎上有个根据curl --connect-timeout https://google.com 返回值判断是否是国内网络的代码段

链接地址:https://www.zhihu.com/question/30262900

# Guess your location, you know it.

location='oversea'

curl --connect-timeout 1 https://google.com 2>&1 >/dev/null

ret=$? if [ $ret -ne 0 ]; then

  location='cn'

else

.......

这里翻译一个window下批处理版本

@echo off
set location='oversea'
echo '当前位置:%location%'
echo '访问http://www.baidu.com' rem -x 设置代理
rem --connect-timeout 连接超时1秒,命令正常执行结果为1指stdout标准输出,
rem 就是控制台输出;2指stderr错误输出,这里 >$1表示重定向到1,
rem 然后再重定向到null,linux下是/dev/null,windows下是nul。
curl -x "http://192.168.0.6:8080" --connect-timeout "http://www.baidu.com" >$>nul
rem errorlevel是个系统变量指上一条语句的执行结果,成功时等于0
echo '结果%errorlevel%' echo '访问https://google.com'
curl -x "http://192.168.0.6:8080" --connect-timeout "https://google.com" >$>nul
rem 这里是56,完整信息是 curl: () Proxy CONNECT aborted due to timeout
echo '结果%errorlevel%' IF not errorlevel then(
set location='CN'
)
echo '当前位置:%location%'