^$ 和 ^# 是什么意思?

QCh*_*yễn 17 regex grep

我不明白BADIPS=$(egrep -v "^#|^$" $tDB)。你能解释一下吗?完整代码:

#!/bin/bash
# Purpose: Block all traffic from AFGHANISTAN (af) and CHINA (CN). Use ISO code. #
# See url for more info - http://www.cyberciti.biz/faq/?p=3402
# Author: nixCraft <www.cyberciti.biz> under GPL v.2.0+
# -------------------------------------------------------------------------------
ISO="af cn" 

### Set PATH ###
IPT=/sbin/iptables
WGET=/usr/bin/wget
EGREP=/bin/egrep

### No editing below ###
SPAMLIST="countrydrop"
ZONEROOT="/root/iptables"
DLROOT="http://www.ipdeny.com/ipblocks/data/countries"

cleanOldRules(){
$IPT -F
$IPT -X
$IPT -t nat -F
$IPT -t nat -X
$IPT -t mangle -F
$IPT -t mangle -X
$IPT -P INPUT ACCEPT
$IPT -P OUTPUT ACCEPT
$IPT -P FORWARD ACCEPT
}

# create a dir
[ ! -d $ZONEROOT ] && /bin/mkdir -p $ZONEROOT

# clean old rules
cleanOldRules

# create a new iptables list
$IPT -N $SPAMLIST

for c  in $ISO
do 
    # local zone file
    tDB=$ZONEROOT/$c.zone

    # get fresh zone file
    $WGET -O $tDB $DLROOT/$c.zone

    # country specific log message
    SPAMDROPMSG="$c Country Drop"

    # get 
    BADIPS=$(egrep -v "^#|^$" $tDB)
    for ipblock in $BADIPS
    do
       $IPT -A $SPAMLIST -s $ipblock -j LOG --log-prefix "$SPAMDROPMSG"
       $IPT -A $SPAMLIST -s $ipblock -j DROP
    done
done

# Drop everything 
$IPT -I INPUT -j $SPAMLIST
$IPT -I OUTPUT -j $SPAMLIST
$IPT -I FORWARD -j $SPAMLIST

# call your other iptable script
# /path/to/other/iptables.sh

exit 0
Run Code Online (Sandbox Code Playgroud)

mur*_*uru 31

^是一个正则表达式的特殊字符,用于标记行首,$标记行尾。它们用于在这些点锚定表达式。因此,^#是开始任何线#,并且^$是一个空行(因为有开始和结束之间没有任何东西)。

-vingrep否定匹配,因此此命令正在查找未注释掉(不以 开头#)或为空的行。


kar*_*rel 15

egrep 搜索匹配模式的文件。

egrep的-v (or --invert-match) 选项反转匹配的意义,以选择不匹配的行。

"^#|^$"计算结果为空行或以 # 开头的行,这是注释行,两者都不会被 bash 执行。反转匹配的计算结果不是空行或注释行。

$tDB 是一个变量,用于存储本地区域文件的值。

总而言之,坏 IP(要阻止的 IP)存储在 BADIPS 中,BADIPS 存储从本地区域文件列表中获取的坏 IP 的值。