我在不同的Ubuntu服务器上有一个/etc/init.d/iptables start|stop|restart
脚本(这是一个普通的shell脚本)
对于每个新服务,我必须编辑并插入一行来打开一个端口。 这导致了不同机器上的init.d脚本的许多不同版本。
是否有可能自动包含/etc/iptables/include.d/
的所有文件?
目标是在/etc/init.d/iptables的start函数中只应该有一行
include /etc/iptables/include.d/*
在/etc/iptables/include.d/
一个额外的文件后,我只是说
/etc/init.d/iptables restart
编辑:正如Saurabh指出,当命令需要一定的顺序时,这可能会导致问题。 高级设置可以有不同的目录,如:
/etc/iptables/include01.d/ /etc/iptables/include02.d/ /etc/iptables/include03.d/
包括他们这样的:
包括/etc/iptables/include01.d/* ...也许一些代码在这里的主文件... 包括/etc/iptables/include02.d/* 包括/etc/iptables/include03.d/*
将以下行添加到您的init.d脚本。
run-parts --report /etc/iptables/include.d
它将以shell脚本的forms运行目录中的所有内容(需要可执行文件)。
如果您只想执行以.port结尾的文件,则可以使用如下所示的内容:
run-parts --regex '\.port$' /etc/iptables/include.d/
如果你想确保订单是正确的,你可以命名文件:
10_web.port 20_ssh.port etc..
for f in /etc/iptables/include.d/* . $f done
注意点和%f之间的空格
Saurabh是正确的 – 这不会像你打算的那样必要的工作,但是使用一些命名约定,例如10-xxx,20-yyy等等,这可能是可控的。
您也可以考虑从模板文件构buildiptables脚本,其中之一是原始的iptables脚本。 创build一个脚本,它将读取相关目录中的模板文件,并从中创build一个新的iptables脚本。 这样,当您需要在模板中进行更改时,只需重新运行脚本生成器即可。
使用这种方法,您甚至可以在基本模板中使用幻灯片并放置标记,该标记可用于指示何时包含来自模板树中特定目录的文件。
你可以在bash中定义简单的函数:
function include() { for FILE in $( find "$1" -type f -print | sort ) do source $FILE done }
接着:
include some_dir/*
甚至:
include some_dir/*.conf
我不认为你可以在iptablesconfiguration中包含文件。 这个select是有道理的,因为防火墙规则很大程度上取决于它们被写入的顺序。 如果我们只是在文件夹中包含文件,iptables将不知道哪些规则先放置,哪些放在后面。