1
#!/bin/bash
#James Kenaley 20120513
#Server Monitor Script
while read -r name ip content
do
    ip_status=`ping -w1 $ip | grep -c "100%"`
    web_status=`nmap -n -PN -p 80 $ip | grep -c open`
    ssh_status=`nmap -n -PN -p 22 $ip | grep -c open`
    content_status=`diff <(curl -s $ip | md5sum) <(cat $content) | grep -c -e "<" -e ">"`
    if [ $web_status -eq 1 ]
    then
            echo "The webserver is running on $name @ $ip"
    else
            echo "  The webserver is offline on $name @ $ip"
    fi

    if [ $ssh_status -eq 1 ]
    then
            echo "SSH is enabled on $name @ $ip"
    else
            echo "  SSH has been disabled on $name @ $ip"
    fi

    if [ $content_change -gt 0 ]
    then
            echo "  The content has changed on $name's webserver"
    else
            echo "The content is the same"
    fi
done < server.list

我知道最简单的方法就是比较另一个变量中的内容,但我真的希望将比较保持在一行中。因此,如果有人可以帮助我,我将不胜感激

4

1 回答 1

2

diff并且grep都支持-q。直接在if语句中使用它们,而不是捕获它们的输出并单独比较。

于 2012-05-16T02:15:39.330 回答