El Telnet-Client está demasiado agotado, mucho más que el SSH-Client.
org.apache.commons.net.telnet.telnetclien-Usó el paquete Commons-Net de Apache, Commons-Net-3.0.1-Bin.zip.
La copia del código es la siguiente:
paquete org.sl.util;
importar org.apache.commons.net.telnet.telnetclient;
import java.io.*;
import java.nio.bytebuffer;
clase pública Telnetutil {
String Charset = NULL;
byte [] buff = new Byte [2048];
TelnetClient TelnetClient = new TelnetClient ();
BufferedReader TelnetReader = null;
Bufferedwriter telnetwirter = null;
InputStream Telnetin = null;
OutputStream TelnetOut = NULL;
Public TelnetUtil () {
TelnetClient = new TelnetClient ();
}
/**
* Conéctese al servidor
* @param ip
* @param puerto
* @throws UnsupportedEncodingException
* @throws ioexception
*/
public void Connect (String IP, int Port) lanza UnspportedEncodingException, IOException {
TelnetClient.Connect (IP, puerto);
setiostream ();
}
/**
* Conéctese al servidor
* @param ip
* @throws UnsupportedEncodingException
* @throws ioexception
*/
public void Connect (String IP) lanza UnspportedEncodingException, ioexception {
TelnetClient.Connect (IP);
setiostream ();
}
void setiostream () lanza sin apoyoCodingEdingException {
Telnetin = TelnetClient.GetInputStream ();
TelnetOut = TelnetClient.getOutputStream ();
if (null == charset) {
TelnetReader = new BufferedReader (new InputStreamReader (Telnetin));
TelnetWirter = new BufferedWriter (nuevo OutputStreamWriter (TelnetOut));
}demás{
TelnetReader = new BufferedReader (new InputStreamReader (Telnetin, Charset));
TelnetWirter = new BufferedWriter (nuevo OutputStreamWriter (TelnetOut, Charset));
}
}
/**
* Acceso
* @param usuario
* @param passwd
* @return si el inicio de sesión es exitoso.
* @throws ioexception
*/
Public Boolean Login (String User, String Passwd) lanza ioexception {
String read = readString ();
para (int i = 0;; i ++) {
if (-1 == read.indexof ("login")) {
read = readString ();
}demás{
romper;
}
}
WriteText (usuario);
read = readString ();
para (int i = 0;; i ++) {
if (-1 == read.indexof ("contraseña")) {
read = readString ();
}demás{
romper;
}
}
WriteText (passwd);
para(;;){
read = readString ();
//System.out.println("last:"+read);
if (-1! = read.indexof ("Última")) {
devolver verdadero;
} else if (-1! = read.indexof ("incorrecto")) {
devolver falso;
}
}
}
/**
* Este es un método de prueba, simplemente escríbelo casualmente.
* @throws ioexception
*/
public void show () lanza ioexception {
// System.out.println (readString ());
// System.out.println (readString ());
// bytebuffer tmp = bytebuffer.allocate (1024);
// byte [] buff = new Byte [1024];
// while (telnetin.available ()> 0) {
// int readlen = readbytes (buff, 0,1024);
// tmp.put (buff, 0, readlen);
//}
// System.out.println (nueva cadena (tmp.array ()));
System.out.println ("1"+readString ());
System.out.println ("2"+readString ());
System.out.println ("3"+readString ());
WriteText ("root");
System.out.println ("4" + readString ());
WriteText ("123456");
System.out.println ("5"+readString ());
// system.out.println ("6"+readString ());
// system.out.println ("7"+readString ());
}
public int Readbytes (byte [] buff, int offset, int len) lanza ioexception {
return telnetin.read (buff, offset, len);
}
/**
* Leer cadena <br/>
* es equivalente a convertir readbyte () en una cadena
* @devolver
* @throws ioexception
*/
public string readString () lanza ioexception {
int readLen = readbytes (this.buff, 0, this.buff.length);
if (0 <readlen)
devolver nueva cadena (buff, 0, readlen) .trim ();
demás
devolver "";
}
/**
* Leer una línea <br/>
* Si el servidor y el cliente no son el mismo sistema operativo, este método puede no calcular la línea.
* @devolver
* @throws ioexception
*/
public string readline () lanza ioexception {
String Read = TelnetReader.ReadLine ();
return null == Read? "": read.trim ();
}
public void writeBytes (byte [] buff, int offset, int len) lanza ioexception {
TelnetOut.Write (Buff, Offset, Len);
}
/**
* Escribir una cadena al servidor
* @param text
* @throws ioexception
*/
public void writeText (texto de cadena) lanza ioexception {
Telnetwirter.write (texto);
TelnetWirter.Write ('/r');
TelnetWirter.Write ('/n');
Telnetwirter.flush ();
}
/**
* Ejecutar el comando y devolver el resultado <br/>
* Equivalente a: <br>
* WriteText ();
* return ReadString ();
* @param cmd
* @devolver
* @throws ioexception
*/
public string exec (string cmd) lanza ioexception {
WriteText (CMD);
return readString ();
}
String Login1 (String User, String Passwd) lanza ioexception {
String read = null;
readString ();
readString ();
read = readString ();
if (-1! = read.indexof ("login")) {
WriteText (usuario);
}
read = readString ();
if (-1! = read.indexof ("contraseña")) {
WriteText (passwd);
}
read = readString ();
read += readString ();
regresar lectura;
// StringBuffer sb = new StringBuffer ();
// while (null! = (read = readString ())) {
// sb.append (leer);
//}
//
// return sb.ToString ();
}
/**
* Cierre
*/
public void Close () {
intentar{
WriteText ("Salir");
WriteText ("Salir");
WriteText ("Salir");
} Catch (Exception Ex) {
}
intentar {
if (null! = Telnetin) Telnetin.Close ();
} capt (excepción e) {
}
intentar {
if (null! = TelnetOut) TelnetOut.Close ();
} capt (excepción e) {
}
intentar {
if (null! = TelnetClient) TelnetClient.Disconnect ();
} capt (excepción e) {
}
}
/**
* Establezca el conjunto de caracteres durante la comunicación de Telnet <br/>
* Nota: Este conjunto de caracteres no tiene una relación necesaria con el conjunto de caracteres del lado del servidor <br/>
* Este método debe llamarse antes de conectar ()
* @param charset
*/
public void setcharset (string charset) {
this.charset = charset;
}
/**
* Restablecer el tamaño de Buff, predeterminado a 2048 bytes.
* @Param tamaño
*/
public void setBuffersize (int tamaño) {
this.buff = nuevo byte [tamaño];
}
}
Clase de prueba
La copia del código es la siguiente:
void estático t4 () {
TelnetUtil tu = new TelnetUtil ();
intentar {
Tu.connect ("192.168.2.154");
System.out.println (Tu.login ("root", "123456"));
//tu.show ();
//System.out.println (Tu.ReadString ());
//System.out.println(tu.exec("pwd "));
System.out.println (Tu.exec ("echo /"123456789/">1.txt"));
System.out.println (Tu.exec ("Cat 1.txt"));
} Catch (ioException e) {
E.PrintStackTrace ();
}
Tu.close ();
}
void estático t1 () {
TelnetUtil tu = new TelnetUtil ();
intentar {
Tu.connect ("192.168.2.154");
System.out.println (Tu.login ("SL1", "Coffee8215"));
//tu.show ();
//System.out.println (Tu.ReadString ());
System.out.println (tu.exec ("pwd"));
} Catch (ioException e) {
E.PrintStackTrace ();
}
Tu.close ();
}