Jsch shell/Expecdt4j 简单示例?

发布于 2024-10-21 04:42:27 字数 717 浏览 5 评论 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 分钟。

I'm looking for a simple example how to use Expect4j in Jsch(using Shell not exec)
I mean how to send commands(~8) to the server, and how to printout the response.

so far I have this:

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

I want to send commands like this: command=("lrp_list;newgrp xxx;date"); send(command);
also some examples i have found only work with time restrictions; and i need something like in above code that would excecute a command even if excecution takes 15 min.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

樱娆 2024-10-28 04:42:27

设置 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);
}

Setup an SCPInfo object to hold the username, password, port:22 and 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);
}
霞映澄塘 2024-10-28 04:42:27

您拥有的代码将 Shell 的输入流/输出流连接到本地 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");

然后等待“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

The code you have connects the Inputstream/outputStream of the Shell to the same streams of the local java process. For executing commands in the shell, you have to submit these commands in the inputstream of the shell (i.e. not connect it to the local input), like this:

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

Then wait until the result of "date" comes, and close the channel. (You may want to send an "exit" or "logout" first.)

I don't know expect4j, but I assume you can feed it a pair of InputStream and OutputStream - then use getInputStream instead of setOutputStream here.


Okay, having found the source for Expect4j.java, I would do it like this:

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文