栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

java 通过ssh 连接执行shell命令,文件传输

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java 通过ssh 连接执行shell命令,文件传输

JSch 是SSH2的纯 Java 实现 。
JSch 允许您连接到 sshd 服务器并使用端口转发、X11 转发、文件传输等,可以将其功能集成到您自己的 Java 程序中。JSch 是在BSD 风格许可下获得许可的。

JSCH 官网:http://www.jcraft.com/jsch/

zip文件中有很多demo.
jar为依赖jar

maven 依赖

        
            com.jcraft
            jsch
            0.1.55
        

测试代码

import com.jcraft.jsch.*;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import java.io.*;
import java.util.Properties;

@Slf4j
public class SshTest {

    private String charset = "UTF-8"; // 设置编码格式
    private static String userName = "kevin"; // 用户名
    private static String passWord= "1"; // 登录密码
    private static String host = "192.168.10.101"; // 主机IP
    private static int port = 22; //默认端口
    private static JSch jsch;
    private static Session session;

    private static ChannelSftp channelSftp;

    @BeforeAll
    public static void createConn() throws JSchException {
        jsch = new JSch();
//        jsch = new KeyGen().keyGenTest();
        // 密钥方式
        jsch.setKnownHosts("C:\Users\Admin\.ssh\known_hosts");
//        jsch.addIdentity("~/.ssh/id_rsa", "~/.ssh/id_rsa.pub", null);
//        jsch.addIdentity("C:\Users\Admin\.ssh\id_rsa", "C:\Users\Admin\.ssh\id_rsa.pub", null);
        String pubKeyPath = "C:\Users\Admin\.ssh\id_rsa";
        jsch.addIdentity(pubKeyPath);

        session = jsch.getSession(userName, host, port);
        // 密码方式
//        session.setPassword(passWord);
        Properties config = new Properties();
        
        config.put("StrictHostKeyChecking", "no");
        session.setConfig(config); // 为Session对象设置properties
        int timeout = 30000;
        session.setTimeout(timeout); // 设置timeout时间
        session.connect(); // 通过Session建立与远程服务器的连接回话
        log.info("connect server host: " + host);
    }

    
    @AfterAll
    public static void disconnect(){
        log.info("disconnect...");
        if (channelSftp != null && channelSftp.isConnected()) {
            channelSftp.disconnect();
        }
        if(session != null && session.isConnected()){
            session.disconnect();
        }
    }

    @Test
    public void downloadFile() throws JSchException, FileNotFoundException, SftpException {
        channelSftp = (ChannelSftp) session.openChannel("sftp");
        channelSftp.connect();
        log.info("start download channel file!");
        String directory = "/tmp";
        channelSftp.cd(directory);
        String saveDir = "D:\desktop\"+System.currentTimeMillis()+".txt";
        File file = new File(saveDir);
        String downloadFile = "Test.java";
        channelSftp.get(downloadFile, new FileOutputStream(file));
        log.info("Download Success!");
        channelSftp.disconnect();
        log.info("end execute channel sftp!");
    }

    @Test
    public void uploadFile() throws JSchException, SftpException, FileNotFoundException {
        channelSftp = (ChannelSftp) session.openChannel("sftp");
        channelSftp.connect();
        log.info("start upload channel file!");
        String directory = "/tmp";
        channelSftp.cd(directory);
        File file = new File("D:\desktop\Test.java");
        channelSftp.put(new FileInputStream(file), file.getName().replace(".", System.currentTimeMillis()+"."));
        log.info("Upload Success!");
        channelSftp.disconnect();
        log.info("end execute channel sftp!");
    }

    @Test
    public void execShell() throws JSchException, IOException {
        ChannelExec channelExec = (ChannelExec) session.openChannel("exec");
        String cmd = "pwd";
        channelExec.setCommand(cmd);         //添加传入进来的shell命令
        channelExec.setErrStream(System.err);//通道连接错误信息提示
        channelExec.connect();
        log.info("start execute channel command!");

        try(BufferedReader in = new BufferedReader(new InputStreamReader(channelExec.getInputStream()))) {
            String msg;
            log.info("start read!");
            while ((msg = in.readLine()) != null) {
                log.info("命令返回信息:{}", msg);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        channelExec.disconnect();
        log.info("end execute channel command!");
    }
}

问题1
com.jcraft.jsch.JSchException: invalid privatekey: [B@17f7cd29

查看id_rsa文件,内容如下

-----BEGIN OPENSSH PRIVATE KEY-----
b3BlbnNzaC...
...
...
...MAECAwQF
-----END OPENSSH PRIVATE KEY-----

解决方案:
Jsch好像不支持上面的私钥格式,要解决这个问题,我们可以使用ssh-keygen将私钥格式转换为RSAorpem模式,再次运行上面的程序。

$ ssh-keygen -p -f ~/.ssh/id_rsa -m pem

重新检查私钥内容,它应该以BEGIN RSA.

-----BEGIN RSA PRIVATE KEY-----
MIIG4wIBAAK...
...
...
...E428GBDI4
-----END RSA PRIVATE KEY-----

再次尝试连接正常。

Admin@DESKTOP-91JEC09 MINGW64 ~
$ ssh kevin@hadoop101
Last login: Wed Oct 20 14:28:41 2021 from 192.168.10.1
[kevin@hadoop101 ~]$ client_loop: send disconnect: Connection reset by peer

Admin@DESKTOP-91JEC09 MINGW64 ~
$ ssh-keygen -p -f C:\Users\Admin\.ssh\id_rsa -m pem
Key has comment 'Admin@DESKTOP-91JEC09'
Enter new passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved with the new passphrase.

Admin@DESKTOP-91JEC09 MINGW64 ~
$ ssh kevin@hadoop101
Last login: Wed Oct 20 15:43:57 2021 from 192.168.10.1

参考文档:
https://mkyong.com/java/jsch-invalid-privatekey-exception/
http://www.jcraft.com/jsch/

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/345612.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号