2017-06-16 44 views

回答

0

您可能要捕获的变量输出,然后调节基础上,500503402匹配消息:

test=$(nc -w 2 -z $host 80) 
if [[ -n $(grep '500\|503\|402' <<< $test) ]]; then 
    ... 
else 
    echo ok 
fi 
+0

退出状态不能高于255 –

+0

你总是有我的背。我已更正了我的答案,谢谢。 – Unwastable

0

要检查网站的HTTP状态代码,我会建议使用curl而不是如果你可以:

#!/bin/bash 
hostlist=(s-example1.us s-example2.us) 
for host in "${hostlist[@]}"; do 
    status=$(curl -sLI -o /dev/null -w "%{http_code}" "$host") 
    case "$status" in 
    200) echo "INFO: ssh on $host responding [Looks Good]" ;; 
    *) echo "ERROR: $host responded with $status" ;; 
    esac 
done 
+0

非常感谢!完善 –

0

我会建议使用cURL为此。

像这样的东西会给你只是你打的任何端点的状态码。这将输出每个主机的响应代码,如果响应代码无效(000),它将在另一个消息中返回失败的响应代码。

CODE:

#!/bin/bash 
hosts=(fliggerfloffin.com google.com) 

for host in ${hosts[@]}; do 
    code=$(curl -m 2 -s -o /dev/null -w "%{http_code}" ${host}) 
    if [[ -z $code ]]; then 
     printf "%s\n" "Missing response code from host" 
     exit 1 
    fi 
    if (($code == 000)); then 
     code="FAILURE" 
    fi 

    printf "%s\n" "HOST: [$host]  STATUS: [${code}]" 
done 

OUTPUT:

[email protected] [OPS]:~ > bash test.sh 
HOST: [fliggerfloffin.com]  STATUS: [FAILURE] 
HOST: [google.com]  STATUS: [301]