expectJ - Reading InputStream from spawn.send()

861 Views Asked by At

With the following code I'd like to read the InputStream from spawn.send("ls | grep *.dat\n");. My calss implements Spawnable and it's method getStdout(), but have no clue how to use it. ExpectJ docs says: getStdout() - Get a stream from which the Spawn's stdout can be read. But this doesn't get me any futher.

    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();
}
1

There are 1 best solutions below

0
On
    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