curl 命令获取网络网站的响应码

简介:

curl命令参数很多,博主很多都没有用过。今天发现可以用-w参数挺好用的。


-w:--write-out,作用就是输出点什么。curl的-w参数用于在一次完整且成功的操作后输出指定格式的内容到标准输出。输出格式由普通字符串和任意数量的变量组成,输出变量需要按照%{variable_name}的格式,如果需要输出%,double一下即可,即%%,同时,\n是换行,\r是回车,\t是TAB。curl会用合适的值来替代输出格式中的变量,所有可用变量如下:

url_effective 最终获取的url地址,尤其是当你指定给curl的地址存在301跳转,且通过-L继续追踪的情形。

http_code http状态码,如200成功,301转向,404未找到,500服务器错误等。(The numerical response code that was found in the last retrieved HTTP(S) or FTP(s) transfer. In 7.18.2 the alias response_code was added to show the same info.)

http_connect The numerical code that was found in the last response (from a proxy) to a curl CONNECT request. (Added in 7.12.4)

time_total 总时间,按秒计。精确到小数点后三位。 (The total time, in seconds, that the full operation lasted. The time will be displayed with millisecond resolution.)

time_namelookup DNS解析时间,从请求开始到DNS解析完毕所用时间。(The time, in seconds, it took from the start until the name resolving was completed.)

time_connect 连接时间,从开始到建立TCP连接完成所用时间,包括前边DNS解析时间,如果需要单纯的得到连接时间,用这个time_connect时间减去前边time_namelookup时间。以下同理,不再赘述。(The time, in seconds, it took from the start until the TCP connect to the remote host (or proxy) was completed.)

time_appconnect 连接建立完成时间,如SSL/SSH等建立连接或者完成三次握手时间。(The time, in seconds, it took from the start until the SSL/SSH/etc connect/handshake to the remote host was completed. (Added in 7.19.0))

time_pretransfer 从开始到准备传输的时间。(The time, in seconds, it took from the start until the file transfer was just about to begin. This includes all pre-transfer commands and negotiations that are specific to the particular protocol(s) involved.)

time_redirect 重定向时间,包括到最后一次传输前的几次重定向的DNS解析,连接,预传输,传输时间。(The time, in seconds, it took for all redirection steps include name lookup, connect, pretransfer and transfer before the final transaction was started. time_redirect shows the complete execution time for multiple redirections. (Added in 7.12.3))

time_starttransfer 开始传输时间。在发出请求之后,Web 服务器返回数据的第一个字节所用的时间(The time, in seconds, it took from the start until the first byte was just about to be transferred. This includes time_pretransfer and also the time the server needed to calculate the result.)

size_download 下载大小。(The total amount of bytes that were downloaded.)

size_upload 上传大小。(The total amount of bytes that were uploaded.)

size_header  下载的header的大小(The total amount of bytes of the downloaded headers.)

size_request 请求的大小。(The total amount of bytes that were sent in the HTTP request.)

speed_download 下载速度,单位-字节每秒。(The average download speed that curl measured for the complete download. Bytes per second.)

speed_upload 上传速度,单位-字节每秒。(The average upload speed that curl measured for the complete upload. Bytes per second.)

content_type 就是content-Type,不用多说了,这是一个访问我博客首页返回的结果示例(text/html; charset=UTF-8);(The Content-Type of the requested document, if there was any.)

num_connects Number of new connects made in the recent transfer. (Added in 7.12.3)

num_redirects Number of redirects that were followed in the request. (Added in 7.12.3)

redirect_url When a HTTP request was made without -L to follow redirects, this variable will show the actual URL a redirect would take you to. (Added in 7.18.2)

ftp_entry_path The initial path libcurl ended up in when logging on to the remote FTP server. (Added in 7.15.4)

ssl_verify_result ssl认证结果,返回0表示认证成功。( The result of the SSL peer certificate verification that was requested. 0 means the verification was successful. (Added in 7.19.0))


示例:用curl命令获取http://baidu.com网站的响应码

1
2
root@localhost:~ # curl -I -s -w "%{http_code}\n" -o /dev/null http://baidu.com
200

-I:获取header信息,

-s:silent模式,不加这个参数的话,执行命令是这样的结果

1
2
3
4
5
root@localhost:~ # curl -I  -w "%{http_code}\n" -o /dev/null http://baidu.com  
   % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                  Dload  Upload   Total   Spent    Left  Speed
   0    81    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
200

-o:将文件保存为命令行中指定的文件名的文件中,我们这里是直接将结果输出到/dev/null中,如果不加-o参数,输出结果如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
root@localhost:~ # curl -I  -w "%{http_code}\n"  http://baidu.com            
HTTP /1 .1 200 OK
Date: Sun, 29 May 2016 07:57:46 GMT
Server: Apache
Last-Modified: Tue, 12 Jan 2010 13:48:00 GMT
ETag:  "51-47cf7e6ee8400"
Accept-Ranges: bytes
Content-Length: 81
Cache-Control: max-age=86400
Expires: Mon, 30 May 2016 07:57:46 GMT
Connection: Keep-Alive
Content-Type: text /html
200










本文转自 曾哥最爱 51CTO博客,原文链接:http://blog.51cto.com/zengestudy/1784235,如需转载请自行联系原作者
目录
相关文章
|
27天前
|
Shell Linux C语言
【Shell 命令集合 网络通讯 】Linux 向指定用户或终端发送消息 write命令 使用指南
【Shell 命令集合 网络通讯 】Linux 向指定用户或终端发送消息 write命令 使用指南
32 0
|
27天前
|
安全 Unix Shell
【Shell 命令集合 网络通讯 】Linux 向所有当前登录的用户发送消息或通知 wall命令 使用指南
【Shell 命令集合 网络通讯 】Linux 向所有当前登录的用户发送消息或通知 wall命令 使用指南
28 0
|
27天前
|
存储 Shell Linux
【Shell 命令集合 网络通讯 】Linux 显示Unix-to-Unix Copy (UUCP) 系统的状态信息 uustat命令 使用指南
【Shell 命令集合 网络通讯 】Linux 显示Unix-to-Unix Copy (UUCP) 系统的状态信息 uustat命令 使用指南
26 0
|
27天前
|
Shell Linux C语言
【Shell 命令集合 网络通讯 】Linux 查看系统中的UUCP日志文件 uulog命令 使用指南
【Shell 命令集合 网络通讯 】Linux 查看系统中的UUCP日志文件 uulog命令 使用指南
29 0
|
27天前
|
Shell Linux C语言
【Shell 命令集合 网络通讯 】Linux 即时通讯工具 ytalk命令 使用指南
【Shell 命令集合 网络通讯 】Linux 即时通讯工具 ytalk命令 使用指南
25 0
|
27天前
|
安全 Unix Shell
【Shell 命令集合 网络通讯 】Linux 在不同的系统之间执行远程命令 uux 命令 使用指南
【Shell 命令集合 网络通讯 】Linux 在不同的系统之间执行远程命令 uux 命令 使用指南
28 0
|
27天前
|
存储 Shell Linux
【Shell 命令集合 网络通讯 】⭐Linux 显示当前系统的主机名和操作系统类型 uuname命令 使用教程
【Shell 命令集合 网络通讯 】⭐Linux 显示当前系统的主机名和操作系统类型 uuname命令 使用教程
28 0
|
27天前
|
存储 Linux Shell
【Shell 命令集合 网络通讯 】Linux 显示当前终端的名称 tty命令 使用指南
【Shell 命令集合 网络通讯 】Linux 显示当前终端的名称 tty命令 使用指南
22 0
|
27天前
|
Shell Linux C语言
【Shell 命令集合 网络通讯 】Linux 验证Samba配置文件 testparm命令 使用教程
【Shell 命令集合 网络通讯 】Linux 验证Samba配置文件 testparm命令 使用教程
31 0
|
27天前
|
网络协议 Shell Linux
【Shell 命令集合 网络通讯 】⭐Linux 远程登录工具 telnet 命令 使用指南
【Shell 命令集合 网络通讯 】⭐Linux 远程登录工具 telnet 命令 使用指南
28 0

热门文章

最新文章