java 如何使用 JSch 执行多项操作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7419513/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 19:53:28  来源:igfitidea点击:

How to perform multiple operations with JSch

javaandroidsshsftpjsch

提问by Pa?lo Ebermann

I am new to SSH and JSch. When I connect from my client to the server I want to do two tasks:

我是 SSH 和 JSch 的新手。当我从客户端连接到服务器时,我想做两个任务:

  1. Upload a file (using ChannelSFTP)
  2. Perform commands, like creating a directory, and searching through a MySQL database
  1. 上传文件(使用ChannelSFTP
  2. 执行命令,例如创建目录和搜索 MySQL 数据库

At the moment I am using two separate shell logins to perform each task (actually I haven't started programming the MySQL queries yet).

目前我使用两个单独的 shell 登录来执行每个任务(实际上我还没有开始对 MySQL 查询进行编程)。

For the upload the relevant code is

对于上传相关代码是

session.connect();

Channel channel=session.openChannel("sftp");
channel.connect();
ChannelSftp c=(ChannelSftp)channel;
c.put(source, destination);

And for the command I have

对于我有的命令

String command = "ls -l";//just an example 
Channel channel=session.openChannel("exec");
((ChannelExec)channel).setCommand(command);

Should I disconnect the session after the first channel and then open the second channel? Or close the session entirely and open a new session? As I said, I'm new to this.

我应该在第一个频道之后断开会话,然后打开第二个频道吗?或者完全关闭会话并打开一个新会话?正如我所说,我是新手。

回答by Pa?lo Ebermann

One SSH session can support any number of channels - both in parallel and sequentially. (There is some theoretical limit in the channel identifier size, but you won't hit it in practice.) This is also valid for JSch. This saves redoing the costly key exchange operations.

一个 SSH 会话可以支持任意数量的通道 - 并行和顺序。(通道标识符大小有一些理论上的限制,但您不会在实践中达到它。)这对于 JSch 也是有效的。这节省了重做昂贵的密钥交换操作。

So, there is normally no need to close the session and reconnect before opening a new channel. The only reason I can think about would be when you need to login with different credentials for both actions.

因此,在打开新通道之前,通常不需要关闭会话并重新连接。我能想到的唯一原因是当您需要为这两个操作使用不同的凭据登录时。

To safe some memory, you might want to close the SFTP channel before opening the exec channel, though.

不过,为了保护一些内存,您可能需要在打开 exec 通道之前关闭 SFTP 通道。

回答by krishna

To give multiple commands through Jsch use shell instead of exec. Shell only support native commands of the connecting system. For example, when you are connecting windows system you can't give commands like dirusing the exec channel. So it is better to use shell.

要通过 Jsch 发出多个命令,请使用 shell 而不是 exec。Shell 仅支持连接系统的本机命令。例如,当您连接 Windows 系统时,您无法发出dir使用 exec 通道之类的命令。所以最好使用shell。

The following code can be used to send multiple commands through Jsch

下面的代码可以用来通过Jsch发送多个命令

Channel channel = session.openChannel("shell");
OutputStream ops = channel.getOutputStream();
PrintStream ps = new PrintStream(ops, true);

channel.connect();
ps.println("mkdir folder");
ps.println("dir");
//give commands to be executed inside println.and can have any no of commands sent.
ps.close();

InputStream in = channel.getInputStream();
byte[] bt = new byte[1024];

while (true) {
    while (in.available() > 0) {
        int i = in.read(bt, 0, 1024);
        if (i < 0) {
            break;
        }
        String str = new String(bt, 0, i);
        //displays the output of the command executed.
        System.out.print(str);

    }
    if (channel.isClosed()) {
            break;
    }
    Thread.sleep(1000);
    channel.disconnect();
    session.disconnect();
}