调用远程shell脚本

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/xujiangdong1992/article/details/79900812

1.远程执行shell脚本类

package com.qingqing.bpt.utils.common;


import ch.ethz.ssh2.ChannelCondition;
import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.Session;
import ch.ethz.ssh2.StreamGobbler;
import org.apache.commons.io.IOUtils;

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;

/**
 * @author XuJD
 * @create 2018-04-11 16:48
 * 远程执行shell脚本类
 **/
public class RemoteShellExecutor {
    private Connection conn;
    /**
     * 远程机器IP
     */
    private String ip;
    /**
     * 远程机器端口
     */
    private int port;
    /**
     * 账号
     */
    private String userName;
    /**
     * 密码
     */
    private String password;

    private String charset = Charset.defaultCharset().toString();
    /**
     * 超时时间
     */
    private static final int TIME_OUT = 1000 * 5 * 60;

    /**
     * 构造函数
     * @param ip  远程主机IP地址
     * @param port  远程主机端口号
     * @param userName  账号
     * @param password  密码
     */
    public RemoteShellExecutor(String ip,int port,String userName, String password) {
        this.ip = ip;
        this.userName = userName;
        this.password = password;
        this.port = port;
    }

    /**
     * 登入
     * @return
     * @throws IOException
     */
    private boolean login() throws Exception{
        conn = new Connection(ip,port);
        conn.connect();
        return conn.authenticateWithPassword(userName, password);
    }

    /**
     * 执行脚本
     * @param cmds
     * @return
     */
    public String exec(String cmds) throws Exception{
        InputStream stdOut = null;
        InputStream stdErr = null;
        String outStr = "";
        String outErr = "";
        int ret = -1;
        try{
            if(login()){
                Session session = conn.openSession();
                session.execCommand(cmds);
                stdOut = new StreamGobbler(session.getStdout());
                outStr = processStream(stdOut, charset);
                stdErr = new StreamGobbler(session.getStderr());
                outErr = processStream(stdErr, charset);
                session.waitForCondition(ChannelCondition.EXIT_STATUS, TIME_OUT);
                ret = session.getExitStatus();
            }else{
                outStr = "远程主机登入失败--"+ip;
                throw new Exception("远程主机登入失败"+ip);
            }
        }finally {
            if(conn != null){
                conn.close();
            }
            IOUtils.closeQuietly(stdOut);
            IOUtils.closeQuietly(stdErr);
        }
        //返回执行结果
        return outStr;
    }

    private String processStream(InputStream in, String charset) throws Exception {
        byte[] buf = new byte[1024];
        StringBuilder sb = new StringBuilder();
        while (in.read(buf) != -1) {
            sb.append(new String(buf, charset));
        }
        return sb.toString();
    }

    public static void main(String args[]) throws Exception {
        RemoteShellExecutor exe = new RemoteShellExecutor('ip地址','端口','账号','密码');
        //需要传递的参数,以空格隔开 参数1 参数2 。。。
        String params="数学 语文";
        // 执行text.sh
        System.out.println(exe.exec("sh "+"/opt/xujd/text.sh "+params));
    }

}



2.shell脚本

#!/bin/bash
echo "Hello World XuJD!"
echo "第一个参数$1"  
echo "第二个参数$2" 

3.执行结果

Hello World XuJD!
第一个参数数学 
第二个参数语文


Process finished with exit code 0

猜你喜欢

转载自blog.csdn.net/xujiangdong1992/article/details/79900812