欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

使用java实现telnet-client工具分享

程序员文章站 2024-02-27 14:20:27
telnet-client太费尽了,比ssh-client费尽的多,搞了一天,凑合能用,还得改。org.apache.commons.net.telnet.telnetcl...

telnet-client太费尽了,比ssh-client费尽的多,搞了一天,凑合能用,还得改。
org.apache.commons.net.telnet.telnetclien --使用了apache的commons-net包,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();
    }

    /**
     * 连接至服务器
     * @param ip
     * @param port
     * @throws unsupportedencodingexception
     * @throws ioexception
     */
    public void connect(string ip, int port) throws unsupportedencodingexception,ioexception {
        telnetclient.connect(ip,port);
        setiostream();
    }

    /**
     * 连接至服务器
      * @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));
        }
    }

    /**
     * 登录
     * @param user
     * @param passwd
     * @return 是否登录成功.
     * @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;
            }
        }
    }

    /**
     * 这是一个测试方法,随便写。
     * @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);
    }

    /**
     * 读取字符串<br/>
     * 相当于readbyte()转为字符串
     * @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 "";
    }

    /**
     * 读取一行<br/>
     * 如果服务器与客户端不是同一种操作系统,可能导致此方法计行失败。
     * @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);
    }

    /**
     * 向服务器写字符串
     * @param text
     * @throws ioexception
     */
    public void writetext(string text) throws ioexception {
        telnetwirter.write(text);
        telnetwirter.write('\r');
        telnetwirter.write('\n');
        telnetwirter.flush();
    }

    /**
     * 执行命令,并返回结果<br/>
     * 相当于: <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();
    }

    /**
     * 关闭
     */
    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) {
        }
    }

    /**
     * 设置telnet通信时的字符集<br/>
     * 注:此字符集与服务器端字符集没有必然关系<br/>
     * 此方法需在connect()前调用
     * @param charset
     */
    public void setcharset(string charset ){
        this.charset = charset;
    }

    /**
     * 重新设置buff大小,默认为2048字节.
     * @param size
     */
    public void setbuffersize(int size){
        this.buff = new byte[size];
    }
}

测试类

复制代码 代码如下:

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();
    }