2017-03-03 3 views
0

私のプログラムに間違いが見落とされていて、何も見つかりません。通常、私がBASHのミスに遭遇したとき、通訳はそのミスがどこにあるのかは分かりません。私はこのスクリプトをSANS InfoSecからカスタマイズして、Linuxスクリプトを使ってセキュリティを監視しようとしています。チェック機能が異なるプロトコルを調べる部分までは、すべて問題ありません。コメントを外すとエラーが出ます:./report:41行目:[:あまりにも多くの引数。ここにプログラムがあります...if文が多くの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

あなたはエラーが発生した行の上に何が起こっているかと思う説明することができます: - あなたは-a'が考慮される 'オペレータが欠落されますが、[= "" -a "$ 2" $ 3 "$ 2"!] – grail

+1

場合時代遅れの代わりに( '$ 2'と' $ 3'の間にあるべき演算子のために) '[-n" $ 2 "] && [" $ 2 "..." $ 3 "]'を使用してください。 – chepner

+0

2番目の引数が空でなければ2番目のargと3番目のargを出力します – user7526725

答えて

0

checkへの最後の3回の呼び出しでは、オペレーターとオペランドの間に必要なスペースがありません。

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 
} 

次にチェックするために、あなたの呼び出しがhttp://shellcheck.netを通して、あなたのコードを実行します

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 

など

のようになります。あなたが訂正できることはたくさんあります。

+0

私はまだプロトコルに問題があります。私はshellcheck.netをチェックし、if文に何らかのパーサーエラーがあると言っていました。コマンドの出力が10.10.0.1であっても、チェックゲートウェイはまだ赤色に戻ります。 – user7526725

+0

通話を修正しましたか? '' grep> '= 0' 'と '' 'grep>' = 0 ''とは異なる' 'grep> 。 – 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行目に終わる行数を数えていることを意味する正規表現です。 –

関連する問題