[心缘地方]同学录
首页 | 功能说明 | 站长通知 | 最近更新 | 编码查看转换 | 代码下载 | 常见问题及讨论 | 《深入解析ASP核心技术》 | 王小鸭自动发工资条VBA版
登录系统:用户名: 密码: 如果要讨论问题,请先注册。

[转帖]Java中使用TelnetClient的一个范例

上一篇:[整理]Java如何判断单次输入流结束?
下一篇:[转帖]Java中的Socket用法详解

添加日期:2011/1/26 15:47:46 快速返回   返回列表 阅读6154次

还是要通过#这个提示符来判断信息返回结束了。

重点看下readUntil方法怎么写的。

用户名或密码错误的时候,结束提示符号可能就不是'#'了,可能是冒号':',
让你再输入用户名。

某个命令的输出,可能要敲个空格,才能继续输出。

哈哈,要考虑的问题多多……

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

            // 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″,
            “username”,
            “password”);
            telnet.sendCommand(“ps -ef “);
            telnet.disconnect();
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
}
=========================================
 

评论 COMMENTS
没有评论 No Comments.

添加评论 Add new comment.
昵称 Name:
评论内容 Comment:
验证码(不区分大小写)
Validation Code:
(not case sensitive)
看不清?点这里换一张!(Change it here!)
 
评论由管理员查看后才能显示。the comment will be showed after it is checked by admin.
CopyRight © 心缘地方 2005-2999. All Rights Reserved