【问题标题】:Telnet Apache Commons NET Printing Junk CharactersTelnet Apache Commons NET 打印垃圾字符
【发布时间】:2016-01-15 21:17:34
【问题描述】:

我们想在 Windows Server 2008/Windows 7 上使用 telnet 执行一些命令。由于每次登录并在大约 50 个相同的设备上运行命令都很乏味,所以我在 google 中搜索并搜索到 apache commons 并找到了一个示例。

它可以工作,但它正在打印一些垃圾字符(我认为这是 Windows 字符编码的一些问题,我是新手)。

package com.kiran.telnet;

import org.apache.commons.net.telnet.TelnetClient;

import java.io.InputStream;
import java.io.PrintStream;

public class AutomatedTelnetClient {
    private TelnetClient telnet = new TelnetClient();
    private InputStream in;
    private PrintStream out;
    private String prompt = ">";

    public AutomatedTelnetClient(String server, String user, String password) {
        try {
            // Connect to the specified server
            telnet.connect(server, 23);

            // Get input and output stream references
            in = telnet.getInputStream();
            out = new PrintStream(telnet.getOutputStream(), true);

            // Log the user on
            readUntil("login: ");
            write(user);
            readUntil("password: ");
            write(password);

            // Advance to a prompt
            readUntil(prompt + " ");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void su(String password) {
        try {
            write("su");
            readUntil("Password: ");
            write(password);
            prompt = ">";
            readUntil(prompt + " ");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public String readUntil(String pattern) {
        try {
            char lastChar = pattern.charAt(pattern.length() - 1);
            StringBuffer sb = new StringBuffer();
            boolean found = false;
            char ch = (char) in.read();
            while (true) {
                System.out.print(ch);
                sb.append(ch);
                if (ch == lastChar) {
                    if (sb.toString().endsWith(pattern)) {
                        return sb.toString();
                    }
                }
                ch = (char) in.read();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public void write(String value) {
        try {
            out.println(value);
            out.flush();
            System.out.println(value);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public String sendCommand(String command) {
        try {
            write(command);
            return readUntil(prompt);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public void disconnect() {
        try {
            telnet.disconnect();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        try {
            AutomatedTelnetClient telnet = new AutomatedTelnetClient(
                    "127.0.0.1", "Kiran", "artha");
            System.out.println("Got Connection...");
            telnet.sendCommand("hostname");
            //telnet.sendCommand("ipconfig");
            //telnet.sendCommand("ps -ef ");
            //System.out.println("run command");
            //telnet.sendCommand("ls ");
            //System.out.println("run command 2");
            telnet.disconnect();
            System.out.println("DONE");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

我运行时的输出是: 欢迎使用 Microsoft Telnet 服务

login: Kiran
Kiran
password: artha
[1;1H*===============================================================                [2;1HMicrosoft Telnet Server.                                                        [3;1H*===============================================================                [4;1HC:\Users\Kiran> Got Connection...
hostname
                                                                [5;1H[K[6;1H[K[7;1H[K[8;1H[K[9;1H[K[10;1H[K[11;1H[K[12;1H[K[13;1H[K[14;1H[K[15;1H[K[16;1H[K[17;1H[K[18;1H[K[19;1H[K[20;1H[K[21;1H[K[22;1H[K[23;1H[K[24;1H[K[25;1H[K[4;16Hhostname[5;1HKiran-PC[7;1HC:\Users\Kiran>DONE

“[”之前还有一些 ESC 字符

关于这个的任何帮助。

谢谢。

【问题讨论】:

    标签: java automation telnet apache-commons-net


    【解决方案1】:

    看看这个wiki article。这些符号只是控制字符,用于格式化终端中的输出。

    您可以尝试配置您的终端类型,如:

    TelnetClient telnet = new TelnetClient("dumb");
    

    或者您可以尝试使用TerminalTypeOptionHandler 进行配置。

    默认情况下,您的 telnet 客户端使用支持控制序列的终端类型 vt100' 创建。 dumb 不支持它们。但是您必须了解,不能保证远程服务器支持这种终端类型。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-06-28
      • 1970-01-01
      • 2017-08-06
      • 2019-05-09
      • 2018-02-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多