0

IP アドレスのブラックリストを取得してブロックする簡単なスクリプトを作成しているときに、次の問題に遭遇しました。

    ## Function giving greif
    function _droplist (){
    while read line; do

$IPT -A droplist -i eth1 -s $line -j LOG --log-prefix "IP BlockList "
$IPT -A droplist -i eth1 -s $line -j DROP

    done < $badlist    ##IPT is /sbin/iptables
    }

この関数を何度か繰り返すと、エラーが発生します。

    Try `iptables -h' or 'iptables --help' for more information.
    ' not found.4.12: host/network `SO.ME.IPH.ERE  

IP でハードコーディングされた同じスクリプトを実行しても問題なく動作します。これは $line に関係するものか、iptables の m 実装に関係しています。

乾杯 - 困惑。

4

3 に答える 3

0

Windows の行末 (\r\n) に起因する同様の問題がありました。UNIX エンディング (\n) に変換すると、問題が解決しました。

乾杯、/phfff

于 2014-04-28T18:23:46.967 に答える
0

これは、私にとって bash スクリプトへの初期の飛び込みでした。コードは、友人のボックスにリモートで配置されました。私が所有する最後の大まかなイテレーションは、ペーストビンにあります。

    #!/bin/bash
# ..
# ..
# ..

## Variables
stamp=$(date "+%d/%m/%Y %T")
seed="$RANDOM-$RANDOM-IPTABLES-$(date "+%d-%m-%Y")-TEMPORY"     ##  proverbial sandpit  
log=/root/.IPTables.log; touch $log                             ##  Always a logfile
dmp=/tmp/IPT_DUMP$seed.temp                                     ##  Intermediate
list=/tmp/IPT_LIST$seed.txt                                ##  F**ing '\r\r\n' regex'rs
pos=0

## Link(s)
link=http://au.hive.sshhoneypot.com/downloads/iplist.php


## Log File
function _tolog (){
        echo -e "$stamp - $@\r" >> $log
}

## Leadin'
_tolog "  "
_tolog "-----Running rottweiler : A simple IP deny auto script "
sh -c "iptables --flush"; _tolog "--OK Tables have been flushed"; sleep 1

## Grab-blacklist(s)            # Fortran array HO!
function _populate (){
        wget $link -O $dmp | egrep '[[:digit:]]{1,3}\.[[:digit:]]{1,3}\.[[:digit:]]{1,3}\.[[:digit:]]{1,3}' | xargs; _tolog "--OK blacklist stored from honeypot"
        tr -d '\r' < $dmp > $list # See above rage comment
        while read line; do
                arrayIp[$pos]=$line
                ((pos++))
        done < $list
        _tolog "--OK IP array created!"
        _tolog $(echo -e "---- Array size: ${#arrayIp[*]}")
        for item in ${arrayIp[*]}
        do
                 sh -c "/sbin/iptables -I INPUT -s $item -j DROP"    # This drops the current blacklist
        done; _tolog "--OK Commands passed to iptables DB"           # Use: /sbin/iptables -L -v -n to get list back quickly ( no resolving crap )
        /sbin/iptables-save > /root/iptables.backup; _tolog "--OK Table database saved to flatfile"
}

_populate
_tolog  "-----Terminating script: Tables logged in ~/iptables.backup"
于 2013-06-28T12:37:24.957 に答える