Linux curl命令

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Linux curl命令相关的知识,希望对你有一定的参考价值。

curl : 命令行里访问网页,网站,下载文件

[[email protected] ~]# curl www.apelearn.com  #这个命令之后,出现了www.apelearn.com网站的源码一大堆

[[email protected] ~]# curl -I !$  -I省略源码

curl -I www.apelearn.com

HTTP/1.1 200 OK

Server: nginx

Date: Wed, 27 Jan 2016 15:16:31 GMT

Content-Type: text/html

Connection: keep-alive

Vary: Accept-Encoding

X-Powered-By: php/5.3.27


上面注意到有一个200 OK ,其实常见的状态码还有200 301 302 404 403 502 503

-----------------------------------------------------------------------------------


[[email protected] ~]# curl -I www.lishiming.net  curl一下这个网站发现一个状态码301,意味着设置了跳转

HTTP/1.1 301 Moved Permanently 

Server: nginx/1.0.15

Date: Wed, 27 Jan 2016 15:23:44 GMT

Content-Type: text/html

Content-Length: 185

Connection: keep-alive

Location: http://www.apelearn.com/bbs/forum.php #跳转到这个域名下。


[[email protected] ~]#


--------------------------------------------------------------------------------------


[[email protected] ~]# vim /etc/hosts

127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4

::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

192.168.1.1 www.qq.com

~

~

~

~

~

~

~

~

~

~

~

~

~

~

~

~

~

~

~

~

"/etc/hosts" 3L, 181C  

[[email protected] ~]# ping www.qq.com

PING www.qq.com (192.168.1.1) 56(84) bytes of data.

---------------------------------------------------------------------------


[[email protected] ~]# ping www.qq.com

PING www.qq.com (220.181.138.29) 56(84) bytes of data.

64 bytes from 220.181.138.29: icmp_seq=1 ttl=128 time=78.3 ms

64 bytes from 220.181.138.29: icmp_seq=2 ttl=128 time=77.5 ms

64 bytes from 220.181.138.29: icmp_seq=3 ttl=128 time=83.9 ms

64 bytes from 220.181.138.29: icmp_seq=4 ttl=128 time=87.4 ms

64 bytes from 220.181.138.29: icmp_seq=5 ttl=128 time=77.2 ms

64 bytes from 220.181.138.29: icmp_seq=6 ttl=128 time=77.6 ms

^Z

[4]+  Stopped                 ping www.qq.com

[[email protected] ~]#

--------------------------------------------------------------------------------

glinux ~]# curl -x220.181.138.29:80 www.qq.com -I  #通过-x命令即使配置文件修改了,也能ping到正确的地址,别忘了                                                   加I省略信息

HTTP/1.1 200 OK

Server: squid/3.4.3

Date: Wed, 27 Jan 2016 15:32:12 GMT

Content-Type: text/html; charset=GB2312

Connection: keep-alive

Vary: Accept-Encoding

Vary: Accept-Encoding

Expires: Wed, 27 Jan 2016 15:33:12 GMT

Cache-Control: max-age=60

Vary: Accept-Encoding

Vary: Accept-Encoding

X-Cache: HIT from beijing.qq.com

----------------------------------------------------------------------------------


[[email protected] ~]# curl -IV www.qq.com             #显示访问过程。

curl 7.19.7 (i386-redhat-linux-gnu) libcurl/7.19.7 NSS/3.14.0.0 zlib/1.2.3 libidn/1.18 libssh2/1.4.2

Protocols: tftp ftp telnet dict ldap ldaps http file https ftps scp sftp

Features: GSS-Negotiate IDN IPv6 Largefile NTLM SSL libz

-----------------------------------------------------------------------------------


[[email protected] ~]# curl -Iv www.qq.com

* About to connect() to www.qq.com port 80 (#0)

*   Trying 220.181.138.29... connected

* Connected to www.qq.com (220.181.138.29) port 80 (#0)

> HEAD / HTTP/1.1

> User-Agent: curl/7.19.7 (i386-redhat-linux-gnu) libcurl/7.19.7 NSS/3.14.0.0 zlib/1.2.3 libidn/1.18 libssh2/1.4.2

> Host: www.qq.com

> Accept: */*

>

< HTTP/1.1 200 OK

HTTP/1.1 200 OK

< Server: squid/3.4.3

Server: squid/3.4.3

< Date: Wed, 27 Jan 2016 15:45:45 GMT

Date: Wed, 27 Jan 2016 15:45:45 GMT

< Content-Type: text/html; charset=GB2312

Content-Type: text/html; charset=GB2312

< Connection: keep-alive

Connection: keep-alive

< Vary: Accept-Encoding

Vary: Accept-Encoding

< Vary: Accept-Encoding

Vary: Accept-Encoding

< Expires: Wed, 27 Jan 2016 15:46:45 GMT

Expires: Wed, 27 Jan 2016 15:46:45 GMT

< Cache-Control: max-age=60

Cache-Control: max-age=60

< Vary: Accept-Encoding

Vary: Accept-Encoding

< Vary: Accept-Encoding

Vary: Accept-Encoding

< X-Cache: HIT from beijing.qq.com

X-Cache: HIT from beijing.qq.com

* no chunk, no close, no size. Assume close to signal end


<

* Closing connection #0



*********useragent 标示,每个网站都有它自己的标识。

-------------------------------------------------------------------------------


[[email protected] ~]# curl -u username:password http://www.qq.com     ^C  指定user访问网站

[[email protected] ~]# curl -u username:password http://www.qq.com -I      当然后面可以加-I,简略信息。

HTTP/1.1 200 OK

Server: squid/3.4.3

Date: Wed, 27 Jan 2016 15:52:13 GMT

Content-Type: text/html; charset=GB2312

Connection: keep-alive

Vary: Accept-Encoding

Vary: Accept-Encoding

Expires: Wed, 27 Jan 2016 15:53:13 GMT

Cache-Control: max-age=60

Vary: Accept-Encoding

Vary: Accept-Encoding

X-Cache: HIT from beijing.qq.com

---------------------------------------------------------------------------------

[[email protected] ~]# curl -O    http://passport.ixpub.net/data/avatar/016/72/81/39_avatar_middle.jpg   最后一节是默认名称,跟网页中的保持一致 -大O不能指定名称,重新命名。

% Total    % Received % Xferd  Average Speed   Time    Time     Time  Current

                                 Dload  Upload   Total   Spent    Left  Speed

100  5725  100  5725    0     0   1060      0  0:00:05  0:00:05 --:--:-- 35559

[[email protected] ~]# ls 39_avatar_middle.jpg  

39_avatar_middle.jpg

[[email protected] ~]# curl -o xiazai  http://passport.ixpub.net/data/avatar/016/72/81/39_avatar_middle.jp #-小O的话可以指定重命名的名称。


本文出自 “11075268” 博客,请务必保留此出处http://11085268.blog.51cto.com/11075268/1739404

以上是关于Linux curl命令的主要内容,如果未能解决你的问题,请参考以下文章

手把手教你在centos 7.4上搭建NTP服务器

手把手教你在centos 7.4上搭建NTP服务器

Linux 环境下NTP 服务搭建

Linux 环境下NTP 服务搭建

鸟哥服务器架设——NTP服务器搭建

linux搭建NTP时间服务器步骤