如何使用 JSch 执行多项操作

发布于 2024-12-04 08:21:16 字数 619 浏览 0 评论 0原文

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

  1. 上传文件(使用 ChannelSFTP)
  2. 执行命令,例如创建目录和搜索 MySQL 数据库

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

对于上传,相关代码是

session.connect();

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

对于我的命令,

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

我应该在第一个通道之后断开会话,然后打开第二个通道吗?或者完全关闭会话并打开一个新会话?正如我所说,我对此很陌生。

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

  1. Upload a file (using ChannelSFTP)
  2. Perform commands, like creating a directory, and searching through a MySQL database

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

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.

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

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

发布评论

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

评论(2

心碎的声音 2024-12-11 08:21:16

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

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

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

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.

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.

柳絮泡泡 2024-12-11 08:21:16

通过 Jsch 发出多个命令
使用 shell 代替 exec。
Shell 仅支持连接系统的本机命令。
例如,当您连接 Windows 系统时,您无法使用 exec 通道发出诸如 dir 之类的命令。
所以还是用shell比较好。

以下代码可用于通过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();
}

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 dir using the exec channel.
So it is better to use shell.

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