使用 ganymed 工具远程登录资源执行脚本命令

10 篇文章 0 订阅

使用 ganymed 工具远程登录资源执行脚本命令

依赖:ganymed-ssh2-262.jar

package com.zxl.ganymed;

import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.Session;
import ch.ethz.ssh2.StreamGobbler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

public final class GanymedDemo {
    private Logger log = LoggerFactory.getLogger(getClass());
    private Connection connection;
    private Session session;
    private BufferedReader stdout;
    private PrintWriter printWriter;
    private ExecutorService service = Executors.newFixedThreadPool(2);
    private static List<String> commands = null;
    private static void initCommands() {
        commands = new ArrayList<String>();
        commands.add("ll");
        commands.add("cd /home/liang");
        commands.add("pwd");
    }

    public void initSession(String hostName, String userName, String passwd, int port) throws IOException {
        connection = new Connection(hostName,port);
        connection.connect();
        boolean authenticateWithPassword = connection.authenticateWithPassword(userName, passwd);
        if (!authenticateWithPassword) {
            throw new RuntimeException("Authentication failed. Please check hostName, userName and passwd");
        }
        session = connection.openSession();
        session.requestDumbPTY();
        session.startShell();
        stdout = new BufferedReader(new InputStreamReader(new StreamGobbler(session.getStdout()), StandardCharsets.UTF_8));
        printWriter = new PrintWriter(session.getStdin());
        initCommands();
    }

    public void execCommand() throws IOException {
        service.submit(new Runnable() {
            @Override
            public void run() {
                String line;
                try {
                    boolean flag = false;
                    while ((line = stdout.readLine()) != null) {
                        if (line.contains(commands.get(0))){
                            flag = true;
                        }
                        if (flag){
                            System.out.println(line);
                        }
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
        for (String command : commands) {
            while (true) {
                printWriter.write(command + "\r\n");
                printWriter.flush();
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                break;
            }
        }
        System.out.println("============结束===============");
    }

    public void close() {
        try {
            connection.close();
            service.shutdown();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws IOException {
        GanymedDemo conn = new GanymedDemo();
        conn.initSession("127.0.0.1", "test", "test",2200);
        conn.execCommand();
        conn.close();
    }


}

采纳博主:A_Beaver

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值