java远程执行服务器命令

1.第一种

pom文件

<dependency>
            <groupId>ch.ethz.ganymed</groupId>
            <artifactId>ganymed-ssh2</artifactId>
            <version>build210</version>
</dependency>

java

脚本名为advertising_generate.sh,替换ip,port,username和password

public static String processStdout(InputStream in, String charset) {
        InputStream stdout = new StreamGobbler(in);
        StringBuffer buffer = new StringBuffer();
        ;
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(stdout, charset));
            String line = null;
            while ((line = br.readLine()) != null) {
                buffer.append(line + "\n");
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return buffer.toString();


    }
public static void main(String[] args) throws IOException, JSchException {
        Connection connection = null;
        try {
            connection = new Connection(ip, port);
            //连接
            connection.connect();
            //身份验证
            boolean isAuthenticated = connection.authenticateWithPassword(username, password);
            if (isAuthenticated) {
                System.out.println("连接成功");

                Session session = null;

                try {
                    session = connection.openSession();
                    //把命令写入脚本,在执行该脚本
                    //session.execCommand("sh /root/aaa.sh" + "a b");

                    session.execCommand("sh /home/nginx/advertising_generate.sh "+"202312281519 https://jm-live-video-dev.oss-cn-shanghai.aliyuncs.com/goods/101164/c14619cb-4fd8-4d45-8f97-428f042bf81344.jpg");

                    String result = processStdout(session.getStdout(), DEFAULTCHART);

                    //如果为得到标准输出为空,说明脚本执行出错了
                    if (result == null) {
                        result = processStdout(session.getStderr(), DEFAULTCHART);
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    session.close();
                    connection.close();
                }

            } else {
                throw new Exception("身份验证失败");
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
}

第二种:

pom文件

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

java

public static String exeCommand(String host, int port, String user, String password, String command) throws JSchException, IOException {

        JSch jsch = new JSch();
        Session session = jsch.getSession(user, host, port);
        session.setConfig("StrictHostKeyChecking", "no");
        //    java.util.Properties config = new java.util.Properties();
        //   config.put("StrictHostKeyChecking", "no");

        session.setPassword(password);
        session.connect();

        ChannelExec channelExec = (ChannelExec) session.openChannel("exec");
        InputStream in = channelExec.getInputStream();
        channelExec.setCommand(command);
        channelExec.setErrStream(System.err);

        channelExec.connect();
        String out = IOUtils.toString(in, "UTF-8");

        channelExec.disconnect();
        session.disconnect();

        return out;
    }
}

main方法

public static void main(String[] args) throws IOException, JSchException {
        String host = ip;
        int port = port;
        String user = user ;
        String password = password ;
        String command = "sh /home/nginx/advertising_generate.sh \"+\"202312281519 https://zpdt-dev.oss-cn-hangzhou.aliyuncs.com/goods/1/99ccbc1e-f20e-4290-a774-e03274e651ad.jpg";
        String res = exeCommand(host,port,user,password,command);

        System.out.println(res);
    }

第三种

pom

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

java

public static void main(String[] args) throws IOException, JSchException {

        Session session = JschUtil.openSession( ip, port, username,  password);
        String cmd="sh /home/nginx/advertising_generate.sh 202312281519"+"https://zpdt-dev.oss-cn-hangzhou.aliyuncs.com/goods/1/99ccbc1e-f20e-4290-a774-e03274e651ad.jpg";
        String result = JschUtil.exec(session,cmd, CharsetUtil.CHARSET_UTF_8);

        System.out.println(result);

    }

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值