dongnan
总版主
总版主
  • 粉丝52
  • 发帖数2165
  • 铜币13236枚
  • 威望5838点
  • 银元150个
  • 社区居民
  • 最爱沙发
  • 忠实会员
  • 原创写手
阅读:7778回复:7

curl 命令

楼主#
更多 发布于:2013-08-24 20:05
curl 命令

例子:
curl -k -L -I https://www.google.com.hk

HTTP/1.1 200 OK
Date: Wed, 21 Aug 2013 10:23:54 GMT
Expires: -1
Cache-Control: private, max-age=0
Content-Type: text/html; charset=Big5
Set-Cookie: PREF=ID=c3e552cb49a35c5c:FF=0:NW=1:TM=1377080634:LM=1377080634:S=s3pr-FoyX2BDhP0r; expires=Fri, 21-Aug-2015 10:23:54 GMT; path=/; domain=.google.com.hk
Set-Cookie: NID=67=Sq6XV7Eos2QXN9y2bWchNvFwDjODpWF-zAVH-ngSLDH1s1GcKboQ0p-HM0mApBY9aWnHNZDqvvYqK53XC3AoiuRcnUWGV5dnVZr5dDR3ldfZz80RHMCabimSwuwU9Zbt; expires=Thu, 20-Feb-2014 10:23:54 GMT; path=/; domain=.google.com.hk; HttpOnly
P3P: CP="This is not a P3P policy! See http://www.google.com/support/accounts/bin/answer.py?hl=en&answer=151657 for more info."
Server: gws
X-XSS-Protection: 1; mode=block
X-Frame-Options: SAMEORIGIN
Transfer-Encoding: chunked

常用选项
man curl
-A, --user-agent <agent string>
              (HTTP) Specify the User-Agent string to send to the HTTP server.
              Some  badly  done  CGIs  fail  if  this  field  isn't   set   to
              "Mozilla/4.0".  To  encode  blanks  in  the string, surround the
              string with single quote marks. This can also be  set  with  the
              -H, --header option of course.

 -H, --header <header>
              (HTTP) Extra header to use when getting  a  web  page.  You  may
              specify any number of extra headers. Note that if you should add
              a custom header that has the same name as one  of  the  internal
              ones  curl  would  use
,  your externally set header will be used
              instead of the internal one. This allows you to make even trick‐
              ier  stuff  than  curl would normally do. You should not replace
              internally set  headers  without  knowing  perfectly  well  what
              you're  doing. Remove an internal header by giving a replacement
              without content on the right  side  of  the  colon,  as  in:  -H
              "Host:".

-I, --head
              (HTTP/FTP/FILE) Fetch the HTTP-header only! HTTP-servers feature
              the command HEAD
which this uses to get nothing but  the  header
              of  a  document.  When used on a FTP or FILE file, curl displays
              the file size and last modification time only.

-L, --location
              (HTTP/HTTPS)  If  the server reports that the requested page has
              moved to a different location (indicated with a Location: header
              and  a  3XX  response code), this option will make curl redo the
              request on the new place. If used together with -i, --include or
              -I, --head, headers from all requested pages will be shown. When
              authentication is used, curl only sends its credentials  to  the
              initial  host
.  If a redirect takes curl to a different host, it
              won't be able to intercept the user+password. See  also  --loca‐
              tion-trusted  on how to change this. You can limit the amount of
              redirects to follow by using the --max-redirs option.

 -k, --insecure
              (SSL) This option explicitly allows curl to  perform  "insecure"
              SSL connections and transfers
. All SSL connections are attempted
              to be made secure by using the CA certificate  bundle  installed
              by  default.  This  makes  all connections considered "insecure"
              fail unless -k, --insecure is used.

#
dongnan
总版主
总版主
  • 粉丝52
  • 发帖数2165
  • 铜币13236枚
  • 威望5838点
  • 银元150个
  • 社区居民
  • 最爱沙发
  • 忠实会员
  • 原创写手
沙发#
发布于:2015-09-21 09:39
curl 使用用户名密码
示例:
curl -udongnan:ywwd.net http://localhost:88/haproxy_stats

参数
 -u/--user <user:password>
              Specify the user name and password to use for server authentication.
dongnan
总版主
总版主
  • 粉丝52
  • 发帖数2165
  • 铜币13236枚
  • 威望5838点
  • 银元150个
  • 社区居民
  • 最爱沙发
  • 忠实会员
  • 原创写手
板凳#
发布于:2015-12-09 11:28
curl -X 指定方法
示例:curl 使用POST
curl -X POST -IL  http://ywwd.net:8000/index.php

验证:
114.242.1x.9x - - [09/Dec/2015:11:24:19 +0800] "POST / HTTP/1.1" 200 41909 "-" "curl/7.35.0" -

帮助
       -X/--request <command>
              (HTTP) Specifies a custom request method to use when communicating with the HTTP server.  The specified request  will  be used instead of the method otherwise used (which defaults to GET). Read the HTTP 1.1 specification for details and expla-nations. Common additional HTTP requests include PUT and DELETE, but related technologies like  WebDAV  offers  PROPFIND, COPY, MOVE and more.

              (FTP) Specifies a custom FTP command to use instead of LIST when doing file lists with FTP.
              If this option is used several times, the last one will be used.

#
dongnan
总版主
总版主
  • 粉丝52
  • 发帖数2165
  • 铜币13236枚
  • 威望5838点
  • 银元150个
  • 社区居民
  • 最爱沙发
  • 忠实会员
  • 原创写手
地板#
发布于:2015-12-09 11:31
curl limit-rate 限制带宽
示例:限制带宽 byte/s
curl --limit-rate 100K http://ywwd.net:8000

帮助:
 --limit-rate <speed>
              Specify  the maximum transfer rate you want curl to use. This feature is useful if you have a limited pipe and you’d like your transfer not to use your entire bandwidth.

              The given speed is measured in bytes/second, unless a suffix is appended.  Appending ’k’ or ’K’ will count the number  as  kilobytes, ’m’ or M’ makes it megabytes, while ’g’ or ’G’ makes it gigabytes. Examples: 200K, 3m and 1G.
              The  given  rate  is  the  average speed counted during the entire transfer. It means that curl might use higher transfer speeds in short bursts, but over time it uses no more than the given rate.

              If you also use the -Y/--speed-limit option, that option  will  take  precedence  and  might  cripple  the  rate-limiting  slightly, to help keeping the speed-limit logic working.

              If this option is used several times, the last one will be used.

#
dongnan
总版主
总版主
  • 粉丝52
  • 发帖数2165
  • 铜币13236枚
  • 威望5838点
  • 银元150个
  • 社区居民
  • 最爱沙发
  • 忠实会员
  • 原创写手
4楼#
发布于:2016-06-12 14:39
curl 下载文件
举个栗子:curl 下载mp4 文件到当前目录
curl -k -O -H "Host:ywwd.net" https://103.243.xxx.xxx/v/t1.mp4

参数
-O:使用URL中默认的文件名保存文件到本地;
-o:将文件保存为命令行中指定的文件名的文件中;
-C:选项可对文件使用断点续传功能;

扩展
curl 默认将输出打印到标准输出中(STDOUT)中;

#update 20160709
curl -o /etc/yum.repos.d/epel-6.repo http://mirrors.aliyun.com/repo/epel-6.repo

参考CURL常用命令

#
dongnan
总版主
总版主
  • 粉丝52
  • 发帖数2165
  • 铜币13236枚
  • 威望5838点
  • 银元150个
  • 社区居民
  • 最爱沙发
  • 忠实会员
  • 原创写手
5楼#
发布于:2016-07-04 17:21
curl 伪造 http 头部
示例:伪造 HTTP_X_FORWARDED_FOR 变量
#注意 变量名称与 值之间使用 冒号分割;
curl -k -H "HTTP_X_FORWARDED_FOR: 123.1XX.XX.XX" https://ywwd.net/ -v
* About to connect() to ywwd.net/ port 443 (#0)
*   Trying 120.27.1xx.1xx... connected
* Connected to ywwd.net/ (120.27.1xx.1xx) port 443 (#0)
* Initializing NSS with certpath: sql:/etc/pki/nssdb
* warning: ignoring value of ssl.verifyhost
* skipping SSL peer certificate verification
* SSL connection using TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256
* Server certificate:
*     subject: CN=*.ywwd.net,O="BJC Network Technology Co., Ltd.",L=Beijing,ST=Beijing,C=CN
*     start date: Jul 09 02:31:12 2015 GMT
*     expire date: Jul 09 02:31:12 2016 GMT
*     common name: *.ywwd.net
*     issuer: CN=WoSign Class 3 OV Server CA G2,O=WoSign CA Limited,C=CN
> GET / HTTP/1.1
> User-Agent: curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.21 Basic ECC zlib/1.2.3 libidn/1.18 libssh2/1.4.2
> Host: ywwd.net/
> Accept: */*
> HTTP_X_FORWARDED_FOR: 123.1XX.XX.XX
>
< HTTP/1.1 404 Not Found
< Date: Mon, 04 Jul 2016 09:16:20 GMT
< Content-Type: text/html; charset=utf8
< Content-Length: 219
< Connection: keep-alive
<
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html>
<head><title>404 Not Found</title></head>
<body bgcolor="white">
<h1>404 Not Found</h1>
<p>The requested URL was not found on this server.</body>
</html>
* Connection #0 to host ywwd.net/ left intact
* Closing connection #0

示例
:指定多个头部
curl http://ywwd.net/ -H 'X-Forwarded-For: 1.1.1.1' -H 'X-Real-IP: 2.2.2.2'

#
dongnan
总版主
总版主
  • 粉丝52
  • 发帖数2165
  • 铜币13236枚
  • 威望5838点
  • 银元150个
  • 社区居民
  • 最爱沙发
  • 忠实会员
  • 原创写手
6楼#
发布于:2017-05-27 16:16
curl 上传文件
示例: curl 上传文件
curl -F "file=@test.doc" "http://your_web_server/json?token=111111"
{"ok":1,"errmsg":null,"data":{xxxxxxx}}
dongnan
总版主
总版主
  • 粉丝52
  • 发帖数2165
  • 铜币13236枚
  • 威望5838点
  • 银元150个
  • 社区居民
  • 最爱沙发
  • 忠实会员
  • 原创写手
7楼#
发布于:2018-06-26 09:49
curl 使用标准输入
如果要在标准输入(stdin)中获取输入来代替文件,则在应提供文件名的位置上使用-代替。
spd-cli status | /usr/bin/curl -v -H"Content-type: application/json"  http://msg.zongming.net/post/ -X POST -d -
游客

返回顶部