需求:从某文件中读取ip地址以及端口号,用iptables打开该ip以及端口

比如文件内容:

[root@localhost xxx]# cat ip.txt
192.168.1.20 22
192.168.1.30 10
192.168.1.30 33

手动操作为:

[root@localhost huwei]# iptables -A INPUT -s 192.168.66.20  -i eth0 -p tcp --dport 22 -j ACCEPT

改写使用脚本读取文件内容,自动添加iptables防火墙规则,脚本为:

[root@localhost xxx]# cat ip.sh
#!/bin/sh
wan0='eth0'
FILE='/home/huwei/ip.txt'
while read i
do
a=$(echo "$i"|awk -F ' ' '{print $1}')
b=$(echo "$i"|awk -F ' ' '{print $2}')
#此处显示变量的值,其实可以省略
echo "$a"
echo "$b"
iptables -A INPUT -s $a  -i $wan0 -p tcp --dport $b -j ACCEPT
/etc/init.d/iptables save
done<$FILE

土办法,我觉得应该还有更好的办法可以实现这样的功能