如何讓curl命令通過代理訪問
我的系統(tǒng)管理員給我提供了如下代理信息:
IP: 202.54.1.1Port: 3128Username: fooPassword: bar
該設(shè)置在 Google Chrome 和 Firefox 瀏覽器上很容易設(shè)置。但是我要怎么把它應(yīng)用到 curl 命令上呢?我要如何讓 curl 命令使用我在 Google Chrome 瀏覽器上的代理設(shè)置呢?
很多 Linux 和 Unix 命令行工具(比如 curl 命令,wget 命令,lynx 命令等)使用名為 http_proxy,https_proxy,ftp_proxy 的環(huán)境變量來獲取代理信息。它允許你通過代理服務(wù)器(使用或不使用用戶名/密碼都行)來連接那些基于文本的會話和應(yīng)用。
本文就會演示一下如何讓 curl 通過代理服務(wù)器發(fā)送 HTTP/HTTPS 請求。
讓 curl 命令使用代理的語法
語法為:
## Set the proxy address of your uni/company/vpn network ##export http_proxy=http://your-ip-address:port/## http_proxy with username and passwordexport http_proxy=http://user:password@your-proxy-ip-address:port/## HTTPS version ##export https_proxy=https://your-ip-address:port/export https_proxy=https://user:password@your-proxy-ip-address:port/
另一種方法是使用 curl 命令的 -x 選項(xiàng):
curl -x <[protocol://][user:password@]proxyhost[:port]> url--proxy <[protocol://][user:password@]proxyhost[:port]> url--proxy http://user:password@Your-Ip-Here:Port url-x http://user:password@Your-Ip-Here:Port url
在 Linux 上的一個(gè)例子
首先設(shè)置 http_proxy:
## proxy server, 202.54.1.1, port: 3128, user: foo, password: bar ##export http_proxy=http://foo:bar@202.54.1.1:3128/export https_proxy=$http_proxy## Use the curl command ##curl -I https://www.cyberciti.bizcurl -v -I https://www.cyberciti.biz
輸出為:
* Rebuilt URL to: www.cyberciti.biz/* Trying 202.54.1.1...* Connected to 1202.54.1.1 (202.54.1.1) port 3128 (#0)* Proxy auth using Basic with user 'foo'> HEAD HTTP://www.cyberciti.biz/ HTTP/1.1> Host: www.cyberciti.biz> Proxy-Authorization: Basic x9VuUml2xm0vdg93MtIz> User-Agent: curl/7.43.0> Accept: */*> Proxy-Connection: Keep-Alive>< HTTP/1.1 200 OKHTTP/1.1 200 OK< Server: nginxServer: nginx< Date: Sun, 17 Jan 2016 11:49:21 GMTDate: Sun, 17 Jan 2016 11:49:21 GMT< Content-Type: text/html; charset=UTF-8Content-Type: text/html; charset=UTF-8< Vary: Accept-EncodingVary: Accept-Encoding< X-Whom: Dyno-l1-com-cyberX-Whom: Dyno-l1-com-cyber< Vary: CookieVary: Cookie< Link: <http://www.cyberciti.biz/wp-json/>; rel="https://api.w.org/"Link: <http://www.cyberciti.biz/wp-json/>; rel="https://api.w.org/"< X-Frame-Options: SAMEORIGINX-Frame-Options: SAMEORIGIN< X-Content-Type-Options: nosniffX-Content-Type-Options: nosniff< X-XSS-Protection: 1; mode=blockX-XSS-Protection: 1; mode=block< X-Cache: MISS from server1X-Cache: MISS from server1< X-Cache-Lookup: MISS from server1:3128X-Cache-Lookup: MISS from server1:3128< Connection: keep-aliveConnection: keep-alive<* Connection #0 to host 10.12.249.194 left intact
本例中,我來下載一個(gè) pdf 文件:
$ export http_proxy="vivek:myPasswordHere@10.12.249.194:3128/"$ curl -v -O http://dl.cyberciti.biz/pdfdownloads/b8bf71be9da19d3feeee27a0a6960cb3/569b7f08/cms/631.pdf
也可以使用 -x 選項(xiàng):
curl -x 'http://vivek:myPasswordHere@10.12.249.194:3128' -v -O https://dl.cyberciti.biz/pdfdownloads/b8bf71be9da19d3feeee27a0a6960cb3/569b7f08/cms/631.pdf
輸出為:

Fig.01:curl in action \(click to enlarge\)
Unix 上的一個(gè)例子
$ curl -x http://prox_server_vpn:3128/ -I https://www.cyberciti.biz/faq/howto-nginx-customizing-404-403-error-page/
socks 協(xié)議怎么辦呢?
語法也是一樣的:
curl -x socks5://[user:password@]proxyhost[:port]/ urlcurl --socks5 192.168.1.254:3099 https://www.cyberciti.biz/
如何讓代理設(shè)置永久生效?
編輯 ~/.curlrc 文件:
$ vi ~/.curlrc
添加下面內(nèi)容:
proxy = server1.cyberciti.biz:3128proxy-user = "foo:bar"
保存并關(guān)閉該文件。另一種方法是在你的 ~/.bashrc 文件中創(chuàng)建一個(gè)別名:
## alias for curl command## set proxy-server and port, the syntax is## alias curl="curl -x {your_proxy_host}:{proxy_port}"alias curl = "curl -x server1.cyberciti.biz:3128"
記住,代理字符串中可以使用 protocol:// 前綴來指定不同的代理協(xié)議。使用 socks4://,socks4a://,socks5://或者 socks5h:// 來指定使用的 SOCKS 版本。若沒有指定協(xié)議或者使用 http:// 表示 HTTP 協(xié)議。若沒有指定端口號則默認(rèn)為 1080。-x 選項(xiàng)的值要優(yōu)先于環(huán)境變量設(shè)置的值。若不想走代理,而環(huán)境變量總設(shè)置了代理,那么可以通過設(shè)置代理為空值("")來覆蓋環(huán)境變量的值。詳細(xì)信息請參閱 curl 的 man 頁 。 















 
 
 



 
 
 
 