利用jsch 远程访问linux执行内部脚本

该文章展示了如何在Java项目中通过Maven添加jsch库作为依赖,用于实现远程SSH连接和执行命令。核心代码创建了一个Session,配置了认证信息,并通过ChannelExec执行shell脚本。如果权限不足,可以使用sudo命令。
摘要由CSDN通过智能技术生成

安装maven依赖

<!-- jsch的方式 远程连接的包-->
<dependency>
   <groupId>com.jcraft</groupId>
   <artifactId>jsch</artifactId>
   <version>0.1.55</version>
</dependency>

 核心代码展示

 

 /**
     *  执行相关的命令(输入式)
     * @param shellScript  执行脚本命令
     * @param sshHost 远程连接地址
     * @param sshPort 端口号
     * @param sshUser 账号
     * @param sshPass 密码
     * @return
     */
    public static boolean shShell(String shellScript, String sshHost, Integer sshPort, String sshUser, String sshPass) {
        boolean flag = true;
        Session jschSession = null;
        System.out.println(sshHost);
        System.out.println(sshPort);
        System.out.println(sshUser);
        System.out.println(sshPass);
        try {
            InputStream in = null;
            JSch jsch = new JSch();
            jschSession = jsch.getSession(sshUser,
                    sshHost,sshPort);
            //密码认证
            jschSession.setPassword(sshPass);
            //SSH授权客户端位置,测试环境设置免授权,生产环境可对应服务器授权
            //jsch.setKnownHosts(SSHConfig.SSH_PATH);
            Properties config = new Properties();
            config.put("StrictHostKeyChecking","no");
            jschSession.setConfig(config);

//            jschSession = JschUtil.getSession(sshHost, sshPort, sshUser, sshPass);
            //SSH授权客户端位置,测试环境设置免授权,生产环境可对应服务器授权
//            jsch.setKnownHosts(SSHConfig.SSH_PATH);
//            Properties config = new Properties();
//            config.put("StrictHostKeyChecking","no");
//            jschSession.setConfig(config);
            //创建连接
            jschSession.connect(5000);
            //建立可执行管道
            ChannelExec channelExec = (ChannelExec) jschSession.openChannel("exec");
            //执行脚本
            channelExec.setCommand(shellScript);
            //获取执行脚本可能出现的错误日志
            channelExec.setErrStream(System.err);
            //执行脚本输出结果
            in = channelExec.getInputStream();
            //设置超时时间
            channelExec.connect(3000);

            byte[] tmp = new byte[1024];
            while (true) {
                while (in.available() > 0) {
                    int i = in.read(tmp, 0, 1024);
                    if (i < 0) break;
                    System.out.println("脚本执行过程:" + new String(tmp, 0, i));
                }

                //获取执行结果
                if (channelExec.isClosed()) {

                    if (in.available() > 0) continue;
                    if (channelExec.getExitStatus() != 0) {
                        //脚本非正常结束,退出吗非零
                        flag = false;
                        System.out.println("脚本执行出错");
                    }
                    break;
                }
                try {
                    Thread.sleep(1000);
                } catch (Exception e) {

                }
            }
            channelExec.disconnect();
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("catch");
            System.out.println("脚本执行出错");
            flag = false;
        } finally {
            if (jschSession != null) jschSession.disconnect();
        }
        return flag;
    }

注意 

如果权限不够(不是root用户)

则通过 sudo 命令的方式 执行

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值