translate done: 20160117 How to use curl command with proxy username-password on Linux- Unix.md

This commit is contained in:
darksun 2018-01-08 16:25:06 +08:00
parent 7c8404ca6a
commit 6203fd62b7

View File

@ -1,8 +1,8 @@
translating by lujun9972
How to use curl command with proxy username/password on Linux/ Unix
如何让 curl 命令通过代理访问
======
My sysadmin provided me the following proxy details:
我的系统管理员给我提供了如下代理信息:
```
IP: 202.54.1.1
Port: 3128
@ -10,15 +10,14 @@ Username: foo
Password: bar
```
The settings worked perfectly with Google Chrome and Firefox browser. How do I use it with the curl command? How do I tell the curl command to use my proxy settings from Google Chrome browser?
该设置在 Google Chrome 和 Firefox 浏览器上很容易设置。但是我要怎么把它应用到 curl 命令上呢?我要如何让 curl 命令使用我在 Google Chrome 浏览器上的代理设置呢?
很多 Linux 和 Unix 命令行工具(比如 curl 命令wget 命令lynx 命令等)使用名为 `http_proxy``https_proxy``ftp_proxy` 的环境变量来获取代理信息。它允许你通过代理服务器(使用或不使用用户名/密码都行)来连接那些基于文本的会话和应用。**本文就会演示一下如何让 curl 通过代理服务器发送 HTTP/HTTPS 请求。**
## 让 curl 命令使用代理的语法
Many Linux and Unix command line tools such as curl command, wget command, lynx command, and others; use the environment variable called http_proxy, https_proxy, ftp_proxy to find the proxy details. It allows you to connect text based session and applications via the proxy server with or without a userame/password. T **his page shows how to perform HTTP/HTTPS requests with cURL cli using PROXY server.**
## Unix and Linux curl command with proxy syntax
The syntax is:
语法为:
```
## Set the proxy address of your uni/company/vpn network ##
export http_proxy=http://your-ip-address:port/
@ -32,7 +31,7 @@ export https_proxy=https://user:password@your-proxy-ip-address:port/
```
Another option is to pass the -x option to the curl command. To use the specified proxy:
另一种方法是使用 curl 命令的 -x 选项:
```
curl -x <[protocol://][user:password@]proxyhost[:port]> url
--proxy <[protocol://][user:password@]proxyhost[:port]> url
@ -40,9 +39,9 @@ curl -x <[protocol://][user:password@]proxyhost[:port]> url
-x http://user:password@Your-Ip-Here:Port url
```
## Linux use curl command with proxy
## 在 Linux 上的一个例子
First set the http_proxy:
首先设置 `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/
@ -51,7 +50,7 @@ export https_proxy=$http_proxy
curl -I https://www.cyberciti.biz
curl -v -I https://www.cyberciti.biz
```
Sample outputs:
输出为:
```
* Rebuilt URL to: www.cyberciti.biz/
@ -98,44 +97,43 @@ Connection: keep-alive
* Connection #0 to host 10.12.249.194 left intact
```
In this example, I'm downloading a pdf file:
本例中,我来下载一个 pdf 文件:
```
$ export http_proxy="vivek:myPasswordHere@10.12.249.194:3128/"
$ curl -v -O http://dl.cyberciti.biz/pdfdownloads/b8bf71be9da19d3feeee27a0a6960cb3/569b7f08/cms/631.pdf
```
OR use the -x option:
也可以使用 -x 选项:
```
curl -x 'http://vivek:myPasswordHere@10.12.249.194:3128' -v -O https://dl.cyberciti.biz/pdfdownloads/b8bf71be9da19d3feeee27a0a6960cb3/569b7f08/cms/631.pdf
```
Sample outputs:
[![Fig.01: curl in action \(click to enlarge\)][1]][2]
输出为:
![Fig.01curl in action \(click to enlarge\)][1]
## How to use the specified proxy server with curl on Unix
## Unix 上的一个例子
```
$ curl -x http://prox_server_vpn:3128/ -I https://www.cyberciti.biz/faq/howto-nginx-customizing-404-403-error-page/
```
## How to use socks protocol?
## socks 协议怎么办呢?
The syntax is same:
语法也是一样的:
```
curl -x socks5://[user:password@]proxyhost[:port]/ url
curl --socks5 192.168.1.254:3099 https://www.cyberciti.biz/
```
## How do I configure and setup curl to permanently use a proxy connection?
## 如何让代理设置永久生效?
Update/edit your ~/.curlrc file using a text editor such as vim:
编辑 ~/.curlrc 文件:
`$ vi ~/.curlrc`
Append the following:
添加下面内容:
```
proxy = server1.cyberciti.biz:3128
proxy-user = "foo:bar"
```
Save and close the file. Another option is create a bash shell alias in your ~/.bashrc file:
保存并关闭该文件。另一种方法是在你的 `~/.bashrc` 文件中创建一个别名:
```
## alias for curl command
## set proxy-server and port, the syntax is
@ -143,7 +141,7 @@ Save and close the file. Another option is create a bash shell alias in your ~/.
alias curl = "curl -x server1.cyberciti.biz:3128"
```
Remember, the proxy string can be specified with a protocol:// prefix to specify alternative proxy protocols. Use socks4://, socks4a://, socks5:// or socks5h:// to request the specific SOCKS version to be used. No protocol specified, http:// and all others will be treated as HTTP proxies. If the port number is not specified in the proxy string, it is assumed to be 1080. The -x option overrides existing environment variables that set the proxy to use. If there's an environment variable setting a proxy, you can set proxy to "" to override it. See curl command man page [here for more info][3].
记住,代理字符串中可以使用 `protocol//` 前缀来指定不同的代理协议。使用 `socks4//``socks4a//``socks5// `或者 `socks5h//` 来指定使用的 SOCKS 版本。若没有指定协议或者 `http//` 表示 HTTP 协议。若没有指定端口号则默认为 1080。-x 选项的值要优先于环境变量设置的值。若不想走代理,而环境变量总设置了代理,那么可以通过设置代理为 "" 来覆盖环境变量的值。[详细信息请参阅 curl 的 man 页 ][3]。
--------------------------------------------------------------------------------