Récupération info d'un serveur telnet
bonjour,
je viens a vous de nouveau. j'ai codé un programme qui me connecte a un serveur via telnet, tel que celui-ci:
Code:
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
|
public class telnet {
private TelnetClient telnet = new TelnetClient();
private InputStream in;
private PrintStream out;
private String prompt ="#";
public telnet(String server, String user, String password){
try{
//connexion au serveur
telnet.connect(server,23);
// recuperation des infos
in = telnet.getInputStream();
out= new PrintStream(telnet.getOutputStream());
// log utilisateur
readUntil("login: ");
write(user);
readUntil("Password: ");
write(password);
write("utoken");
for(int i=0; i<10;i++){
write("\n");
}
write("ls");
readUntil(prompt + "");
disconnect();
}catch(Exception ev){
ev.printStackTrace();
}
} |
ici ma fonction ecriture:
Code:
1 2 3 4 5 6 7 8 9 10
|
public void write(String value){
try{
out.println(value);
out.flush();
System.out.println(value);
}catch(Exception e){
e.printStackTrace();
}
} |
fonction deconnection
Code:
1 2 3 4 5 6 7 8 9
|
public void disconnect(){
try{
telnet.disconnect();
}catch(Exception e){
e.printStackTrace();
}
} |
Code:
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
| public void su(String password){
try{
write("su");
readUntil("Password: ");
write(password);
prompt = "#";
readUntil(prompt + "");
}catch(Exception e){
e.printStackTrace();
}
}
////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////
public String readUntil(String pattern){
try{
char lastChar = pattern.charAt(pattern.length()-1);
StringBuffer sb = new StringBuffer();
boolean found = false;
char ch = (char) in.read();
while(true){
System.out.append(ch);
sb.append(ch);
System.out.println(" / ");
if(ch == lastChar){
if(sb.toString().endsWith(pattern)){
return sb.toString();
}
}
ch=(char) in.read();
}
}catch(Exception e){
e.printStackTrace();
}
return null;
} |
bon jusqu'ici tout fonctionne, là j'envoie mon login, password, et ma commande "utoken". cette derniere me renvoie tous les utilisateurs qui utilisent le serveur et leur activité.
Mon probleme est que je voudrais récupérer ce flux d'info et les inscrire dans un texte pour les traiter. j'ai essayer plein de truc genre :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
public void recuperer(){
try{
InputStream is =telnet.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
String s = File.separator;
File file= new File("D:"+ s +"local"+s+"er82467"+s+"Autre"+s+"server_response.rtf");
FileWriter fw = new FileWriter(file);
fw.write(bis.toString());
fw.close();
}catch(Exception e){
e.printStackTrace();
}
} |
Que j'incorpore dans le public telnet().
Bah jme doute que l'on va me :fessee: ou :sm: , que ce que j'ai fait c'est du n'importe quoi et que sa marche pas ! j'ai tenté sans trop savoir où je mettais les pieds. mais bon faut bien expérimentez quand on sait pas !