Linux 中 CURL常用命令详解

2019-09-23 09:22:29于海丽

 curl -z 21-Dec-11 http://www.example.com/yy.html

CURL授权

在访问需要授权的页面时,可通过-u选项提供用户名和密码进行授权

curl -u username:password URL
 # 通常的做法是在命令行只输入用户名,之后会提示输入密码,这样可以保证在查看历史记录时不会将密码泄露
4 curl -u username URL

从FTP服务器下载文件

CURL同样支持FTP下载,若在url中指定的是某个文件路径而非具体的某个要下载的文件名,CURL则会列出该目录下的所有文件名而并非下载该目录下的所有文件

 # 列出public_html下的所有文件夹和文件
 curl -u ftpuser:ftppass -O ftp://ftp_server/public_html/
 # 下载xss.php文件
 curl -u ftpuser:ftppass -O ftp://ftp_server/public_html/xss.php
上传文件到FTP服务器

通过 -T 选项可将指定的本地文件上传到FTP服务器上

# 将myfile.txt文件上传到服务器

curl -u ftpuser:ftppass -T myfile.txt ftp://ftp.testserver.com

# 同时上传多个文件

curl -u ftpuser:ftppass -T "{file1,file2}" ftp://ftp.testserver.com

# 从标准输入获取内容保存到服务器指定的文件中

curl -u ftpuser:ftppass -T - ftp://ftp.testserver.com/myfile_1.txt

获取更多信息

通过使用 -v 和 -trace获取更多的链接信息

通过字典查询单词

 # 查询bash单词的含义
 curl dict://dict.org/d:bash 
# 列出所有可用词典
 curl dict://dict.org/show:db
  # 在foldoc词典中查询bash单词的含义
curl dict://dict.org/d:bash:foldoc

为CURL设置代理

-x 选项可以为CURL添加代理功能

# 指定代理主机和端口
 curl -x proxysever.test.com:3128 http://google.co.in 

其他网站整理

保存与使用网站cookie信息

# 将网站的cookies信息保存到sugarcookies文件中
curl -D sugarcookies http://localhost/sugarcrm/index.php 
# 使用上次保存的cookie信息
 curl -b sugarcookies http://localhost/sugarcrm/index.php
传递请求数据

默认curl使用GET方式请求数据,这种方式下直接通过URL传递数据
可以通过 --data/-d 方式指定使用POST方式传递数据

 # GET
 curl -u username https://api.github.com/user?access_token=XXXXXXXXXX
 # POST
 curl -u username --data "param=value¶m=value" https://api.github.com
 # 也可以指定一个文件,将该文件中的内容当作数据传递给服务器端
 curl --data @filename https://github.api.com/authorizations

注:默认情况下,通过POST方式传递过去的数据中若有特殊字符,首先需要将特殊字符转义在传递给服务器端,如value值中包含有空格,则需要先将空格转换成%20,如:

curl -d "value%201" http://hostname.com