Pergunta

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!

Foi útil?

Solução

You need to read the output streams, I think. Otherwise the process can block, and you'll never see anything it says.

Outras dicas

You could also just try reading and parsing the file that iptables reads: "/etc/sysconfig/iptables".

Executes quicker than running the iptables binary

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top