package test;import org.apache.commons.net.telnet.*;
import java.io.*;
public class TmhTelnet {
    private static TelnetClient tc = null;
    private InputStream in;
    private PrintStream out;
    public static void main(String args[]) {
        new TmhTelnet("192.168.0.100", 23, "user", "密码");
    }    public TmhTelnet(String host, int port, String username, String password) {
        byte[] a = new byte[1024];
        if (intconnect(host, port)) {
            write(username);
            write(password);
            writescript("command.txt");//dir
            closeconnect();        }
    }    private void writescript(String filename) {
        try {
            if (new File(filename).exists()) {
                FileReader f = new FileReader(filename);
                BufferedReader reader = new BufferedReader(f);
                String command = "";
                while (command != null) {
                    command = reader.readLine();
                    write(command);
                }
            }
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }    }    public void write(String command) {
        try {
            System.out.println("command:>>>>>>>>>" + command);
            out.println(command);
            out.flush();
            printresponse2();
            //for(int i=0;i<800000000;i++);
            //茼蜆妏蚚盄最肮祭妏最唗婓諉忳善勤源眈茼綴婬硒俴狟珨沭韜鍔涴爵妏蚚for悜遠懂耀攜勤源腔眈茼奀潔
        } catch (Exception e) {
            e.printStackTrace();
        }
    }    private boolean intconnect(String host, int port) {
        try {
            tc = new TelnetClient();
            TerminalTypeOptionHandler ttopt = new TerminalTypeOptionHandler(
                    "vt100", false, false, true, false);
            EchoOptionHandler echoopt = new EchoOptionHandler(true, false,
                    true, false);
            SuppressGAOptionHandler gaopt = new SuppressGAOptionHandler(true,
                    true, true, true);            tc.addOptionHandler(ttopt);
            tc.addOptionHandler(echoopt);
            tc.addOptionHandler(gaopt);
            tc.connect(host, port);
            in = tc.getInputStream();
            out = new PrintStream(tc.getOutputStream());
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            try {
                tc.disconnect();
                in.close();
                out.close();
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            return false;
        }
    }    private void closeconnect() {
        try {
            tc.disconnect();
            in.close();
            //out.close();
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
    }    private String printresponse() {
        try {
            byte[] buff = new byte[1024];
            int ret_read = 0;
            do {
                ret_read = in.read(buff);
                String a = new String(buff, 0, ret_read);
                System.out.print("=" + a);
                if (a.endsWith(":") | a.endsWith(">") | a.endsWith("]")) {
                    System.out.print(a);
                    return null;
                }            } while (ret_read >= 0);
        } catch (Exception e) {
            System.err.println("Exception while reading socket:"
                               + e.getMessage());
        }        try {
            tc.disconnect();
        } catch (Exception e) {
            System.err.println("Exception while closing telnet:"
                               + e.getMessage());
        }
        return null;
    }    private String printresponse2() {
        try {
            byte[] buff = new byte[1024];
            int ret_read = 0;
            ret_read = in.read(buff);
            while (ret_read >= 0) {
                String a = new String(buff, 0, ret_read);
                a = a.trim();
                System.out.print("=" + a);
                if (a.endsWith(":") | a.endsWith(">") | a.endsWith("]")) {
                    System.out.print(a);
                    return null;
                }
            }
        } catch (Exception e) {
            System.err.println("Exception while reading socket:"
                               + e.getMessage());
        }        try {
            tc.disconnect();
        } catch (Exception e) {
            System.err.println("Exception while closing telnet:"
                               + e.getMessage());
        }
        return null;
    }}返回的是

那位高手有解决的方法
我是新手没有多少分

解决方案 »

  1.   

    这个东西啊,还真难办。
    这个东西好像叫做终端转意字符。具体英文忘记了。字符串的内容是怎么在一个屏幕上显示字符串。包括位置颜色什么的。有协议专门说这个,rfc文档有说这个东西。自己解析比较累,反正我最后放弃了。很多java开源的telnet工具都实现,当初看着比较复杂,也没有什么动力,不了了之了。给你的建议也只能使找个开源工具去查源码。
      

  2.   

    找到了,如果没有错,叫做Terminal Emulator
    wiki:
    http://en.wikipedia.org/wiki/Terminal_emulator上面描述比较清楚,不过涉及很多很多东西。
    我放弃了!自己玩这个东西,有点狠。
      

  3.   

    厉害!!高手我的乱码问题还在解决中。。测试获取dir的时候只有一部分的数据我先以为是stringbuffer的原因,但是sb可以存大于几十M的东西 我的才2k 。。不晓得是什么原因
      

  4.   

    最开始new TelnetClient()的时候没有设置参数,如下所示: 
    private TelnetClient telnet = new TelnetClient(); 
    远程登录AIX、linux、sun 服务器执行脚本返回信息都正常,但远程登录windows执行脚本返回信息是乱码。 后来在new TelnetClient()的时候设置了参数,如下所: 
    private TelnetClient telnet = new TelnetClient("VT220"); 
    登录Windows 2003 和XP执行脚本返回信息就正常了。 但是Windows 2000还是不行! 
    是不是TelnetClient不支持2000呀,麻烦大哥们分析分析! 
      

  5.   

    楼主你好,我也在做相同的工作,请问你的command.txt里写的什么命令。我在command.txt里写了类似open等telent命令,但是没有执行?