Shell脚本:从curl获取服务器信息 [英] shell script: get server info from curl
问题描述
我需要一些帮助,以在Shell脚本中完成curl调用来获取服务器信息。
i need some help getting the server info from a curl call done in a shell script.
在我的脚本中,我遍历了列表中的几个URL并执行了
In my script, I iterate over several URLs in a list and do a cURL on each URL.
但是我很难获得服务器信息,因为它不在cURL结果的静态位置。
But I struggle getting the server information as it is not in a static position of the result of cURL.
>curl -I -s $temp
其中,$ temp是任意URL,例如example.org
where, $temp is some arbitrary URL, e.g. example.org
HTTP/1.1 200 OK
Accept-Ranges: bytes
Cache-Control: max-age=604800
Content-Type: text/html
Date: Mon, 03 Feb 2014 14:35:39 GMT
Etag: "359670651"
Expires: Mon, 10 Feb 2014 14:35:39 GMT
Last-Modified: Fri, 09 Aug 2013 23:54:35 GMT
Server: ECS (iad/19AB)
X-Cache: HIT
x-ec-custom-error: 1
Content-Length: 1270
以上结果为example.org。
Above the result for example.org.
问题:如何提取显示 server 的部分?
结果应为
Question: how do I extract the part where it says server?
The result should be such that
>echo $server
会产生收益(因此基本上, Server:之后的其余行)
will yield (so basically the entire rest of the line after the "Server: ")
ECS(iad / 19AB)
ECS (iad/19AB)
感谢大家!
推荐答案
使用awk:
server=$(curl -I -s http://example.org | awk -F': ' '$1=="Server"{print $2}')
echo "$server"
ECS (cpm/F858)
或者您也可以使用 grep -oP
:
server=$(curl -I -s http://example.org | grep -oP 'Server: \K.+')
echo "$server"
ECS (cpm/F858)
这篇关于Shell脚本:从curl获取服务器信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!