开发者

Get output from Telnet

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 开发者_JS百科case...

Igor


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜