Java远程连接Linux服务器执行shell脚本

导读:本篇文章讲解 Java远程连接Linux服务器执行shell脚本,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

Java远程连接Linux服务器执行shell脚本

1.准备工作

1.搭建maven工程

2.引入jsch依赖

<dependency>
   <groupId>com.jcraft</groupId>
   <artifactId>jsch</artifactId>
   <version>0.1.55</version>
</dependency>

3.在linux服务器或虚拟机中编写shell脚本

#!/bin/bash 
echo "hello world~"

2.执行sh脚本代码

示例代码如下,如果是boot项目可以将配置信息编写在配置文件中,从配置文件中获取服务器连接信息已经需要执行的脚本文件

/**
 * @param host    主机ip
 * @param user    用户名
 * @param pass    用户密码
 * @param port    端口
 * @param command 要执行的指令
 */
public static void sshCommand(String host, String user, String pass, int port, String command) {
    JSch jsch = new JSch();
    Session session = null;
    Channel channel = null;
    InputStream in = null;
    try {
        session = jsch.getSession(user, host, port);
        session.setPassword(pass);
        session.setTimeout(5000);
        Properties config = new Properties();
        //严格的主机key检查
        config.put("StrictHostKeyChecking", "no");
        session.setConfig(config);
        session.connect();
        channel = session.openChannel("exec");
        ChannelExec execChannel = (ChannelExec) channel;
        execChannel.setCommand(command);
        in = channel.getInputStream();
        channel.connect();

        StringBuffer sb = new StringBuffer();
        int c = -1;
        while ((c = in.read()) != -1) {
            sb.append((char) c);
        }
        System.out.println("输出结果是:" + sb.toString());
    } catch (JSchException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (channel != null) {
            channel.disconnect();
        }
        if (session != null) {
            session.disconnect();
        }
    }
}

3.上传文件到linux服务器

/**
 * sftp文件上传
 *
 * @param host       主机
 * @param username   用户名
 * @param password   用户密码
 * @param port       端口
 * @param location   上传文件路径
 * @param uploadFile 要上传的文件
 * @return
 */
public static boolean upload(String host, String username, String password, int port, String location, File uploadFile) {
    Session session = null;
    Channel channel = null;
    ChannelSftp sftp = null;
    JSch jsch = new JSch();
    try {
        session = jsch.getSession(username, host, port);
        session.setPassword(password);

        Properties config = new Properties();
        config.put("StrictHostKeyChecking", "no");
        session.setConfig(config);

        //建立连接
        session.connect();
        channel = session.openChannel("sftp");
        channel.connect();
        sftp = (ChannelSftp) channel;
        sftp.cd(location);
        sftp.put(new FileInputStream(uploadFile), uploadFile.getName());
        return true;
    } catch (JSchException e) {
        e.printStackTrace();
    } catch (SftpException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (channel != null) {
            channel.disconnect();
        }
        if (sftp != null) {
            sftp.disconnect();
        }
        if (session != null) {
            session.disconnect();
        }
    }
    return false;
}

4.测试案例

image-20221010104852336

如果上传相同文件名的文件,文件会被覆盖

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/81187.html

(0)
小半的头像小半

相关推荐

极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!