Contenu | Rechercher | Menus

Annonce

Si vous avez des soucis pour rester connecté, déconnectez-vous puis reconnectez-vous depuis ce lien en cochant la case
Me connecter automatiquement lors de mes prochaines visites.

À propos de l'équipe du forum.

#1 Le 11/05/2011, à 21:36

hichem tunis

executer iptables via java

salut à tous,

je trouve un programme qu'execute la commande shell de linux en java,

tout les commandes marchent bien mais j'ai un probleme au niveau de iptables.

[u]voir le code main:[/u]
public class iptables {
    public static void main(String args[])
    {
        try{
Runtime runtime = Runtime.getRuntime();
//String cmdline= "sh ~/home/atoui/iptables.sh";
//String[] cmd1 = { "/bin/sh", "-c", "ping 192.168.1.1"};
String[] cmd = { "/bin/sh", "-c", "sudo iptables -A INPUT -d 192.168.1.7 -j DROP >fichier.txt" };
Process p = runtime.exec(cmd);
Thread stdTh = new InputStreamConsumerThread("STDStream", p.getInputStream(), System.out);
Thread errTh = new InputStreamConsumerThread("ErrorStream", p.getErrorStream(), System.out);
errTh.start();
stdTh.start();

// Attends la fin du process
p.waitFor();



}catch(Exception e) {
System.out.println("erreur d'execution");
}
    }
}

[u]et la class InputStreamConsumerThread:[/u]

import java.io.OutputStream;
import java.io.InputStream;
import java.io.IOException;

public class InputStreamConsumerThread
extends Thread
{

private InputStream _in;
private OutputStream _out;

public InputStreamConsumerThread(ThreadGroup group, String name, InputStream in, OutputStream out)
{
super(group, name);
setDaemon(true);
if ( in == null )
{
throw new IllegalArgumentException("InputStream argument cannot be null");
}
_in = in;
_out = out;
}

public InputStreamConsumerThread(String name, InputStream in, OutputStream out)
{
this(null, name, in, out);
}

public void run()
{
byte[] buf = new byte[512];
int count = 0;
try
{
while ( (count = _in.read(buf)) != -1 )
{
if ( _out != null )
{
_out.write(buf, 0, count);
}
}
}
catch (IOException e)
{
e.printStackTrace();
}
}
}

Dernière modification par slasher-fun (Le 11/05/2011, à 21:50)

Hors ligne