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.
 

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

 

 

 

 

 

 

 

 

 

 

猜你喜欢

转载自isilic.iteye.com/blog/1764049