Comment flushdns en java


J'essaie actuellement de trouver un moyen de vider mon cache dns via un programme en java. Lorsque j'exécute mon code, l'invite de commande apparaît mais je ne peux pas comprendre comment exécuter mon code.

import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;

class SyncPipe implements Runnable
{
public SyncPipe(InputStream istrm, OutputStream ostrm) {
  istrm_ = istrm;
  ostrm_ = ostrm;
 }
  public void run() {
  try
  {
      final byte[] buffer = new byte[1024];
      for (int length = 0; (length = istrm_.read(buffer)) != -1; )
      {
          ostrm_.write(buffer, 0, length);
      }
  }
  catch (Exception e)
  {
      e.printStackTrace();
  }
}

private final OutputStream ostrm_;
private final InputStream istrm_;
}
public class FlushCommand {

FlushCommand(InputStream errorStream, PrintStream err) {
    throw new UnsupportedOperationException("Not supported yet."); 
}

}

C'est la méthode principale ci-dessous:

    String command = "cmd /c start cmd.exe";
    {

};
Process p = null;
    try {// Execute command

Process child = Runtime.getRuntime().exec(command);

// Get output stream to write from it
OutputStream out = child.getOutputStream();

out.write("cd C:/ /r/n".getBytes());
out.flush();
out.write("dir /r/n".getBytes());
out.close();
        p = Runtime.getRuntime().exec(command);
    } catch (IOException ex) {
        Logger.getLogger(FlushDNS.class.getName()).log(Level.SEVERE, null, ex);
    }
new Thread((Runnable) new FlushCommand(p.getErrorStream(), System.err)).start();
new Thread((Runnable) new FlushCommnad(p.getInputStream(), System.out)).start();
PrintWriter stdin = new PrintWriter(p.getOutputStream());
stdin.println("dir c:\\ /A /Q");
stdin.println("ipconfig/flushdns");
// write any other commands you want here
stdin.close();
int returnCode = 0;
    try {
        returnCode = p.waitFor();
    } catch (InterruptedException ex) {
        Logger.getLogger(FlushDNS.class.getName()).log(Level.SEVERE, null, ex);
    }
System.out.println("Return code = " + returnCode);
}                                        
Author: ZebraDonkey, 2017-03-15

1 answers

Pourquoi ne pas commencer par très basique?

Votre commande ouvre simplement une fenêtre de commande. Si je me souviens bien, ce qui suit devrait vider votre DNS et rester non fermé (cmd ne sera pas automatiquement fermé).

String command = "cmd.exe /c start cmd.exe /c start ipconfig /flushdns";
try{
      Runtime.getRuntime.exec(command);
catch(...){}

Je dois vérifier mes anciens codes sur mon ordinateur pour m'assurer, mais je vous suggère de commencer à partir de basic, puis de faire du multi-threading, etc.

Vous pouvez également utiliser process builder pour créer votre commande.

 0
Author: smttsp, 2017-03-15 05:57:46