2013-03-07 91 views
0

用下面的代碼我想從spawn.send("ls | grep *.dat\n");讀取InputStream。我的執行Spawnable和它的方法​​,但沒有線索如何使用它。 ExpectJ docs說:​​- 獲取一個可讀取Spawn標準輸出的流。但是這並沒有讓我有任何進展。expectJ - 從spawn.send()讀取InputStream()

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

回答

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