`

Linux命令详解 curl

 
阅读更多

 

Linux命令详解 curl

 

 

curl命令是个相当强大的命令,wiki见这里:http://en.wikipedia.org/wiki/CURL

官网见这里:http://curl.haxx.se/

手册见这里:http://curl.haxx.se/docs/manpage.html

wiki里面的内容比较单薄,大家可以参见官网和手册。不过我学习习惯man一下看看系统自带的帮助,其实这个帮助和手册上面的差不多。

 

man一下看下:

 

 curl - transfer a URL

 

 

这个命令的解释很简单,大家一看都明白,不过后面支持的功能一点都不简单。为什么功能强大的命令都是只有很简单的一句解释呢?强大勿需多言。

 

我们再看下curl的描述,其实就是curl的功能:

 

curl  is  a  tool  to  transfer data from or to a server, using one of the supported protocols (HTTP, HTTPS, FTP, FTPS,TFTP, DICT, TELNET, LDAP or FILE).  The command is designed to work without user interaction.
curl offers a busload of useful tricks like proxy support, user authentication, ftp upload,  HTTP  post,  SSL  (https:) connections,  cookies, file transfer resume and more. As you will see below, the amount of features will make your head spin!
curl is powered by libcurl for all transfer-related features. See libcurl(3) for details.

 

 

看完描述,就能知道curl命令的强大。

1:多协议支持

2:自动完成,勿需用户交互

3:很多有用的功能:cookiefileSSLftp

最后提出一点,curl命令需要libcurl支持,不过目前貌似机器上都有这个包,如果没有,自行下载安装。

 

 

由于curl的功能极其强大,命令极其繁复,参数极其之多,暂时无法做到面面俱到,本节就学习下curl的一般功能,更高级的功能,大家用到时可以再行学习。

 

curl的参数,只解释常用的参数,也只能解释常用的参数,不然本文的长度会严重超出限制的:

 

-A/--user-agent;指定用户代理发送User-Agent字段。有些服务器对该字段有校验。也可以使用-H/--header设置。多次设置使用最近的覆盖,有空格的话使用引号包括该字段

-b/--cookie <name=date>:添加cookie,会在请求头部Set-Cookie字段加上这个cookie

--basic:使用curl的HTTP基本的authentication功能

--compressed:response使用libcurl支持的压缩格式返回

--connect-timeout <seconds>:在curl连接过程使用,一旦连接,这个参数就没有用了

-c/--cookie-jar <file name>:将response的cookie写入到这个文件里,这个是个挺实用的功能,如果文件没有成功创建的话,curl命令也不会失败。

-d/--data <data>:将data以post请求格式提交给Server,模拟用户提交功能;所以希望是urlencoded过的,提交的content-type也会默认是application/x-www-form-urlencoded,标准的form提交。

--data-ascii <data>
--data-binary <data>:这个提供上个命令的更进一步的支持。

-D/--dump-header <file>:保存请求的response头

-e/--referer <URL>:请求中的Referer参数,当然也可以用-H/--header参数来设置

-F/--form <name=content>:form表单提交,提供更多的参数支持,支持文件、form、type设置等方式,这个比上面那个-d参数要更加详细。

--form-string <name=string>:更-F设置一样只是使用这种方式提交的内容,就是字面意思。举个例子说,提交的name以@、<、type开头的内容,就是按照字面意义一样,这些name在上面-F参数提交时时有特殊含义的、如@表示提交的是文件内容等。

-G/--get:强制使用Post请求的地方使用Get请求。如-d、-F等

-H/--header <header>:设置HTTP的头部,所有可以在头部发送的字段都可以在这里设置,也包括前面的-A、-e等

-i/--include:输出response的头部

--interface <name>:设置请求的网卡,ip等

-I/--head:只是请求URL的头部。实际上发出的命令是HEAD命令

--limit-rate <speed>:限速,这个是最大速度

--max-filesize <bytes>:告诉服务器最大的请求文件内容

-m/--max-time <seconds>:这个是最大请求时间,包括请求发出、连接、返回的时间。

-N/--no-buffer:禁用缓存

-o/--output <file>:将response结果输出到file中

-r/--range <range>:请求部分内容,格式比较简单,如0-499,500-999,-500,1000-等,也可以使用这些的组合,分段输出。

--retry <num>:如果发生错误,重连次数
--retry-delay <seconds>:重试延迟时间
--retry-max-time <seconds>:重试最大时间

-s/--silent:silent模式,隐藏进程输出或者是error输出

-S/--show-error:这个是用来修正-s参数的,如果出现错误,这个参数会输出错误信息

--stderr <file>:重定向所有输出到stderr

--tcp-nodelay:打开TCP_NODELAY参数,这个参数有什么用,大家自行查找

-T/--upload-file <file>:上传文件到远端地址URL

--trace <file>:这个是最全的功能,如果想看下连接交互,信息返回可以使用这个参数,这个和tcpdump命令类似。

--trace-ascii <file>:这个是信息格式的不同,没有上面的信息全,不过这个参数看起来要方便许多。

-v/--verbose:这个参数能看到更多的信息,和正常情况下比;当然不如trace参数,不过好处是可以即时看到,不用输出到文件。
 

 

 

这些命令看完,有没有感觉很curl命令的强大,其中的ftpauthenticationSSLProxy等功能都没有提及,如果需要这些功能,推荐看下curl的文档。

不过对于常用的getpost请求设置,应该足够应付工作中的问题了。

另外对于curl的高级功能,后面会考虑补充

 

学习完理论知识,我们来看下应用:

直接添加-v参数来看下输出结果:

 

curl -v www.baidu.com

 

添加-A参数,也就是改变user-agent:

 

curl -A "Molliza " -v www.baidu.com
 

添加-b参数,添加cookie

 

curl -A "Molliza" -b uid=182930SDFGISJ8SNCJSFSJ -v www.baidu.com

 

添加-d参数,提交post请求:

 

curl -A "Molliza" -b uid=182930SDFGISJ8SNCJSFSJ -v www.baidu.com/s -d "wd=123"
 

哎呦,501了,服务器不接受post请求查询,添加-G参数强制使用get请求

 

curl -A "Molliza" -b uid=182930SDFGISJ8SNCJSFSJ -v -G  www.baidu.com/s -d "wd=123"
 

-D参数保存response头部:

 

curl -A "Molliza" -b uid=182930SDFGISJ8SNCJSFSJ -v -G -D response.txt  www.baidu.com/s -d "wd=123"
 

添加--trace参数:

 

curl -A "Molliza" -b uid=182930SDFGISJ8SNCJSFSJ -v -G -D response.txt --trace trace.txt www.baidu.com/s -d "wd=123"
 

添加--trace-ascii参数:

 

curl -A "Molliza" -b uid=182930SDFGISJ8SNCJSFSJ -v -G -D response.txt --trace-ascii trace.txt www.baidu.com/s -d "wd=123"

 还是这个看起来比trace参数最方便

 

添加-v参数,可以直接在屏幕上看到连接的交互情况:

 

curl -A "Molliza" -b uid=182930SDFGISJ8SNCJSFSJ -v -G -D response.txt --trace-ascii trace.txt  -v www.baidu.com/s -d "wd=123"

 这个比trace-ascii参数要方便,当然信息不如上面的多啦

 

这些参数足够模拟浏览器的请求了,至少get和post请求没有问题。

 

 

 

最后列下curl命令的异常码,以供各位查询,其实这个在curl的help文档里面都有:

 

       1      Unsupported protocol. This build of curl has no support for this protocol.
       2      Failed to initialize.
       3      URL malformat. The syntax was not correct.
       4      URL user malformatted. The user-part of the URL syntax was not correct.
       5      Couldn?ˉt resolve proxy. The given proxy host could not be resolved.
       6      Couldn?ˉt resolve host. The given remote host was not resolved.
       7      Failed to connect to host.
       8      FTP weird server reply. The server sent data curl couldn?ˉt parse.
       9      FTP access denied. The server denied login or denied access to the particular resource or directory  you  wanted to reach. Most often you tried to change to a directory that doesn?ˉt exist on the server.
       10     FTP user/password incorrect. Either one or both were not accepted by the server.
       11     FTP weird PASS reply. Curl couldn?ˉt parse the reply sent to the PASS request.
       12     FTP weird USER reply. Curl couldn?ˉt parse the reply sent to the USER request.
       13     FTP weird PASV reply, Curl couldn?ˉt parse the reply sent to the PASV request.
       14     FTP weird 227 format. Curl couldn?ˉt parse the 227-line the server sent.
       15     FTP can?ˉt get host. Couldn?ˉt resolve the host IP we got in the 227-line.
       16     FTP can?ˉt reconnect. Couldn?ˉt connect to the host we got in the 227-line.
       17     FTP couldn?ˉt set binary. Couldn?ˉt change transfer method to binary.
       18     Partial file. Only a part of the file was transferred.
       19     FTP couldn?ˉt download/access the given file, the RETR (or similar) command failed.
       20     FTP write error. The transfer was reported bad by the server.
       21     FTP quote error. A quote command returned error from the server.
       22     HTTP  page  not  retrieved.  The  requested url was not found or returned another error with the HTTP error code being 400 or above. This return code only appears if -f/--fail is used.
       23     Write error. Curl couldn?ˉt write data to a local filesystem or similar.
       24     Malformed user. User name badly specified.
       25     FTP couldn?ˉt STOR file. The server denied the STOR operation, used for FTP uploading.
       26     Read error. Various reading problems.
       27     Out of memory. A memory allocation request failed.
       28     Operation timeout. The specified time-out period was reached according to the conditions.
       29     FTP couldn?ˉt set ASCII. The server returned an unknown reply.
       30     FTP PORT failed. The PORT command failed. Not all FTP servers support the PORT command,  try  doing  a  transfer using PASV instead!
       31     FTP couldn?ˉt use REST. The REST command failed. This command is used for resumed FTP transfers.
       32     FTP couldn?ˉt use SIZE. The SIZE command failed. The command is an extension to the original FTP spec RFC 959.
       33     HTTP range error. The range "command" didn?ˉt work.
       34     HTTP post error. Internal post-request generation error.
       35     SSL connect error. The SSL handshaking failed.
       36     FTP bad download resume. Couldn?ˉt continue an earlier aborted download.
       37     FILE couldn?ˉt read file. Failed to open the file. Permissions?
       38     LDAP cannot bind. LDAP bind operation failed.
       39     LDAP search failed.
       40     Library not found. The LDAP library was not found.
       41     Function not found. A required LDAP function was not found.
       42     Aborted by callback. An application told curl to abort the operation.
       43     Internal error. A function was called with a bad parameter.
       44     Internal error. A function was called in a bad order.
       45     Interface error. A specified outgoing interface could not be used.
       46     Bad password entered. An error was signaled when the password was entered.
       47     Too many redirects. When following redirects, curl hit the maximum amount.
       48     Unknown TELNET option specified.
       49     Malformed telnet option.
       51     The remote peer?ˉs SSL certificate wasn?ˉt ok
       52     The server didn?ˉt reply anything, which here is considered an error.
       53     SSL crypto engine not found
       54     Cannot set SSL crypto engine as default
       55     Failed sending network data
       56     Failure in receiving network data
       57     Share is in use (internal error)
       58     Problem with the local certificate
       59     Couldn?ˉt use specified SSL cipher
       60     Problem with the CA cert (path? permission?)
       61     Unrecognized transfer encoding
       62     Invalid LDAP URL
       63     Maximum file size exceeded
       64     Requested FTP SSL level failed
       65     Sending the data requires a rewind that failed
       66     Failed to initialise SSL Engine
       67     User, password or similar was not accepted and curl failed to login
       68     File not found on TFTP server
       69     Permission problem on TFTP server
       70     Out of disk space on TFTP server
       71     Illegal TFTP operation
       72     Unknown TFTP transfer ID
       73     File already exists (TFTP)
       74     No such user (TFTP)
       75     Character conversion failed
       76     Character conversion functions required
       XX     There will appear more error codes here in future releases. The existing ones are meant to never change.
 

本节完成,各位可以自行试用下这个命令。

 

 

 

 

 

 

 

 

 

 

分享到:
评论

相关推荐

    Linux curl命令参数详解.docx

    Linux curl命令参数详解.docx

    Linux中的curl命令详解

    大家应该都知道在Linux中curl是一个利用URL规则在命令行下工作的文件传输工具,可以说是一款很强大的http命令行工具。它支持文件的上传和下载,是综合传输工具...本文将详细介绍Linux中的curl命令,下面来一起看看吧。

    超全的curl命令详解文档集.zip

    linux curl是一个利用URL规则在命令行下工作的文件传输工具。它支持文件的上传和下载,所以是综合传输工具,但按传统,习惯称url为下载工具。  一,curl命令参数,有好多我没有用过,如果有误的地方,还请指正。 ...

    Linux 中 CURL常用命令详解

    主要介绍了Linux 中 CURL常用命令详解,需要的朋友可以参考下

    Linux Curl 命令满足你的工作需求!简直不要太香了

    curl命令详解

    Linux curl命令详解

    主要为大家详细介绍了Linux curl命令的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

    linux命令行下使用curl命令查看自己机器的外网ip

    您可能感兴趣的文章:Linux 中 CURL常用命令详解linux下为php添加curl扩展的方法Linux下命令行cURL的10种常见用法示例linux curl命令详解及实例分享Linux中curl命令和wget命令的使用介绍与比较linux shell中curl ...

    linux curl命令详解及实例分享

    curl命令使用了libcurl库来实现,libcurl库常用在C程序中用来处理HTTP请求,curlpp是libcurl的一个C++封装,这几个东西可以用在抓取网页、网络监控等方面的开发,而curl命令可以帮助来解决开发过程中遇到的问题。

    win10下用vs2019编译好的curl 64位库 版本7.84.0

    curl是一个利用URL语法在命令行下工作的文件传输工具,支持很多种http请求操作,详情可以参考Linux curl命令最全详解_Angel_CG的博客-CSDN博客_curl命令。curl现在在linux与win10都是有内置的,在命令行中可以直接...

    php中如何执行linux命令详解

    开发中遇到一种问题,需要在php函数中运行Linux系统代码,所以下面这篇文章主要给大家介绍了关于php中如何执行linux命令的相关资料,文中通过示例代码介绍的非常详细,需要的朋友可以参考下

    Linux下模拟http的get/post请求(curl or wget)详解

    Linux下模拟http的get/post请求(curl or wget)详解 背景 最近项目中需要测试接口,但是测试服务器通过堡垒机才能访问,暂时又没有通过Nginx进行转发,只好直接在Linux上模拟http请求进行测试。 方法 get请求 curl ...

    Linux中curl命令和wget命令的使用介绍与比较

    主要给大家介绍了Linux中curl命令和wget命令使用以及这两者之间的区别比较的相关资料,curl和wget命令都是Linux下的工具,可以用来下载文件。文中介绍的非常详细,相信对大家具有一定的参考价值,需要的朋友们下面来...

    Linux下安装PHP curl扩展的方法详解

    主要介绍了Linux下安装PHP curl扩展的方法,简单分析了Linux环境安装php的curl扩展具体步骤、相关命令与注意事项,需要的朋友可以参考下

    IdeaProjects.zip

    - Linux curl命令详解 命令:curl 在Linux中curl是一个利用URL规则在命令行下工作的文件传输工具,可以说是一款很强大的http命令行工具。它支持文件的上传

    详解几种Linux 查询外网出口IP命令的方法

    Curl 纯文本格式输出: curl icanhazip.com curl ifconfig.me curl curlmyip.com curl ip.appspot.com curl ipinfo.io/ip curl ipecho.net/plain curl www.trackip.net/i curl JSON格式输出: curl ipinfo.io/...

    linux中了minerd之后的完全清理过程(详解)

    一不小心装了一个Redis服务,开了一个全网的默认端口,一开始以为这台服务器没有公网ip,结果发现之后悔之莫及啊 某天发现cpu load高的出奇,发现一个minerd进程 占了大量cpu,google了一下,发现自己中招了 ...

    Docker中RocketMQ的安装与使用详解

    搜索RocketMQ的镜像,可以通过docker的hub.docker.com上进行搜索,也可以在Linux下通过docker的search命令进行搜索,不过最近防火墙升级后,导致国外的网站打开都很慢,通过命令搜索反而会更加方便,操作Docker命令...

Global site tag (gtag.js) - Google Analytics