通过命令行与 iptables 交互
我正在尝试创建一个 Java 程序作为 iptables 的前端。为了实现这一点,我使用 Java 的 Process 类并传递命令。
我想知道我的处理方式是否正确。例如,下面是一些将 iptables 重置为其默认设置的命令,这些命令将在终端中按顺序执行。我在这里正确使用 Process 类吗?
Process proc1 = Runtime.getRuntime().exec("iptables -P INPUT ACCEPT");
proc1.waitFor();
Process proc2 = Runtime.getRuntime().exec("iptables -P FORWARD ACCEPT");
proc2.waitFor();
Process proc3 = Runtime.getRuntime().exec("iptables -P OUTPUT ACCEPT");
proc3.waitFor();
Process proc4 = Runtime.getRuntime().exec("iptables -t nat -P PREROUTING ACCEPT");
proc4.waitFor();
感谢您的任何指示!
I'm trying to create a Java program that is a front-end for iptables. To accomplish this, I'm using Java's Process class and pass commands.
I'm wondering if I'm going about it correctly in general. For example, here is a selection of commands that resets iptables to its default settings, which are meant to be executed in a terminal sequentially. Am I using the Process class correctly here?
Process proc1 = Runtime.getRuntime().exec("iptables -P INPUT ACCEPT");
proc1.waitFor();
Process proc2 = Runtime.getRuntime().exec("iptables -P FORWARD ACCEPT");
proc2.waitFor();
Process proc3 = Runtime.getRuntime().exec("iptables -P OUTPUT ACCEPT");
proc3.waitFor();
Process proc4 = Runtime.getRuntime().exec("iptables -t nat -P PREROUTING ACCEPT");
proc4.waitFor();
Thanks for any direction!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为您需要读取输出流。否则该进程可能会阻塞,并且您将永远看不到它所说的任何内容。
You need to read the output streams, I think. Otherwise the process can block, and you'll never see anything it says.
您也可以尝试读取并解析 iptables 读取的文件:
“/etc/sysconfig/iptables”。
执行速度比运行
iptables
二进制文件更快You could also just try reading and parsing the file that
iptables
reads:"/etc/sysconfig/iptables".
Executes quicker than running the
iptables
binary