2017-03-03 74 views
0

我忽略了我的程序的任何错误,找不到任何。通常,当我遇到BASH的错误时,解释器关闭错误的位置。我试图从SANS InfoSec使用Linux脚本来监控安全性来定制这个脚本。一切都很好,直到检查函数查看不同协议的部分。当我取消注释他们时,我得到错误:./report:line 41:[:太多的参数。这里是程序...如果声明函数对许多argumnets

#!/bin/bash 

if [ "$(id -u)" != "0" ]; then 
    echo "Must be root to run this script!" 
    exit 1 
fi 

##### CONSTANTS - 
report=/home/chron/Desktop/report.log 
#router=/home/chron/Desktop/router.log 
red=`tput bold;tput setaf 1` 
yellow=`tput bold;tput setaf 3` 
green=`tput bold;tput setaf 2` 
blue=`tput bold;tput setaf 4` 
magenta=`tput bold;tput setaf 5` 
cyan=`tput bold;tput setaf 6` 
white=`tput sgr0` 

##### FUNCTIONS - 
pingtest() { 
ping=`ping -c 3 localhost | tail -2` 
loss=`echo $ping | cut -d"," -f3 | cut -d" " -f2` 
delay=`echo $ping | cut -d"=" -f2 | cut -d"." -f1` 

if [ "$loss" = "100%" ]; then 
    echo -n $red$1$white is not responding at all | mail -s'REPORT' localhost 
    echo 'You have mail in /var/mail!' 
    echo `date` $1 is not responding at all >> $report 
elif [ "$loss" != "0%" ]; then 
    echo $yellow$1$white is responding with some packet loss 
else 
    if [ "$delay" -lt 100 ]; then 
    echo $green$1$white is responding normally 
    else 
    echo $yellow$1$white is responding slow 
    fi 
fi 
} 

check() { 
if [ "$2" != "" -a "$2" $3 ] ; then 
    echo -n $green$1$white' ' 
else 
    echo -n $red$1$white' ' 
    echo `date` $1 was not $3 >> $report 
fi 
} 

##### __MAIN__ - 
pingtest localhost # hostname or ip 

echo "Server Configuration:" 
check hostname `hostname -s` '= localhost' 
check domain `hostname -d` '= domain.com' 
check ipaddress `hostname -I | cut -d" " -f1` '= 10.10.0.6' 
check gateway `netstat -nr | grep ^0.0.0.0 | cut -c17-27` '= 10.10.0.1' 
echo 

echo "Integrity of Files:" 
check hostsfile `md5sum /etc/hosts | grep 7c5c6678160fc706533dc46b95f06675 | wc -l` '= 1' 
check passwd `md5sum /etc/passwd | grep adf5a9f5a9a70759aef4332cf2382944 | wc -l` '= 1' 
#/etc/inetd.conf is missing... 
echo 
#echo "Integrity of Website:" 
#check www/index.html `lynx -reload -dump http://<LOCALIP> 2>&1 | md5sum | cut -d" " -f1 '=<MD5SUM>' 

#echo 
echo "Incoming attempts:" 
#lynx -auth user:password -dump http://10.10.0.1 >> $router 2>&1 
check telnet `grep \ 23$ $PWD/router.log | wc -l` '= 0' 
check ftp `grep \ 21$ $PWD/router.log | wc -l` '= 0' 
check ssh `grep \ 22$ $PWD/router.log | wc -l` '=0' 
check smtp `grep \ 25$ $PWD/router.log | wc -l` '=0' 
check dns `grep \ 53$ $PWD/router.log | wc -l` '=0' 
echo 

一些线被注释掉以后调整。现在我的问题是与协议。不知道有什么问题,因为它看起来像我有3个函数的参数。

+2

你能解释一下你认为对的误行正在发生的事情: - 如果[ “$ 2”= “” -a “2 $” $ 3!] – grail

+1

你缺少运营商,但'-a'被认为是过时。改用'[-n“$ 2”] && [“$ 2”...“$ 3”]'(无论操作符应该在$ 2和$ 3之间)。 – chepner

+0

如果第二个参数不为空然后打印第二个参数和第三个参数 – user7526725

回答

0

在您最后三次调用check时,缺少操作符和操作数之间所需的空间。

check ssh `grep \ 22$ $PWD/router.log | wc -l` '=0' 
check smtp `grep \ 25$ $PWD/router.log | wc -l` '=0' 
check dns `grep \ 53$ $PWD/router.log | wc -l` '=0' 

所有这些的最后一个参数应该是'= 0'

但是,这不是构建代码的好方法。如果您确实需要完全参数化比较(所有呼叫均使用=作为操作),请将操作员作为单独的参数传递。此外,正确写入,不需要预先检查$2是否为非空字符串。

check() { 
    if [ "$2" "$3" "$4" ] ; then 
    printf '%s%s%s ' "$green" "$1" "$white" 
    else 
    printf '%s%s%s ' "$red" "$1" "$white" 
    printf '%s %s was not %s\n' "$(date)" "$1" "$3" >> "$report" 
    fi 
} 

然后你的电话,检查应该像

check hostname "$(hostname -s)" = localhost 
check domain "$(hostname -d)" = domain.com 
check ipaddress "$(hostname -I | cut -d" " -f1)" = 10.10.0.6 
check gateway "$(netstat -nr | grep ^0.0.0.0 | cut -c17-27)" = 10.10.0.1 

通过http://shellcheck.net运行代码;有很多事情可以纠正。

+0

所以我仍然有一个协​​议的问题。我检查了shellcheck.net,它说if语句有某种解析器错误。即使命令的输出是10.10.0.1,检查网关仍会恢复为红色。 – user7526725

+0

您是否修复了电话? ''检查ssh'grep ...''= 0''试图运行'[“”=“输出”],与'[的输出不同'= 0] 。 – chepner

+0

我在操作员之后得到了这个空间。我认为协议不起作用,因为这本书正在使用某种我实际上没有的路由器日志文件。 – user7526725

0

这是我的另一个问题。为了看看发生了什么,我改变了一下。

router=/home/chron/Desktop/router.log 

check() { 
if [ "$2" "$3" "$4" ]; then 
    printf "%s%s%s" "$green" "$1" "$white" 
else 
    printf "%s%s%s" "$red" "$1" "$white" 
    printf "%s %s was not %s\n" "$(date)" "$1" $3" >> report.log 
fi 

check gateway "$(route | grep 10.10.0.1 | cut -c17-27)" = 10.10.0.1 

check telnet "$(grep -c \ 23$ $router)" = 0 
check ftp "$(grep -c \ 21$ $router)" = 0 
check ssh "$(grep -c \ 22$ $router)" = 0 
check smtp "$(grep -c \ 25$ $router)" = 0 
check dns "$(grep -c \ 53$ $router)" = 0 
+0

只是为了提醒,我从书中拉出来。我似乎无法确切知道22 $是如何被bash解释的。它是什么? – user7526725

+0

我猜它与本书使用的日志文件有关。刚想到我。我没有那个相同的日志文件。所以... – user7526725

+0

'22 $'不被bash解释,它被grep解释。在这种情况下,这是一个正则表达式,这意味着您要计算以'22'结尾的行数。 –