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秒,命令正常執行結果為1指stdout標准輸出,
rem 就是控制台輸出;2指stderr錯誤輸出,這里 2>$1表示重定向到1,
rem 然后再重定向到null,linux下是/dev/null,windows下是nul。 
curl -x "http://192.168.0.6:8080" --connect-timeout 1 "http://www.baidu.com" 2>$1>nul
rem errorlevel是個系統變量指上一條語句的執行結果,成功時等於0
echo '結果%errorlevel%'

echo '訪問https://google.com'
curl -x "http://192.168.0.6:8080" --connect-timeout 1 "https://google.com" 2>$1>nul
rem 這里是56,完整信息是 curl: (56) Proxy CONNECT aborted due to timeout
echo '結果%errorlevel%'

IF not  errorlevel 0 then(
set  location='CN'
)   
echo '當前位置:%location%'    

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM