1

I'm writing a BASH script that outputs iptables -L -n and searches for the existence of an IP address. I'm stuck with how to use this with egrep. Roughly:

CHECK=$(iptables -L -n | egrep $the_string)

which "looks" like it would work, but it doesn't have an end delimiter $ so it would match:

25.24.244 and 25.24.24

When I really just need to match for 25.24.24 only.

I tried escaping this but the $ creates issues with the regular expression.

At least this is the only means I've found to search for the IP in the iptables system. It doesn't appear to have any query mechanism itself (puzzling).

I am probably missing something very simple here, and just need a pointer or two :-)

Thanks.

4

2 回答 2

0

You should backslash the . : this means any character in regex...

iptables -L -n | grep "25\.24\.24$"

(no need egrep there)

于 2012-11-30T20:16:31.627 回答
0

正则表达式的$末尾按预期工作:

the_ip=25.24.24
the_string=$(echo $the_ip | sed 's/\./\\\./g')
iptables -L -n | egrep "$the_string$"
于 2012-11-30T20:20:40.363 回答