0

使用以下代码,我想从spawn.send("ls | grep *.dat\n");. 我的 calss 实现Spawnable了它的方法getStdout(),但不知道如何使用它。ExpectJ 文档说:getStdout()- 获取一个流,从中可以读取 Spawn 的标准输出。但这并没有让我更进一步。

    private void expectC() throws IOException, TimeoutException, ExpectJException, InterruptedException {   
        ExpectJ expectJ = new ExpectJ();
        Spawn spawn = expectJ.spawn("192.168.1.60", 22, "login", "pass");
        spawn.send("ls | grep *.dat\n");
        spawn.expectClose();
}
4

1 回答 1

0
    ExpectJ ex = new ExpectJ(5);
    Spawn spawn = ex.spawn(new SshSpawn("192.168.56.101", 22, "alice", "password"));
    String command = "ls | grep dat \n";
    spawn.send(command);
    try{
        spawn.expectClose();
    }catch(Exception e){}
    String lsResults = spawn.getCurrentStandardOutContents().split("grep dat")[2];
    String[] lsRows = lsResults.split("\r\n"); //remove the 1st and the last
于 2013-04-02T11:59:30.057 回答