【问题标题】:shell script: get server info from curlshell 脚本:从 curl 获取服务器信息
【发布时间】:2014-02-03 14:49:05
【问题描述】:

我需要一些帮助,从 shell 脚本中完成的 curl 调用中获取服务器信息。

在我的脚本中,我遍历列表中的多个 URL,并对每个 URL 执行 cURL。

但我很难获取服务器信息,因为它不是 cURL 结果的静态位置。

>curl -I -s $temp

其中,$temp 是任意 URL,例如例子.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 的结果上方。

问题:我如何提取它说 server 的部分?
结果应该是这样的

>echo $server 

将产生(所以基本上是“服务器:”之后的整个其余行)

ECS (iad/19AB)

非常感谢!

【问题讨论】:

    标签: regex shell curl


    【解决方案1】:

    使用 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)
    

    【讨论】:

      猜你喜欢
      • 2012-10-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-04
      相关资源
      最近更新 更多