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,如需转载请自行联系原作者
目录
相关文章
|
8天前
|
监控 安全 Linux
【专栏】Linux 中有哪些常用的网络命令,分别适用于什么场景?
【4月更文挑战第28天】本文介绍了Linux中六个常用的网络命令:ping、traceroute、netstat、nmap、ifconfig和ip,以及它们在测试网络连通性、追踪路由、查看网络状态、安全扫描和接口配置等场景的应用。通过学习和运用这些命令,系统管理员和网络爱好者能更有效地诊断和管理网络问题,确保网络稳定运行。
|
3天前
|
Linux Shell 网络安全
网络安全中Dos和linux常用命令总结
本篇是对网安学习中,常用的命令做一个图文与命令示例,并对一些比较重要的dos和shell命令进行总结,方便自己后续学习进行查询,并希望能够给更多人有一个总结命令和了解命令的地方.
27 5
|
6天前
|
JSON 网络协议 Linux
Linux ip命令:网络的瑞士军刀
【4月更文挑战第25天】
10 1
|
7天前
|
缓存 网络协议 Linux
Linux 网络命令大全,详细归纳!
【4月更文挑战第24天】
33 3
Linux 网络命令大全,详细归纳!
|
8天前
|
运维 网络协议 安全
【专栏】30 个思科设备巡检命令,值得每位网络工程师收藏!
【4月更文挑战第28天】本文列举了30个必备的思科设备巡检命令,涵盖设备基本信息、性能、网络连接、安全及其它重要方面。这些命令包括`show version`、`show running-config`、`show ip route`、`show access-lists`等,对监控设备状态、排查故障及优化性能至关重要。熟悉并运用这些命令能提升网络工程师的工作效率,确保网络稳定运行。不断学习新命令以适应网络技术发展是网络工程师的必修课。
|
8天前
|
安全 网络协议 Linux
【专栏】一文教你玩转 Linux 的 ping 命令,从此成为 Linux 网络高手
【4月更文挑战第28天】本文详细介绍了Linux系统中ping命令的使用,包括其基本语法、输出信息、常用参数及高级用法。通过ping,用户可测试网络连通性、诊断故障及评估性能。此外,文章还讨论了ping在不同协议、模拟网络环境及与其他命令结合使用时的场景。注意防火墙和网络环境可能影响ping结果,理解错误信息有助于网络问题排查。熟练掌握ping命令,能助你成为Linux网络专家。不断学习和实践,提升网络技能,为构建稳定网络环境贡献力量。
|
8天前
|
网络虚拟化 数据安全/隐私保护 数据中心
【专栏】思科和华为网络设备命令对比,多维度,建议网络工程师收藏!
【4月更文挑战第28天】本文对比了思科与华为网络设备的基本配置、接口、VLAN、路由、访问控制列表及其它关键命令。尽管两者在很多操作上相似,如设备命名(思科:`hostname`,华为:`sysname`)、查看版本信息(思科:`show version`,华为:`display version`),但在某些方面存在差异,如接口速率设置(两者都使用`speed`和`duplex`,但命令结构略有不同)和VLAN配置(华为的`port hybrid`命令)。
|
8天前
|
运维 监控 安全
【专栏】运维工程师必须知道的 11 个 Linux 网络命令
【4月更文挑战第28天】本文介绍了11个必备的Linux网络命令,包括ifconfig、ip、ping、traceroute、netstat、tcpdump、ss、telnet、ftp、ssh和nmap,这些命令在网络配置、故障排查和性能监控中发挥关键作用。通过实例分析,强调了它们在实际工作中的综合应用,帮助运维工程师提升效率和应对网络挑战。学习和熟练运用这些命令是提升网络管理能力的基础。
|
10天前
|
安全 Linux 网络安全
【Linux笔记】网络操作命令详细介绍
【Linux笔记】网络操作命令详细介绍
|
13天前
|
安全 Ubuntu Linux
Linux 网络操作命令FTP
Linux 网络操作命令FTP
31 0