获取 Telnet 的输出

发布于 2024-10-22 01:11:48 字数 232 浏览 1 评论 0原文

有人知道如何用 Java 读取 telnet 的输出吗?我能够连接到 服务器并执行一些命令,但我需要该命令的输出。

例如,命令 ls 给出所有文件和目录的列表,所以我想得到它 列出并在我的 Java 代码中使用它做一些事情。

我尝试过 Telnet 的第 3 方库,例如 apache-commons 和 sinetfactory(www.jscape.com )但我的案例没有结果......

伊戈尔

does anybody know how to read output from telnet with Java? I'm able to connect to
the server and execute some commands, but I need output from that commands.

For example, command ls gives a list of all files and directory so I want to get that
list and do something with it in my Java code.

I had tried 3rd party libraries for Telnet like apache-commons and sinetfactory(www.jscape.com
) but with no results for my case...

Igor

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

泼猴你往哪里跑 2024-10-29 01:11:48

您可以从进程InputStream读取输出,如下所示:

final Process process =
    new ProcessBuilder("path/to/telnet", "and", "some", "args").start();
final AtomicBoolean running = new AtomicBoolean(true);
final InputStream processData = process.getInputStream();

// start a thread to read process output
new Thread(new Runnable(){

    @Override
    public void run(){
        while(running.get()){

            // read processData

        }

    }
}).start();
process.waitFor();
running.set(false);

You can read the output from the process InputStream, something like this:

final Process process =
    new ProcessBuilder("path/to/telnet", "and", "some", "args").start();
final AtomicBoolean running = new AtomicBoolean(true);
final InputStream processData = process.getInputStream();

// start a thread to read process output
new Thread(new Runnable(){

    @Override
    public void run(){
        while(running.get()){

            // read processData

        }

    }
}).start();
process.waitFor();
running.set(false);
任性一次 2024-10-29 01:11:48

我知道你正在寻求一个java解决方案,但是expect脚本语言是为这种类型的事情开发的。 http://expect.sourceforge.net/

如果它必须是java,那么请无视。

I know you are asking for a java solution, but the expect scripting language was developed for this type of thing. http://expect.sourceforge.net/

If it has to be java, then please disregard.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文