最新消息:天气越来越冷,记得加一件厚衣裳

curl命令通过代理访问资源

Linux w3sun 515浏览 0评论

生产环境中IDC托管的服务器或者云主机通常不具备访问外网权限,部署开源软件一般都是在赋予外网权限的开发机(网关机)下载后再分发到相应集群,操作起来非常不方便。如果开发机启用了http或者socket代理服务,那么我们可以很方便地使用curl命令通过代理服务器访问外网资源下载所需软件或者补丁包。

系统管理员提供的代理详细信息如下:

IP: 202.54.1.1
Port: 3128
Username: foo
Password: bar

该代理设置在Google Chrome 和 Firefox 浏览器工作的很好。但是,我要如何将其应用到curl命令上呢?我要如何让curl命令使用我在谷歌浏览器上的代理设置呢?

诸如curl 、wget 、lynx 命令等许多 Linux 和 Unix 命令行工具都会使用名为 http_proxy、https_proxy、ftp_proxy 的环境变量来查找代理详细信息。 它允许用户通过代理服务器(使用或者不使用用户名/密码)连接基于文本的会话和应用。本文将演示代理服务器环境下如何通过curl命令行界面执行 HTTP/HTTPS 请求。

Unix和Linux下curl命令设置代理语法

语法如下:

## Set the proxy address of your uni/company/vpn network ## 
export http_proxy=http://your-ip-address:port/
 
## http_proxy with username and password 
export 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选项,该选项允许用户使用指定的代理设置:

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使用curl 命令实现代理访问演示

首先设置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.biz
curl -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 OK
HTTP/1.1 200 OK
< Server: nginx
Server: nginx
< Date: Sun, 17 Jan 2016 11:49:21 GMT
Date: Sun, 17 Jan 2016 11:49:21 GMT
< Content-Type: text/html; charset=UTF-8
Content-Type: text/html; charset=UTF-8
< Vary: Accept-Encoding
Vary: Accept-Encoding
< X-Whom: Dyno-l1-com-cyber
X-Whom: Dyno-l1-com-cyber
< Vary: Cookie
Vary: 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: SAMEORIGIN
X-Frame-Options: SAMEORIGIN
< X-Content-Type-Options: nosniff
X-Content-Type-Options: nosniff
< X-XSS-Protection: 1; mode=block
X-XSS-Protection: 1; mode=block
< X-Cache: MISS from server1
X-Cache: MISS from server1
< X-Cache-Lookup: MISS from server1:3128
X-Cache-Lookup: MISS from server1:3128
< Connection: keep-alive
Connection: keep-alive
 
< 
* Connection #0 to host 10.12.249.194 left intact

在接下来这个例子中,我将下载一个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选项:

curl -x 'http://vivek:myPasswordHere@10.12.249.194:3128' -v -O https://dl.cyberciti.biz/pdfdownloads/b8bf71be9da19d3feeee27a0a6960cb3/569b7f08/cms/631.pdf

示例输出:

如何在Unix上通过curl使用指定代理服务器

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

如何使用socks协议

语法和http/https代理是一样的:

curl -x socks5://[user:password@]proxyhost[:port]/ url
curl --socks5 192.168.1.254:3099 https://www.cyberciti.biz/

如何配置并设置curl使其代理永久生效

使用 vim 等文本编辑器更新/编辑~/.curlrc 文件:

$ vi ~/.curlrc

追加以下内容:

proxy = server1.cyberciti.biz:3128
proxy-user = "foo:bar"

保存并关闭文件。另一种方式是在 ~/.bashrc 文件中创建一个别名:

## 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://前缀来指定不同的代理协议。通过socks4://、socks4a://、socks5://或socks5h://指定所使用的 SOCKS 版本。未指定代理协议的情况下,http:// 和其他所有协议都将被默认视为 HTTP 代理。如果代理字符串中未指定端口号,则默认为 1080。-x 选项将会覆盖环境变量中现有的代理设置。如果环境变量中已经设置了代理,用户可以通过将代理设置为””来进行覆盖。更多信息请参阅curl命令手册

参考资料

在构思这篇文章的时候偶然间看到一篇通俗易懂且实操性比较高的英文博文,随决定直接将该文翻译成中文分享给大家。

作者:Vivek Gite

原文地址:https://www.cyberciti.biz/faq/linux-unix-curl-command-with-proxy-username-password-http-options/

转载请注明:雪后西塘 » curl命令通过代理访问资源

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址