Java捕获文件中的telnet输出

时间:2013-04-05 20:49:50

标签: java file telnet output

我正在尝试telnet到服务器,运行命令并将该命令的输出放在一个文件中。 我可以在文件中获取命令,但不能获得此命令的结果。 我也无法在控制台上看到我的输出,所以我认为它运行但我不确定。  有人有任何想法吗?

public final static void main(String[] args) throws IOException, InterruptedException
{
    FileOutputStream fout = null;

    try
    {
        fout = new FileOutputStream ("spyfile.log");
    }
    catch (IOException e)
    {
        System.err.println(
            "Exception while opening the spy file: "
            + e.getMessage());
    }


    TelnetClient telnet;

    telnet = new TelnetClient();

    try
    {

        telnet.connect("myserver", 23);
    }
    catch (IOException e)
    {
        e.printStackTrace();
        System.exit(1);
    }

    telnet.registerSpyStream(fout);
    PrintStream out = new PrintStream( telnet.getOutputStream() );
    out.println( "mycommand" );     

    try
    {
        telnet.disconnect();
    }
    catch (IOException e)
    {
        e.printStackTrace();
        System.exit(1);
    }

   fout.close();
    System.exit(0);  
}

1 个答案:

答案 0 :(得分:0)

我不确定什么是TelneClient,如果这是Commons Net类,那么你缺少实际读取在telnet会话期间交换的数据的部分。请运行this example并查看其工作原理,一旦获得它,您就可以根据自己的需要进行切割。