0

我正在寻找一个简单的示例,如何在 Jsch 中使用 Expect4j(使用 Shell 而不是 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

设置一个 SCPInfo 对象来保存用户名、密码、端口:22 和 ip。

    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

您的代码将 Shell 的 Inputstream/outputStream 连接到本地 java 进程的相同流。要在 shell 中执行命令,您必须在 shell 的输入流中提交这些命令(即不要将其连接到本地输入),如下所示:

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");

然后等到“日期”的结果来了,关闭通道。(您可能想先发送“退出”或“注销”。)

我不知道 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 回答