Telnet-client too much, more than ssh-client, engaged in a day, do can use, still have to change.
Org.apache.commons.net.telnet.TelnetClien--Using Apache commons-net packs, Commons-net-3.0.1-bin.zip.
Package org.sl.util;
Import org.apache.commons.net.telnet.TelnetClient;
Import java.io.*;
Import Java.nio.ByteBuffer;
public class 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 ();
}
/**
* Connect to Server
* @param IP
* @param Port
* @throws unsupportedencodingexception
* @throws IOException
*/
public void Connect (String IP, int port) throws Unsupportedencodingexception,ioexception {
Telnetclient.connect (Ip,port);
Setiostream ();
}
/**
* Connect to Server
* @param IP
* @throws unsupportedencodingexception
* @throws IOException
*/
public void Connect (String IP) throws unsupportedencodingexception,ioexception {
Telnetclient.connect (IP);
Setiostream ();
}
void Setiostream () throws Unsupportedencodingexception {
Telnetin = Telnetclient.getinputstream ();
Telnetout = Telnetclient.getoutputstream ();
if (Null==charset) {
Telnetreader = new BufferedReader (new InputStreamReader (Telnetin));
Telnetwirter = new BufferedWriter (new OutputStreamWriter (telnetout));
}else{
Telnetreader = new BufferedReader (new InputStreamReader (Telnetin, CharSet));
Telnetwirter = new BufferedWriter (new OutputStreamWriter (Telnetout, CharSet));
}
}
/**
* Login
* @param user
* @param passwd
* @return Whether the login is successful.
* @throws IOException
*/
public boolean login (String user,string passwd) throws IOException {
String read = ReadString ();
for (int i=0; i++) {
if ( -1==read.indexof ("login")) {
Read = ReadString ();
}else{
Break
}
}
WRITETEXT (user);
read = ReadString ();
for (int i=0;; i++) {
if ( -1==read.indexof ("Password")) {
read = ReadString ();
}else{
break;
}
}
WRITETEXT (passwd);
for (;;) {
read = ReadString ();
//system.out.println ("Last:" +read);
if ( -1!=read.indexof ("last")) {
return true;
}else if ( -1!=read.indexof ("incorrect")) {
return false;
}
}
}
/**
* This is a test method, write casually.
* @throws ioexception
*/
public void Show () throws 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 (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) throws IOException {
Return Telnetin.read (Buff,offset,len);
}
/**
* Read String <br/>
* Equivalent to ReadByte () to String
* @return
* @throws IOException
*/
Public String readString () throws IOException {
int readlen = readbytes (this.buff, 0, this.buff.length);
if (0<readlen)
return new String (Buff,0,readlen). Trim ();
Else
Return "";
}
/**
* Read one line <br/>
* If the server and the client are not the same operating system, this method may cause the line to fail.
* @return
* @throws IOException
*/
Public String ReadLine () throws IOException {
String read = Telnetreader.readline ();
Return null==read? ": Read.trim ();
}
public void Writebytes (byte[] buff, int offset, int len) throws IOException {
Telnetout.write (Buff,offset,len);
}
/**
write string to server
* @param text
* @throws ioexception
*/
public void WriteText ( String text) throws IOException {
telnetwirter.write (text);
telnetwirter.write (' \ R ');
telnetwirter.write (' \ n ');
Telnetwirter.flush ();
}
/**
* Execute the command and return the result <br/>
* Equivalent to: <br>
* WRITETEXT (); <br/>
* return readString ();
* @param cmd
* @return
* @throws IOException
*/
public string exec (String cmd) throws IOException {
WRITETEXT (CMD);
return readString ();
}
String Login1 (String user,string passwd) throws IOException {
String read = null;
ReadString ();
ReadString ();
Read = ReadString ();
if ( -1!=read.indexof ("login")) {
WRITETEXT (user);
}
Read = ReadString ();
if ( -1!=read.indexof ("Password")) {
WRITETEXT (passwd);
}
Read = ReadString ();
Read + + readString ();
return read;
StringBuffer sb = new StringBuffer ();
while (null!= (read = ReadString ())) {
Sb.append (read);
// }
//
return sb.tostring ();
}
/**
* Close
*/
public void Close () {
try{
WRITETEXT ("Exit");
WRITETEXT ("Exit");
WRITETEXT ("Exit");
}catch (Exception ex) {
}
try {
if (Null!=telnetin) telnetin.close ();
catch (Exception e) {
}
try {
if (null!=telnetout) telnetout.close ();
catch (Exception e) {
}
try {
if (null!=telnetclient) Telnetclient.disconnect ();
catch (Exception e) {
}
}
/**
* Set the character set for Telnet communication <br/>
* Note: This character set is not necessarily related to the server-side character set <br/>
* This method needs to be called before Connect ()
* @param CharSet
*/
public void Setcharset (String charset) {
This.charset = CharSet;
}
/**
* Reset the buff size by default of 2048 bytes.
* @param size
*/
public void setbuffersize (int size) {
This.buff = new Byte[size];
}
}