Java实现的Expect工具:Expectit

jopen 10年前

Expectit - 是纯 Java 1.6+ 实现的 Expect 工具。简单易用和可扩展,全新编写无需依赖第三方库,使用管道和非堵塞 API 基于 NIO 实现。

expect用于自动化地执行linux环境下的命令行交互任务,例如scp、ssh之类需要用户手动输入密码然后确认的任务。有了这个工具,定义在scp过程中可能遇到的情况,然后编写相应的处理语句,就可以自动地完成scp操作了

Maven:

<dependency>      <groupId>net.sf.expectit</groupId>      <artifactId>expectit-core</artifactId>      <version>0.3.0</version>  </dependency>

使用方法:

// the stream from where you read your input data  InputStream inputStream = ...;  // the stream to where you send commands  OutputStream outputStream = ...;  Expect expect = new ExpectBuilder()      .withInputs(inputStream)      .withOutput(outputStream)      .build();  expect.sendLine("command").expect(contains("string"));  Result result = expect.expect(regexp("(.*)--?--(.*)"));  // accessing the matching group  String group = result.group(2);

项目主页:http://www.open-open.com/lib/view/home/1395712080884