1. 程式人生 > >如何讓 curl 命令通過代理訪問 | Linux 中國

如何讓 curl 命令通過代理訪問 | Linux 中國

640?wx_fmt=jpeg&wxfrom=5&wx_lazy=1我要如何讓 curl 命令使用我在 Google Chrome 瀏覽器上的代理設定呢?-- Vivek Gite
本文導航◈ 讓 curl 命令使用代理的語法14%◈ 在 Linux 上的一個例子27%◈ Unix 上的一個例子64%◈ socks 協議怎麼辦呢?68%◈ 如何讓代理設定永久生效?71%編譯自 | https://www.cyberciti.biz/faq/linux-unix-curl-command-with-proxy-username-password-http-options/ 
 作者 | Vivek Gite
 譯者 | lujun9972

我的系統管理員給我提供瞭如下代理資訊:

  1. IP

    :202.54.1.1

  2. Port:3128

  3. Username: foo

  4. Password: bar

該設定在 Google Chrome 和 Firefox 瀏覽器上很容易設定。但是我要怎麼把它應用到 curl命令上呢?我要如何讓 curl 命令使用我在 Google Chrome 瀏覽器上的代理設定呢?

很多 Linux 和 Unix 命令列工具(比如 curl 命令,wget 命令,lynx 命令等)使用名為 http_proxyhttps_proxyftp_proxy 的環境變數來獲取代理資訊。它允許你通過代理伺服器(使用或不使用使用者名稱/密碼都行)來連線那些基於文字的會話和應用。

本文就會演示一下如何讓 curl

 通過代理伺服器傳送 HTTP/HTTPS 請求。

讓 curl 命令使用代理的語法

語法為:

  1. ##Set the proxy address of your uni/company/vpn network ##

  2. export http_proxy=http://your-ip-address:port/

  3. ## http_proxy with username and password

  4. export http_proxy=http://user:[email protected]:port/

  5. ## HTTPS version ##

  6. export https_proxy=https://your-ip-address:port/

  7. export https_proxy=https://user:[email protected]:port/

另一種方法是使用 curl 命令的 -x 選項:

  1. curl -x <[protocol://][user:[email protected]]proxyhost[:port]> url

  2. --proxy <[protocol://][user:[email protected]]proxyhost[:port]> url

  3. --proxy http://user:[email protected]:Port url

  4. -x http://user:[email protected]:Port url

在 Linux 上的一個例子

首先設定 http_proxy

  1. ## proxy server,202.54.1.1, port:3128, user: foo, password: bar ##

  2. export http_proxy=http://foo:[email protected]:3128/

  3. export https_proxy=$http_proxy

  4. ##Use the curl command ##

  5. curl -I https://www.cyberciti.biz

  6. curl -v -I https://www.cyberciti.biz

輸出為:

  1. *Rebuilt URL to: www.cyberciti.biz/

  2. *Trying202.54.1.1...

  3. *Connected to 1202.54.1.1(202.54.1.1) port 3128(#0)

  4. *Proxy auth usingBasicwith user 'foo'

  5. > HEAD HTTP://www.cyberciti.biz/ HTTP/1.1

  6. >Host: www.cyberciti.biz

  7. >Proxy-Authorization:Basic x9VuUml2xm0vdg93MtIz

  8. >User-Agent: curl/7.43.0

  9. >Accept:*/*

  10. > Proxy-Connection: Keep-Alive

  11. >

  12. < HTTP/1.1 200 OK

  13. HTTP/1.1 200 OK

  14. < Server: nginx

  15. Server: nginx

  16. < Date: Sun, 17 Jan 2016 11:49:21 GMT

  17. Date: Sun, 17 Jan 2016 11:49:21 GMT

  18. < Content-Type: text/html; charset=UTF-8

  19. Content-Type: text/html; charset=UTF-8

  20. < Vary: Accept-Encoding

  21. Vary: Accept-Encoding

  22. < X-Whom: Dyno-l1-com-cyber

  23. X-Whom: Dyno-l1-com-cyber

  24. < Vary: Cookie

  25. Vary: Cookie

  26. < Link: <http://www.cyberciti.biz/wp-json/>; rel="https://api.w.org/"

  27. Link: <http://www.cyberciti.biz/wp-json/>; rel="https://api.w.org/"

  28. < X-Frame-Options: SAMEORIGIN

  29. X-Frame-Options: SAMEORIGIN

  30. < X-Content-Type-Options: nosniff

  31. X-Content-Type-Options: nosniff

  32. < X-XSS-Protection: 1; mode=block

  33. X-XSS-Protection: 1; mode=block

  34. < X-Cache: MISS from server1

  35. X-Cache: MISS from server1

  36. < X-Cache-Lookup: MISS from server1:3128

  37. X-Cache-Lookup: MISS from server1:3128

  38. < Connection: keep-alive

  39. Connection: keep-alive

  40. <

  41. * Connection #0 to host 10.12.249.194 left intact

本例中,我來下載一個 pdf 檔案:

  1. $ export http_proxy="vivek:[email protected]:3128/"

  2. $ curl -v -O http://dl.cyberciti.biz/pdfdownloads/b8bf71be9da19d3feeee27a0a6960cb3/569b7f08/cms/631.pdf

也可以使用 -x 選項:

  1. curl -x 'http://vivek:[email protected]:3128'-v -O https://dl.cyberciti.biz/pdfdownloads/b8bf71be9da19d3feeee27a0a6960cb3/569b7f08/cms/631.pdf

輸出為:

0?wx_fmt=jpeg

Fig.01:curl in action \(click to enlarge\)

Unix 上的一個例子

  1. $ curl -x http://prox_server_vpn:3128/ -I https://www.cyberciti.biz/faq/howto-nginx-customizing-404-403-error-page/

socks 協議怎麼辦呢?

語法也是一樣的:

  1. curl -x socks5://[user:[email protected]]proxyhost[:port]/ url

  2. curl --socks5 192.168.1.254:3099 https://www.cyberciti.biz/

如何讓代理設定永久生效?

編輯 ~/.curlrc 檔案:

  1. $ vi~/.curlrc

新增下面內容:

  1. proxy = server1.cyberciti.biz:3128

  2. proxy-user ="foo:bar"

儲存並關閉該檔案。另一種方法是在你的 ~/.bashrc 檔案中建立一個別名:

  1. ##aliasfor curl command

  2. ##set proxy-server and port, the syntax is

  3. ##alias curl="curl -x {your_proxy_host}:{proxy_port}"

  4. alias curl ="curl -x server1.cyberciti.biz:3128"

記住,代理字串中可以使用 protocol:// 字首來指定不同的代理協議。使用 socks4://socks4a://socks5://或者 socks5h:// 來指定使用的 SOCKS 版本。若沒有指定協議或者使用 http:// 表示 HTTP 協議。若沒有指定埠號則預設為 1080-x 選項的值要優先於環境變數設定的值。若不想走代理,而環境變數總設定了代理,那麼可以通過設定代理為空值("")來覆蓋環境變數的值。詳細資訊請參閱 curl的 man 頁 [1]

via: https://www.cyberciti.biz/faq/linux-unix-curl-command-with-proxy-username-password-http-options/

作者:Vivek Gite[3] 譯者:lujun9972 校對:wxy

本文由 LCTT 原創編譯,Linux中國 榮譽推出

LCTT 譯者0?wx_fmt=pnglujun9972 ? ? ? ?共計翻譯:73 篇貢獻時間:52 天

推薦文章

< 左右滑動檢視相關文章 >

0?wx_fmt=png0?wx_fmt=png0?wx_fmt=png0?wx_fmt=png0?wx_fmt=gif0?wx_fmt=gif

點選圖片、輸入文章 ID 或識別二維碼直達

原文連結請訪問“原文連結”獲得可點選的文內連結、全尺寸原圖和相關文章。

相關推薦

如何 curl 命令通過代理訪問 | Linux 中國

我要如何讓 curl 命令使用我在 Google Chrome 瀏覽器上的代理設定呢?-- Vi

如何 curl 命令通過代理訪問

如何讓 curl 命令通過代理訪問 Linux.中國 - 開源中文社群 2018-01-18  8909 閱讀 技術 我的系統管理員給我提供瞭如下代理資訊: IP: 202.54.1.1 Port: 3128 Username: foo Password

菜鳥們,通過這幾個樣例,包你學會 uptime 命令的使用方法 | Linux 中國

望名生義。uptime 命令告訴你係統啟動了(執行了)多長時間。這是語法:-- Himanshu Arora 本文導航◈ Linux uptime 命令 09%◈ Q1、怎樣使用 uptime 命令 22%◈ Q2、怎樣以更人

通過nginx訪問linux目錄

http { ...... autoindex on; autoindex_exact_size off; autoindex_localtime on; server { listen 80; ......

curl命令支援http2

CentOS 安裝新版libcurl的yum源 rpm -ivh http://mirror.city-fan.org/ftp/contrib/yum-repo/city-fan.org-release-1-13.rhel6.noarch.rpm

關於windows通過cuteftp訪問linux的問題

最近在學習嵌入式linux開發,想通過cuteftp工具(cuteftp中文版)訪問linux上的ftp伺服器,實現windows和linux的檔案的共享。在訪問linux時,出現不能訪問ftp伺服器和windows不能上傳檔案到linux上。 windows訪問redha

C# webBrowser 通過代理訪問網頁

改變webBrowser1的代理 程式碼 #region 改變代理 [DllImport("wininet.dll", SetLastError = true)] private static extern bool InternetSe

Windows下通過VNC訪問Linux伺服器(視覺化介面)

問題: 實習公司用的是伺服器,我們只能使用SecureCRT通過指令進行類似linux系統下終端的指令操作,如下圖: 而上傳檔案就要通過WinSCP: 一般的工作,不需要視覺化介面也能完成 可是,當我們有些操作需要顯示出來,就有問題了,例如我們訓練完s

使用 Ansible 你的系統管理自動化 | Linux 中國

遠端管理工具有很多,SaltStack、Puppet、Chef,以及 Ansible 都是很流行

Mac sudo命令代理

在Mac OS中,在終端中利用命令 export http_proxy=http://127.0.0.1:1087;export https_proxy=http://127.0.0.1:1087; 讓當前終端走Shadowsockes的代理,但是通過s

Linux curl命令模擬Http請求(get/post),繫結host訪問

linux curl模擬http請求傳送資料: ①get方式提交資料: curl -G -d “name=value&name2=value2” http://www.baidu.com

命令列瀏覽器 curl 命令詳解,Linux訪問url地址

http://hi.baidu.com/oyvfhp/item/747ecf16e4619c3ab83180ffCURL --- 命令列瀏覽器這東西現在已經是蘋果機上內建的命令列工具之一了,可見其魅力之一斑1)二話不說,先從這裡開始吧!curl http://www.yaho

Linux某個路徑下的命令隨處可以訪問

對於沒有新增到環境變數PATH中的路徑,這樣的路徑下的命令,一般執行時都要指定路徑。如果想讓它們不帶路徑就可以訪問到,可以這樣做: (如果/etc/profile 是隻讀的,要加w屬性)假設這些命令位於 /usr/local/webserver/rabbitmq/sbin

linux中shell curl命令獲取http狀態碼

linux中shell curl命令獲取http狀態碼linux中shell curl命令獲取http狀態碼通過curl的-w參數我們可以自定義curl的輸出,%{http_code}代表http狀態碼 代碼如下 復制代碼# curl -I -m 10 -o /dev/null -s -w %{http_co

CentOS 安裝rz和sz命令 lrzsz(通過Xshell向Linux服務器上傳文件)

到你 dem zmodem .cn ftp linux install 自動 all lrzsz在liunx裏面可代替ftp上傳可下載。 lrzsz是一個unix通信套件提供的X,Y和ZModem文件傳輸協議。 winddows需要向centos服務器上傳文件,可直接

CentOS7 nginx+tomcat實現代理訪問java web項目項目支持jsp和php

default fault text serve auto direct dep web項目 ffi 首先配置:nginx # cd /etc/nginx/conf.d/ # cp default.conf tomcat.conf # nano tomcat.co

Linux curl 命令下載文件

接口 mpat hello current 什麽 表示 發送 source binary 引用自http://blog.csdn.net/wh211212/article/details/54285921 命令:curl 在Linux中curl是一個利用URL規則在命令

lamp架構-訪問控制-禁止php解析、屏蔽curl命令訪問

屏蔽curl訪問 訪問控制 禁止php解析 網頁防止上傳木馬 lamp架構-訪問控制-禁止php解析 有的網站論壇開啟了上傳圖片功能,導致黑客木馬捆綁圖片上傳到服務器,通過訪問php來提升服務器後臺權限,這裏我們就需要將上傳圖片的目錄禁止訪問php文件,來達到防止黑客木馬上傳上來通過訪問web

Linux curl命令詳解

cookied current form eat -o party tp服務器 cap lin 命令:curl 在Linux中curl是一個利用URL規則在命令行下工作的文件傳輸工具,可以說是一款很強大的http命令行工具。它支持文件的上傳和下載,是綜合傳輸工具,但按傳統,