0

JschでExpect4jを使用する方法(execではなくシェルを使用)の簡単な例を探しています。コマンド(〜8)をサーバーに送信する方法と、応答を出力する方法を意味します。

これまでのところ、私はこれを持っています:

  JSch jsch=new JSch();
  String host="www.superserver.uk.com";
  String user="tom1234";
  String passwd="12345a";
  Session session=jsch.getSession(user, host, 22);
  session.setPassword(passwd);
  session.setConfig("StrictHostKeyChecking", "no"); // if yes nothing works, but we're secure!
  session.connect();  
  Channel channel=session.openChannel("shell");//only shell 
  channel.setInputStream(System.in);// enter lrp_list
  channel.setOutputStream(System.out);

次のようなコマンドを送信したい: command=("lrp_list;newgrp xxx;date"); 送信 (コマンド); また、私が見つけたいくつかの例は、時間制限でのみ機能します。そして、実行に15分かかってもコマンドを実行する上記のコードのようなものが必要です。

4

2 に答える 2

1

ユーザー名、パスワード、ポート 22、および IP を保持する SCPInfo オブジェクトをセットアップします。

    List<String> commands = new ArrayList<String>();
    commands.add("touch test1.txt");
    commands.add("touch test2.txt");
    commands.add("touch test3.txt");
    runCommands(scpInfo, commands);

public static void runCommands(SCPInfo scpInfo, List<String> commands){
    try {
        JSch jsch = new JSch();
        Session session = jsch.getSession(scpInfo.getUsername(), scpInfo.getIP(), scpInfo.getPort());
        session.setPassword(scpInfo.getPassword());
        setUpHostKey(session);
        session.connect();

        Channel channel=session.openChannel("shell");//only shell  
        channel.setOutputStream(System.out); 
        PrintStream shellStream = new PrintStream(channel.getOutputStream());  // printStream for convenience 
        channel.connect(); 
        for(String command: commands) {
            shellStream.println(command); 
            shellStream.flush();
        }

        Thread.sleep(5000);

        channel.disconnect();
        session.disconnect();
    } catch (Exception e) { 
        System.err.println("ERROR: Connecting via shell to "+scpInfo.getIP());
        e.printStackTrace();
    }
}

private static void setUpHostKey(Session session) {
    // Note: There are two options to connect
    // 1: Set StrictHostKeyChecking to no
    //    Create a Properties Object
    //    Set StrictHostKeyChecking to no
    //    session.setConfig(config);
    // 2: Use the KnownHosts File
    //    Manually ssh into the appropriate machines via unix
    //    Go into the .ssh\known_hosts file and grab the entries for the hosts
    //    Add the entries to a known_hosts file
    //    jsch.setKnownHosts(khfile);
    java.util.Properties config = new java.util.Properties();
    config.put("StrictHostKeyChecking", "no");
    session.setConfig(config);
}
于 2012-03-24T22:30:51.677 に答える
0

あなたが持っているコードは、シェルの Inputstream/outputStream をローカル Java プロセスの同じストリームに接続します。シェルでコマンドを実行するには、次のように、これらのコマンドをシェルの入力ストリームに送信する必要があります (つまり、ローカル入力に接続しないでください)。

JSch jsch=new JSch();
String host="www.superserver.uk.com";
String user="tom1234";
String passwd="12345a";
Session session=jsch.getSession(user, host, 22);
session.setPassword(passwd);
session.setConfig("StrictHostKeyChecking", "no"); // if yes nothing works, but we're secure!
session.connect();  
Channel channel=session.openChannel("shell");//only shell 
channel.setOutputStream(System.out);
PrintStream shellStream = new PrintStream(channel.getOutputStream());  // printStream for convenience
channel.connect();
shellStream.println("lrp_list");
shellStream.println("newgrp xxx");
shellStream.println("date");

そして「日付」の結果が来るまで待って、チャンネルを閉じます。(最初に「exit」または「logout」を送信することをお勧めします。)

私はexpect4jを知りませんが、InputStreamとOutputStreamのペアをフィードできると思います-その後、ここgetInputStreamの代わりに使用してsetOutputStreamください。


さて、Expect4j.java のソースを見つけたら、次のようにします。

JSch jsch=new JSch();
String host="www.superserver.uk.com";
String user="tom1234";
String passwd="12345a";
Session session=jsch.getSession(user, host, 22);
session.setPassword(passwd);
session.setConfig("StrictHostKeyChecking", "no"); // if yes nothing works, but we're secure!
session.connect();  
Channel channel=session.openChannel("shell");//only shell 
Expect4j expect = new Expect4j(channel.getInputStream(), channel.getOutputStream());
// use expect methods
于 2011-04-06T12:48:58.700 に答える