Java實現的Expect工具:Expectit
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);
本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!