2016-04-26 11 views
0

ExpectItで使用するTelnet用Javaストリームを取得するにはどうすればよいですか? では、Apache Telnet as belowなどのJavaライブラリを使用し、Linux上で実際のTelnetクライアントを使用します。ExpectItのJavaストリームを取得してTelnetを駆動する方法

これは期待してからmanページのコンテキストである:

は期待し、他の対話型プログラム への「交渉」はスクリプトに従ったプログラムです。

以下のコメントがなされたprevious, vague, overly broad質問へのはるかに狭い範囲でのフォローアップの質問:他のすべてのJavaライブラリなど

ライブラリは、から来ることができる標準のJava ストリームを使用していますTelnetクライアント、ソケット接続、またはその他のストリームソースを使用することもできます。ここではtelnetの例である: github.com/Alexey1Gavrilov/ExpectIt/blob/master/expectit-core/... - アレクシー・ガフリラブ

ここではexample codeです:

package net.sf.expectit; 

/* 
* #%L 
* ExpectIt 
* %% 
* Copyright (C) 2016 Alexey Gavrilov and contributors 
* %% 
* Licensed under the Apache License, Version 2.0 (the "License"); 
* you may not use this file except in compliance with the License. 
* You may obtain a copy of the License at 
* 
*  http://www.apache.org/licenses/LICENSE-2.0 
* 
* Unless required by applicable law or agreed to in writing, software 
* distributed under the License is distributed on an "AS IS" BASIS, 
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
* See the License for the specific language governing permissions and 
* limitations under the License. 
* #L% 
*/ 

import static net.sf.expectit.matcher.Matchers.contains; 

import java.io.IOException; 
import org.apache.commons.net.telnet.TelnetClient; 

/** 
* A telnet client example showing weather forecast for a city. 
*/ 
public class TelnetExample { 
    public static void main(String[] args) throws IOException { 
     TelnetClient telnet = new TelnetClient(); 
     telnet.connect("rainmaker.wunderground.com"); 


     StringBuilder wholeBuffer = new StringBuilder(); 
     Expect expect = new ExpectBuilder() 
       .withOutput(telnet.getOutputStream()) 
       .withInputs(telnet.getInputStream()) 
       .withEchoOutput(wholeBuffer) 
       .withEchoInput(wholeBuffer) 
       .withExceptionOnFailure() 
       .build(); 

     expect.expect(contains("Press Return to continue")); 
     expect.sendLine(); 
     expect.expect(contains("forecast city code--")); 
     expect.sendLine("SAN"); 
     expect.expect(contains("X to exit:")); 
     expect.sendLine(); 

     String response = wholeBuffer.toString(); 
     System.out.println(response); 

     expect.close(); 
    } 
} 

も参照してください。https://stackoverflow.com/a/4532395/262852

答えて

1

テルネットクライアントプロセスを起動して、プロセスの入出力ストリームに接続することができます。次のようなものがあります。

 Process process = Runtime.getRuntime().exec("telnet rainmaker.wunderground.com"); 
     StringBuilder wholeBuffer = new StringBuilder(); 
     Expect expect = new ExpectBuilder() 
       .withOutput(process.getOutputStream()) 
       .withInputs(process.getInputStream()) 
       .withEchoOutput(wholeBuffer) 
       .withEchoInput(wholeBuffer) 
       .withExceptionOnFailure() 
       .build(); 

完全な例には、ここにはlinkがあります。

関連する問題