O Telnet-Client é muito exausto, muito mais que o SSH-Client.
org.apache.commons.net.telnet.telnetclien --us o pacote Commons-Net do Apache, Commons-Net-3.0.1-bin.zip.
A cópia do código é a seguinte:
pacote org.sl.util;
importar org.apache.commons.net.telnet.telnetclient;
importar java.io.*;
importar java.nio.byteBuffer;
classe pública Telnetutil {
String charset = null;
byte [] buff = novo byte [2048];
TelnetClient telnetClient = new TelnetClient ();
TelnetReader do BufferReader TelnetReader = NULL;
BufferWriter telnetwirter = null;
InputStream telnetina = nulo;
OutputStream telnetout = null;
public Telnetutil () {
telnetClient = new TelnetClient ();
}
/**
* Conecte -se ao servidor
* @param ip
* Porta @param
* @THOWS UnsupportEdEncodingException
* @THOWSows IoException
*/
public void Connect (string ip, int porta) lança UnsupportEdEncodingingException, ioexception {
telnetClient.Connect (IP, porta);
SetiStream ();
}
/**
* Conecte -se ao servidor
* @param ip
* @THOWS UnsupportEdEncodingException
* @THOWSows IoException
*/
public void Connect (string ip) lança UnsupportEdEncodingException, ioException {
telnetClient.Connect (IP);
SetiStream ();
}
void SetiStream () lança UnsupportEdEncodingException {
telnetina = telnetclient.getInputStream ();
telnetout = telnetclient.getOutputStream ();
if (null == charset) {
telnetReader = new BufferReader (new InputStreamReader (telnetina));
telnetwirter = new bufferwriter (new outputStreamWriter (telnetout));
}outro{
telnetReader = new BufferredReader (new InputStreamReader (telnetina, charset));
telnetwirter = new bufferwriter (new outputStreamWriter (telnetout, charset));
}
}
/**
* Conecte-se
* Usuário @param
* @param passwd
* @return se o login é bem -sucedido.
* @THOWSows IoException
*/
public boolean Login (String User, String passwd) lança ioexception {
String read = readString ();
para (int i = 0;; i ++) {
if (-1 == read.indexof ("login")) {
leitura = readString ();
}outro{
quebrar;
}
}
writeText (usuário);
leitura = readString ();
para (int i = 0;; i ++) {
if (-1 == read.indexof ("senha")) {
leitura = readString ();
}outro{
quebrar;
}
}
writeText (passwd);
para(;;){
leitura = readString ();
//System.out.println("last:"+Read);
if (-1! = read.indexof ("last")) {
retornar true;
} else if (-1! = read.indexof ("incorreto")) {
retornar falso;
}
}
}
/**
* Este é um método de teste, apenas escreva -o casualmente.
* @THOWSows IoException
*/
public void show () lança ioexception {
// System.out.println (readString ());
// System.out.println (readString ());
// bytebuffer tmp = bytebuffer.allocate (1024);
// byte [] buff = novo byte [1024];
// while (telnetin.Available ()> 0) {
// int readlen = readbytes (buff, 0,1024);
// tmp.put (buff, 0, readlen);
//}
// System.out.println (new String (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) lança ioexception {
return telnetin.read (buff, offset, len);
}
/**
* Leia String <br/>
* é equivalente a converter readbyte () em uma string
* @retornar
* @THOWSows IoException
*/
public string readString () lança ioexception {
int readlen = readbytes (this.buff, 0, this.buff.length);
if (0 <readlen)
retornar nova string (buff, 0, readlen) .Trim ();
outro
retornar "";
}
/**
* Leia uma linha <br/>
* Se o servidor e o cliente não forem o mesmo sistema operacional, esse método poderá deixar de calcular a linha.
* @retornar
* @THOWSows IoException
*/
public string readline () lança ioexception {
String read = telnetReader.readline ();
return null == leia? "": read.trim ();
}
public void writeBytes (byte [] buff, int offset, int len) lança ioexception {
telnetout.write (buff, offset, len);
}
/**
* Escreva a string no servidor
* Texto @param
* @THOWSows IoException
*/
public void writeText (text string) lança ioexception {
telnetwirter.write (texto);
telnetwirter.write ('/r');
telnetwirter.write ('/n');
telnetwirter.flush ();
}
/**
* Execute o comando e retorne o resultado <br/>
* Equivalente a: <br>
* writeText ();
* return readString ();
* @param cmd
* @retornar
* @THOWSows IoException
*/
public String Exec (String cmd) lança ioexception {
writeText (cmd);
return readString ();
}
String Login1 (String User, String passwd) lança IoException {
String read = null;
readString ();
readString ();
leitura = readString ();
if (-1! = read.indexof ("login")) {
writeText (usuário);
}
leitura = readString ();
if (-1! = read.indexof ("senha")) {
writeText (passwd);
}
leitura = readString ();
leia += readString ();
Retornar leia;
// stringbuffer sb = new StringBuffer ();
// while (null! = (read = readString ())) {
// sb.append (leia);
//}
//
// return sb.toString ();
}
/**
* fechamento
*/
public void Close () {
tentar{
writeText ("Exit");
writeText ("Exit");
writeText ("Exit");
} catch (Exceção ex) {
}
tentar {
if (null! = telnetina) telnetin.close ();
} catch (Exceção e) {
}
tentar {
if (null! = telnetout) telnetout.close ();
} catch (Exceção e) {
}
tentar {
if (null! = telnetClient) telnetClient.disconnect ();
} catch (Exceção e) {
}
}
/**
* Defina o personagem definido durante a comunicação telnet <br/>
* Nota: Este conjunto de personagens não tem um relacionamento necessário com o conjunto de caracteres do lado do servidor <br/>
* Este método precisa ser chamado antes do Connect ()
* @param charset
*/
public void setcharset (string charset) {
this.charset = charset;
}
/**
* Redefina o tamanho do buff, padrão para 2048 bytes.
* tamanho do @param
*/
public void setBuffersize (int size) {
this.Buff = novo byte [tamanho];
}
}
Classe de teste
A cópia do código é a seguinte:
estático void t4 () {
Telnetutil tu = new telNetutil ();
tentar {
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 ();
tentar {
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 ();
}