1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
| import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
public class Console {
private InputStream std;
private OutputStream out;
private InputStream err;
Process p;
Runtime r;
private BufferedReader getOutput(Process p) {
return new BufferedReader(new InputStreamReader(p.getInputStream()));
}
private BufferedReader getError(Process p) {
return new BufferedReader(new InputStreamReader(p.getErrorStream()));
}
void commande(String commande) throws IOException, InterruptedException
{
commande(commande,false);
}
void commande(String commande,boolean cacherCommande) throws IOException, InterruptedException
{
out.write((commande+"\n").getBytes ());
out.flush();
if(!cacherCommande) System.out.println ("< "+commande);
//MON PROBLEME
//p.waitFor();
//Thread.sleep (1000);
viderFlux() ;
}
void viderFlux() throws IOException
{
int value = 0;
if (std.available () > 0) {
System.out.print("> ");
value = std.read ();
System.out.print ((char) value);
while (std.available () > 0) {
value = std.read ();
System.out.print ((char) value);
}
}
if (err.available () > 0) {
System.out.println ("ERREUR:");
value = err.read ();
System.out.print ((char) value);
while (err.available () > 0) {
value = err.read ();
System.out.print ((char) value);
}
}
}
void demarrer()
{
try {
String adresseServeur="000.000.000.000";
String command = "plink "+adresseServeur;
r = Runtime.getRuntime ();
p = r.exec (command);
std = p.getInputStream ();
out = p.getOutputStream ();
err = p.getErrorStream ();
viderFlux();
commande("root");
commande("******");
p.destroy ();
}
catch (Exception e) {
e.printStackTrace ();
}
}
} |
Partager