2013-05-01 39 views
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的作品精細編碼,它要麼是與$線或iptables的m的實施。

乾杯 - 擋板。

+0

通過修正了該問題$ IPT -A INPUT -s $ @ -j DROP ......仍然不確定,如果它是$線或黑名單格式化? – deviantlamb 2013-05-01 03:48:27

回答

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 
+0

感謝您的意見,壞名單就是這樣的:ipv4的問題列表是每行都包含\ r \ r \ n! – deviantlamb 2013-06-28 12:31:22

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 - [email protected]\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" 
0

由於Windows行尾(\ r \ n)導致類似問題。轉換爲unix結尾(\ n)解決了我的問題。移動迭代並傳回一個變量,如::

乾杯,/ phfff