使用egrep从shell变量中搜索八位字节的IP地址

时间:2012-11-30 20:14:44

标签: bash scripting ip-address iptables grep

我正在编写一个输出iptables -L -n的BASH脚本,并搜索是否存在IP地址。我一直坚持如何使用 egrep 。大致是:

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

“看起来”它会起作用,但它没有结束分隔符$所以它会匹配:

25.24.24425.24.24

当我真的只需匹配25.24.24时。

我试图逃避这一点,但$会产生正则表达式的问题。

至少这是我在 iptables 系统中搜索IP的唯一方法。它本身似乎没有任何查询机制(令人费解)。

我可能在这里遗漏了一些非常简单的东西,只需要一两个指针: - )

感谢。

2 个答案:

答案 0 :(得分:0)

你应该反斜杠 .:这意味着正则表达式中的任何字符 ...

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

(不需要egrep

答案 1 :(得分:0)

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

the_ip=25.24.24
the_string=$(echo $the_ip | sed 's/\./\\\./g')
iptables -L -n | egrep "$the_string$"