jsch的简单使用

Could not parse response code.Server Reply: SSH-2.0-OpenSSH_5.3
服务器不支持协议,使用JSch

        <dependency>
            <groupId>com.jcraft</groupId>
            <artifactId>jsch</artifactId>
            <version>0.1.54</version>
        </dependency>
package com.xxl.job.executor;

import com.jcraft.jsch.*;

import java.io.IOException;
import java.util.Properties;
import java.util.Vector;

/**
 * Created by nbcoolkid on 2017-12-27.
 */
public class Main {
    public static void main(String[] args) throws IOException {
//        FTPClient ftpClient = new FTPClient();
//        ftpClient.connect("vm.docker",22);
//        boolean b = ftpClient.login("docker","docker");
//        System.out.println("连接成功:"+b);
        ChannelSftp sftp = null;
        Channel channel = null;
        Session sshSession = null;

        String username = "docker";
        String host = "vm.docker";
        String password = "docker";
        int port = 22;

        try {
            JSch jsch = new JSch();
            jsch.getSession(username, host, port);
            sshSession = jsch.getSession(username, host, port);
            sshSession.setPassword(password);
            Properties sshConfig = new Properties();
            sshConfig.put("StrictHostKeyChecking", "no");
            sshSession.setConfig(sshConfig);
            sshSession.connect();
            System.out.println("连接成功");
            channel = sshSession.openChannel("sftp");
            channel.connect();
            sftp = (ChannelSftp) channel;
            Vector vector = sftp.ls("/home/docker/opt/pcre-8.40");
            for (Object item:vector) {
                ChannelSftp.LsEntry entry = (ChannelSftp.LsEntry) item;
                System.out.println(entry.getFilename());
            }

        } catch (JSchException e) {
            e.printStackTrace();
        } catch (SftpException e) {
            e.printStackTrace();
        }finally {
            closeChannel(sftp);
            closeChannel(channel);
            closeSession(sshSession);
        }

    }

    private static void closeChannel(Channel channel) {
        if (channel != null) {
            if (channel.isConnected()) {
                channel.disconnect();
            }
        }
    }

    private static void closeSession(Session session) {
        if (session != null) {
            if (session.isConnected()) {
                session.disconnect();
            }
        }
    }
}

  • 0
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用 JSch 库来实现在 Java 中使用 SSH 协议发送 jar 文件。以下是一个简单的示例代码: ```java import com.jcraft.jsch.*; import java.io.*; public class SendJarUsingJSch { public static void main(String[] args) throws JSchException, IOException { String host = "hostname"; // 服务器地址 String username = "username"; // 登录用户名 String password = "password"; // 登录密码 int port = 22; // SSH 端口号 String jarFilePath = "path/to/jar/file.jar"; // 待发送的 jar 文件路径 String remoteDirectory = "path/to/remote/directory"; // 远程服务器上的目录 JSch jSch = new JSch(); Session session = jSch.getSession(username, host, port); session.setPassword(password); session.setConfig("StrictHostKeyChecking", "no"); session.connect(); ChannelSftp channelSftp = (ChannelSftp) session.openChannel("sftp"); channelSftp.connect(); // 上传 jar 文件 File jarFile = new File(jarFilePath); String fileName = jarFile.getName(); long fileSize = jarFile.length(); InputStream inputStream = new FileInputStream(jarFile); channelSftp.put(inputStream, remoteDirectory + "/" + fileName); // 关闭连接 channelSftp.disconnect(); session.disconnect(); } } ``` 以上代码中,首先创建一个 JSch 对象并创建一个 SSH 会话。然后使用 Session#openChannel 方法打开 SFTP 通道,并连接到远程服务器。接着,使用 ChannelSftp#put 方法将文件上传到远程服务器上指定的目录。最后,关闭连接。 需要注意的是,JSch 库需要依赖 JZlib 库,需要将其添加到项目中。另外,如果服务器端使用的是非标准的 SSH 端口号,需要将 port 参数设置为正确的值。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值