linux curl命令学习

用法:

    curl [options] [URL...]


DESCRIPTION(描述):

    curl用来‘从server传输数据’或者‘传输数据到server’,支持多种协议(HTTP, HTTPS, FTP, FTPS, SCP, SFTP, TFTP, DICT, TELNET, LDAP or FILE)。

    curl提供了大量有用的技巧像  proxy support, user authentication, FTP upload, HTTP post, SSL connections, cookies,  file  transfer  resume and more。

    curl由libcurl支持完成transfer-related(传输相关)的功能

URL:

    最好是  协议://url的形式。可以采用如下方式指定多个url

        http://site.{one,two,three}.com

        ftp://ftp.numericals.com/file[1-100].txt
        ftp://ftp.numericals.com/file[001-100].txt    (with leading zeros)
        ftp://ftp.letters.com/file[a-z].txt

        http://any.org/archive[1996-1999]/vol[1-4]/part{a,b,c}.html

    从curl 7.15.1版本开始,可以为一个范围指定‘步进’,可以取得每N个url匹配的资源,如下

         http://www.numericals.com/file[1-100:10].txt
         http://www.letters.com/file[a-z:2].txt

    对于一个curl命令请求多个文件的情况,curl尝试‘重用’链接(re-use connections)。

PROGRESS METER(进度条):

    curl默认会将文件内容显示到终端,可以通过linux重定向或者‘-o’选项重定向至文件,并显示一个如下进度条

   

    wKiom1VpF-DRqHegAACpi5Dhz4o200.jpg

    如果希望出现更简短的‘#’的进度条,可以通过‘-#’选项实现

    wKioL1VpGXXT42OAAABv3-zqK8Q892.jpg

OPTIONS(选项):

    通常,所有布尔选项通过--option开启,--no-option 关闭

    -a/--append

        (FTP/SFTP) 在upload过程中使用该选项,告诉curl把内容附加(append)到目标文件最后,若目标不存在则创建

    -A/--user-agent <agent string>

        (HTTP)指定发送给server的User-Agent,若指定的字符串中含有空格,放到单引(single quote)内。该功能同样可以通过‘-H/--header’选项设置。如果设置多次,最后一次的设置生效。

    效果如下

wKiom1VpILKgYQqJAACYdo3VSqs037.jpg

wKioL1VpIknS2kq1AAC8qx36MQI494.jpg





    --anyauth

        (HTTP)

   -b/--cookie <name=data>

        (HTTP)将数据作为cookie传递给server。它会认为这些data是server之前通过"Set-Cookie:"指令在客户端设置的。data格式为"NAME1=VALUE1;NAME2=VALUE2"。

    -B/--use-ascii

    --basic    

    --ciphers <list of ciphers>

    --compressed

        (HTTP) Request a compressed response using one of  the  algorithms libcurl  supports,  and return the uncompressed document.  If this option is used and the server sends an unsupported encoding,  curl will report an error.

    --connect-timeout <seconds>

        设置与server建立链接的超时时间,一旦与server建立连接成功,则此选项便不再使用

    -c/--cookie-jar <file name>

    -C/--continue-at <offset>

        续传相关

    --create-dirs

    --crlf

        (FTP)在上穿过程中将LF转换成CRLF

    --crlfile <file>

         (HTTPS/FTPS)

    -d/--data <data>

        (HTTP)通过POST请求向server发送指定的data。这和在浏览器填写一个表单并点击提交按钮的过程相同。This will cause curl to pass the data to the server using thecontent-type  application/x-www-form-urlencoded(注意和 -F/--form 的区别)
        -d/--data和--data-ascii一样,如果想以纯二进制POST 数据(To post data purely binary),应该使用--data-binary选项;想以URL-encode方式POST数据,使用--data-urlencode。

        如果这些选项在一个curl命令里出现多次,data片段会被用‘&’组合成一串data,例如‘-d name=daniel -d skill=lousy’会被自动组合为'name=daniel&skill=lousy' 的形式。  

        如果数据部分以 @ 开头,则剩余部分应该是一个文件,curl从该文件读取内容传递给server; 如果数据部分以 - 开头,表示希望从stdin读取数据。文件的内容必须是经过URL-encoded编码的,也可以指定多个文件。

    --data-binary <data>

    --data-urlencode <data>

    --delegation

    --digest

        (HTTP) Enables HTTP Digest authentication. This is  a  authentication  that  prevents the password from being sent over the wire in clear text. Use this in  combination  with  the  normal  -u/--user option to set user name and password. See also --ntlm, --negotiate and --anyauth for related options.

    --disable-eprt

    -D/--dump-header <file>

        将协议头信息(protocol headers)写进指定的文件

    -e/--referer <URL>

        (HTTP)发送"Referer Page"信息到server。可以用来欺骗server绕过一些简单的防盗链设置。同样可以通过-H/--header选项设置。当使用-L/--location时可以将该选项参数设为”;auto“。自动将--referer 设置为前一个url。

    -F/--form <name=content>

        (HTTP)让curl仿真完成浏览器端填写表单并提交的过程。This causes curl to POST data using the Content-Type  multipart/form-data(注意和 -d/--data <data> 的区别)。如果要将'内容'部分指定为文件,在文件前面加上 @;如果仅要读出文件里的内容,在文件前面加 < 。@和<的区别在于:

@以文件形式post;< 将文件内容取出来post。    

        举例:上传你的密码文件到server,表单名字为’password‘,文件为/etc/passwd,命令如下

        curl -F password=@/etc/passwd www.mypasswords.com

        如果想从stdin读取content,用 - 

        也可以通过’type=‘告诉curl以何种Content-Type post,如下

        curl -F "[email protected];type=text/html" url.com

    --form-string <name=string>

    -g/--globoff

    -G/--get

    -H/--header <header>

    (HTTP)请求页面是额外的请求头信息。可以设置任意数量的额外请求头信息,如果你设置的头信息的名称和curl默认使用的头信息相同,则你额外设置的头信息会覆盖curl默认会使用的。若想删除某个请求头信息,则将其值指定为空即可,例如 -H "Host:"

    例如:开启gzip传输

        curl -I http://www.sina.com.cn/ -H Accept-Encoding:gzip,defalte

    -i/--include

        在输出中包含响应头信息

    --interface <name>

        指定走哪个网络接口

    -I/--head

        (HTTP/FTP/FILE)仅取回请求文件的头信息

    -j/--junk-session-cookies

        (HTTP) When curl is told to read cookies from a given  file,  this option will make it discard all "session cookies". This will basically have the same effect as if a new session is started. Typical browsers  always discard session cookies when they’re closed down.

    -k/--insecure

        (SSL)

    --keepalive-time <seconds>

        keepalived超时时间

    --key <key>

        (SSL/SSH)

    --key-type <type>

        (SSL)

    -K/--config <config file>

        指定配置文件。可以讲curl的参数写进文件,每个选项及其参数一行,分隔符为:空格、冒号、等号(推荐用 等号);若参数中包含空格,则放到引号中;以 # 注释一行;长选项在config文件里不需要加--;

    --limit-rate <speed>

        指定最大传输速度, 默认以bytes/second来衡量,Examples: 200K, 3m and 1G。该选项指定传输过程中的平均速率,允许出现峰值高过该设置值的情况。

    -l/--list-only

        (FTP)

    --local-port <num>[-num]

        指定本地连接server时端口范围

    -L/--location

        (HTTP/HTTPS)如果遇到重定向的页面,该选项会使curl重发连接至新的location。如果和-i/--include  or -I/--head一起使用,所有请求过的server的响应头信息都会被显示。可以通过--max-redirs选项限制跳转的最多次数。

        当使用认证(authentication)时,curl只将user+password发送到第一台server(对比--location-trusted)

    --location-trusted

        和-L/--location相似,但是有认证时,会将user+password传递给所有被重定向到的server,这可能会不安全。

    --max-filesize <bytes>

        设定下载文件的最大值(in bytes),如果请求的页面大于设定的值,传输不会开始并且curl会返回错误码63。

    -m/--max-time <seconds>

        设定整个curl命令执行的最长时间,防止任务一直被hung住(联想下--connect-timeout)

    -N/--no-buffer

        禁止’输出缓冲‘。使用--buffer选项强制使用’缓冲‘。

    --no-keepalive

        禁用keepalive,默认curl开始keepalived。使用--keepalive强制开启。

    --no-sessionid

        (SSL)

    --noproxy <no-proxy-list>

    --ntlm    (HTTP) 允许NTLM认证,NTLM认证是微软开发的,用于IIS。

    -o/--output <file>

        将输出写入指定文件而非标准输出。如果使用了{}或[]请求多个页面,可以使用 '#' 来自动生成对应的页面文件,例如:

        curl http://{one,two}.site.com -o "file_#1.txt"

        curl http://{site,host}.host[1-5].com -o "#1_#2"

    -O/--remote-name

        将输出写入到本地文件,以下载的远程文件名命名。

    --remote-name-all

    --pass <phrase>    (SSL/SSH)

    --post301

        告诉curl当POST请求返回301状态码时不将POST请求转为GET请求(web浏览器普遍会进行这种转换)。该选项仅在使用 -L/--location 的前提下可用

    --post302   同上

    --proxy-anyauth

    --proxy-basic

    --proxy-digest

    --proxy-negotiate(协商、谈判)

    --proxy-ntlm

    --proxy1.0 <proxyhost[:port]>

    -p/--proxytunnel

    --pubkey <key>

    -P/--ftp-port <address>

    -q

    -Q/--quote <command>    (FTP/SFTP)

    --random-file <file>    (SSL)

    -r/--range <range>

        (HTTP/FTP/SFTP/FILE) 请求接收的字节范围(请求页面的一部分)。对于HTTP/1.1,  FTP  or SFTP server or a local FILE,范围可以用如下方式指定

        0-499    第一个500字节

        500-999    第二个500字节

        -500    最后的500字节

        9500-

        0-0,-1    指定第一和最后一字节

        500-700,600-799   

        100-199,500-599    指定两个分开的100字节

    --raw

        该选项禁用所有的’http内部解码‘(internal HTTP decoding  of  content)或’传输过程编码(transfer encodings)‘,以为改变的形式传递内容。

    -R/--remote-time

        When used, this will make libcurl attempt to figure out the timestamp of the remote  file,  and  if  that  is available make the local file get that same timestamp.

    --retry <num>

        重试次数

    --retry-delay <seconds>

        重试间隔

    --retry-max-time <seconds>

    -s/--silent

        安静模式不显示进度条和错误信息

    -S/--show-error

        和-s一起用,只输出错误信息

    --tcp-nodelay

        开启内核的TCP_NODELAY。

    -u/--user <user:password>

        指定用户名密码

    -U/--proxy-user <user:password>

        指定代理需要的用户名密码

    --url <URL>

        指定url,该选项只在config文件里需要

    -v/--verbose

    -V/--version

    -w/--write-out <format>

    定义当传输完成时将显示到标准输出的信息(类似于定义日志格式)

    例如:监控网页的响应时间

        curl -o /dev/null -s -w "time_connect: %{time_connect}\ntime_starttransfer: %{time_starttransfer}\ntime_total: %{time_total}\n" "http://www.kklinux.com"

    监控状态码

        curl -o /dev/null -s -w %{http_code} "http://www.kklinux.com"

    -0/--http1.0

        强制curl使用http1.0协议

   

  参考:http://www.cnblogs.com/wangkangluo1/archive/2012/04/17/2453975.html 

   

   

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章