java执行远程机器上的脚本

java执行远程机器上的脚本

 

package com.bbpay.publish.util;

import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

/**
 * Created by Administrator on 2015/9/21 0021.
 */
public class ShellExecUtil {

    private static Log log = LogFactory.getLog(ShellExecUtil.class);

    /**
     * 创建会话连接
     *
     * @param ip         主机IP
     * @param user       主机登陆用户名
     * @param pwd        主机登陆密码
     * @param port       主机ssh2登陆端口,如果取默认值,传-1
     * @param privateKey
     * @param passphrase
     */
    public static Session sshConnect(String ip, String user, String pwd, int port,
                                     String privateKey, String passphrase) throws Exception {


        //验证主机ip
        if (null == ip || "".equals(ip)) {
            log.error("主机IP为空");
            throw new RuntimeException("主机IP为空");
        }

        //验证主机登陆用户名
        if (null == user || "".equals(user)) {
            log.error("主机登陆用户名为空");
            throw new RuntimeException("主机登陆用户名为空");
        }

        //验证主机登陆密码
        if (null == pwd || "".equals(pwd)) {
            log.error("主机登陆密码为空");
            throw new RuntimeException("主机登陆密码为空");
        }


        JSch jsch = new JSch();
        Session session = null;

        //设置密钥和密码
        if (privateKey != null && !"".equals(privateKey)) {
            if (passphrase != null && "".equals(passphrase)) {
                //设置带口令的密钥
                jsch.addIdentity(privateKey, passphrase);
            } else {
                //设置不带口令的密钥
                jsch.addIdentity(privateKey);
            }
        }

        if (port <= 0) {
            //连接服务器,采用默认端口
            session = jsch.getSession(user, ip);
        } else {
            //采用指定的端口连接服务器
            session = jsch.getSession(user, ip, port);
        }

        //如果服务器连接不上,则抛出异常
        if (null == session) {
            log.error("ssh session is null");
            throw new Exception("ssh session is null");
        }

        //设置登陆主机的密码
        session.setPassword(pwd);//设置密码
        //设置第一次登陆的时候提示,可选值:(ask | yes | no)
        session.setConfig("StrictHostKeyChecking", "no");
        //设置登陆超时时间
        session.connect(10000);
        session.sendKeepAliveMsg();
        session.setServerAliveCountMax(10000);


        //返回会话
        return session;

    }


    /**
     * 关闭会话连接
     *
     * @param session
     */
    public static void sshDisconnect(Session session) throws Exception {

        if (null == session) {
            log.error("ssh session is null,关闭session异常");
            throw new Exception("ssh session is null,关闭session异常");
        }
        session.disconnect();

    }


    /**
     * 执行ssh远程命令
     *
     * @param ip
     * @param user
     * @param pwd
     * @param port
     * @param privateKey
     * @param passphrase
     * @param command    执行命令
     * @param isGetMes   是否返回命令执行的结果
     */
    public static String sshExecCmd(String ip, String user, String pwd, int port,
                                    String privateKey, String passphrase, String command, boolean isGetMes) throws Exception {

        //获取ssh连接会话
        Session session = sshConnect(ip, user, pwd, port,
                privateKey, passphrase);

        if (null == session) {
            log.error("创建ssh连接失败");
            throw new RuntimeException("创建ssh连接失败");
        }


        ChannelExec openChannel = null;

        openChannel = (ChannelExec) session.openChannel("exec");
        System.out.println(command);
        openChannel.setCommand(command);
//        int exitStatus = openChannel.getExitStatus();
//        System.out.println(exitStatus);
        openChannel.connect(10000);

        InputStream in = openChannel.getInputStream();
        BufferedReader reader = new BufferedReader(new InputStreamReader(in));
        String buf = null;

        String result = "";
        while ((buf = reader.readLine()) != null) {
            result += buf.toString() + "\n";//new String(buf.getBytes("gbk"),"UTF-8") + "\n";
        }
        log.info(result);
        //System.out.println(result);

        //断开连接
        openChannel.disconnect();
        sshDisconnect(session);

        if (isGetMes) {
            return result;
        } else {
            return "";
        }

    }


    public static void main(String[] args) {

        try {
            String execCmdResult = sshExecCmd("192.168.15.61", "bebepay", "bebepay", 4022, null, null, "/home/bebepay/bebepayplatform/send_dxw_lol.sh && /home/bebepay/bebepayplatform/rupdate_dxw_lol.sh", true);
            System.out.println(execCmdResult);

//            String execCmdResult = sshExecCmd("192.168.15.10", "root", "111111", 22, null, null, "/home/bebepay/bebepayplatform/publish_dev_lol.sh", true);
//            System.out.println(execCmdResult);
        } catch (Exception e) {
            System.out.println(e);
            e.printStackTrace();
        }

    }


}

 

 

 

转载于:https://my.oschina.net/lenglingx/blog/808128

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值