关于linux:每天学一个-Linux-命令73curl

3次阅读

共计 3496 个字符,预计需要花费 9 分钟才能阅读完成。

命令简介

curl 命令应用 HTTP、HTTPS、FTP、FTPS、SCP、SFTP、TFTP、DICT、TELNET、LDAP 或 FILE 反对的协定之一,将数据传输到网络服务器或从网络服务器传输数据。它非常适合在 Shell 脚本中应用。

curl 命令提供代理反对,用户身份验证,FTP 上传,HTTP 公布,SSL 连贯,cookie,文件传输复原,metalink 和其余性能。十分值得一看的 Curl 用法指南

语法格局

curl [options] [URL...]

选项阐明

-A  #用户代理
-b  #发送 Cookie 信息
-c  #将 Cookie 写入文件
-d  #发送 POST 申请的数据体
-e  #设置 HTTP 的标头 Referer 字段
-F  #向服务器上传文件
-G  #结构 URL 的查问字符串
-H  #增加 HTTP 申请的标头
-i  #打印服务器回应的 HTTP 标头
-I  打印服务器回应的 HEAD 标头
-k  #跳过 SSL 检测
-L  #追随服务器的重定向
–limit-rate   #限度申请和回应的带宽
-o  #将服务器的回应保留成文件 (下载文件,而后重新命名)
-O  #将服务器的回应保留成文件(下载多个文件)-s  #不输入谬误和进度信息
-S  #只输入错误信息
-u  #设置认证的用户名和明码
-v  #打印调试信息
-x  #设置申请代理
-X  #指定申请的办法

利用举例

打印版本信息

[root@CentOS7-1 ~]# curl --version
curl 7.29.0 (x86_64-redhat-linux-gnu) libcurl/7.29.0 NSS/3.44 zlib/1.2.7 libidn/1.28 libssh2/1.8.0
Protocols: dict file ftp ftps gopher http https imap imaps ldap ldaps pop3 pop3s rtsp scp sftp smtp smtps telnet tftp 
Features: AsynchDNS GSS-Negotiate IDN IPv6 Largefile NTLM NTLM_WB SSL libz unix-sockets 

下载文件

[root@CentOS7-1 download]# curl -O http://nginx.org/download/nginx-1.18.0.zip
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 1671k  100 1671k    0     0   300k      0  0:00:05  0:00:05 --:--:--  414k
[root@CentOS7-1 download]# ll
total 1672
-rw-r--r-- 1 root root 1711619 Mar 13 09:29 nginx-1.18.0.zip
#将下载下来的文件从新改名成新的指定的文件名
[root@CentOS7-1 download]# curl -o $(date +%F)_download_nginx  http://nginx.org/download/nginx-1.18.0.zip
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 1671k  100 1671k    0     0   480k      0  0:00:03  0:00:03 --:--:--  480k
[root@CentOS7-1 download]# ll
total 5016
-rw-r--r-- 1 root root 1711619 Mar 13 09:31 2021-03-13_download_nginx
-rw-r--r-- 1 root root 1711619 Mar 13 09:30 F_download_nginx
-rw-r--r-- 1 root root 1711619 Mar 13 09:29 nginx-1.18.0.zip

POST 申请举例

[root@CentOS7-1 ~]# curl http://192.168.1.199
this is a www web stie
#显示全副信息
[root@CentOS7-1 ~]# curl -i http://192.168.1.199
HTTP/1.1 200 OK
Date: Wed, 10 Mar 2021 17:05:04 GMT
Server: Apache/2.4.6 (CentOS)
Last-Modified: Wed, 10 Mar 2021 17:03:20 GMT
ETag: "17-5bd31a4e35769"
Accept-Ranges: bytes
Content-Length: 23
Content-Type: text/html; charset=UTF-8
this is a www web stie
#只显示头部信息
[root@CentOS7-1 ~]# curl -l http://192.168.1.199
this is a www web stie
#显示 get 申请全过程解析信息
[root@CentOS7-1 ~]# curl -v http://192.168.1.199
* About to connect() to 192.168.1.199 port 80 (#0)
*   Trying 192.168.1.199...
* Connected to 192.168.1.199 (192.168.1.199) port 80 (#0)
> GET / HTTP/1.1
> User-Agent: curl/7.29.0
> Host: 192.168.1.199
> Accept: */*
> 
< HTTP/1.1 200 OK
< Date: Wed, 10 Mar 2021 17:05:16 GMT
< Server: Apache/2.4.6 (CentOS)
< Last-Modified: Wed, 10 Mar 2021 17:03:20 GMT
< ETag: "17-5bd31a4e35769"
< Accept-Ranges: bytes
< Content-Length: 23
< Content-Type: text/html; charset=UTF-8
< 
this is a www web stie
* Connection #0 to host 192.168.1.199 left intact

登录服务器

[root@CentOS7-1 ~]# curl -u mingongge:passwdPassWd  https://github.com/mingongge/
[root@CentOS7-1 ~]# curl -u mingongge  https://github.com/mingongge/
Enter host password for user 'mingongge':

上传文件(如上传文件到文件服务器)

[root@CentOS7-1 ~]# curl -T user1.png ftp://username:password@ip:port/www/web/user_download/

打印下载进度条

[root@CentOS7-1 ~]# curl -# -O http://nginx.org/download/nginx-1.18.0.zip
######################################################################## 100.0%

每天学一个 Linux 命令(71):traceroute

每天学一个 Linux 命令(72):tcpdump

正文完
 0