Sharing _java with Java implementation telnet-client tools

Source: Internet
Author: User
Tags int size readline stringbuffer

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.

Copy Code code as follows:

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];
}
}

Test class

Copy Code code as follows:

static void T4 () {
Telnetutil tu = new Telnetutil ();
try {
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 ();
}

static void T1 () {
Telnetutil tu = new Telnetutil ();
try {
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 ();
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.