【问题标题】:How to use curl to get web server type?如何使用 curl 获取 Web 服务器类型?
【发布时间】:2016-06-08 19:52:41
【问题描述】:

这是我的 shell 脚本中的一些行:

curl -I --connect-timeout 60 google.com|grep "Server:" >> 1000-server.txt
curl -I --connect-timeout 60 yahoo.com|grep "Server:" >> 1000-server.txt
curl -I --connect-timeout 60 bing.com|grep "Server:" >> 1000-server.txt

我的脚本在大多数情况下都能正确获取网络服务器类型:

Server: gws
Server: ATS
Server: Microsoft-IIS/8.5

但是,如果curl 连接超时或出错,它不会输出任何消息。另外,如果由于某种原因grep 没有找到任何东西,它不会输出任何错误消息。

如果curlgrep 失败,是否有任何方法可以输出类似"ERROR: NOT FOUND" 的错误消息?

对不起,如果这是一个新手问题,只是想不通......

【问题讨论】:

    标签: bash shell curl


    【解决方案1】:
    curl -I --connect-timeout 60 google.com|grep "Server:" >> 1000-server.txt || echo ERROR | tee -a 1000-server.txt
    

    如果 grep 没有找到字符串,curl ... | grep ... 命令将返回 false。在这种情况下,|| echo ERROR | tee -a 1000-server.txt 将执行。

    echo 的输出通过管道传输到 tee,然后将输出附加 (-a) 到文件 1000-server.txt 中,同时打印相同的输出到 STDOUT。

    【讨论】:

    • 这几乎似乎做到了,但它并没有向1000-server.txt 回显“错误”,它只是在终端中显示它。
    • 嗨,你能解释一下你的答案吗?出现在审查队列中,因为只有代码的答案往往如此。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2020-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多