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

1

$badlist 包含什么?文件名还是 IP 列表?

如果它是一个文件名,它应该像你所做的那样工作,但如果它是一个 ip 列表,你必须改变你阅读它们的方式。

假设它是一个以换行符分隔的 IP 列表,例如:

$ badlist="1.1.1.1\n2.2.2.2\n3.3.3.3" 
$ echo -e "$badlist"
1.1.1.1
2.2.2.2
3.3.3.3

那么你必须修改循环如下:

$ echo -e "$badlist"|while read line; do 
    # do stuff with $line
done
于 2013-05-01T10:59:58.547 回答
0

对我来说,这是对 bash 脚本的早期探索,代码也被远程放置在好友框上,我拥有的最后一次粗略迭代是在我的 pastebin 上:

    #!/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 回答
0

由于 Windows 行尾 (\r\n) 导致了类似的问题。转换为 unix 结尾 (\n) 解决了我的问题。

干杯,/phfff

于 2014-04-28T18:23:46.967 回答