通过远程连接,docker访问获取数据表信息

<dependency>
    <groupId>com.jcraft</groupId>
    <artifactId>jsch</artifactId>
    <version>0.1.55</version>
</dependency>
	private static final String HOST = "xxx.xx.xx.xx";
    private static final String USERNAME = "xxx";
    private static final String PASSWORD = "xxxxxxxxxx";
    private static final String DOCKERCOMMAND =
            "sudo -S docker exec hrm_database /bin/bash -c \"" +
                    "echo '--- START: tbl_asset_port ---'; " +
                    "mysql -uhrm -pP@ssw0rd2022 hrm -e 'SELECT * FROM tbl_asset_port;'; " +
                    "echo '--- END: tbl_asset_port ---'; " +
                    "echo '--- START: tbl_asset_host ---'; " +
                    "mysql -uhrm -pP@ssw0rd2022 hrm -e 'SELECT * FROM tbl_asset_host;'; " +
                    "echo '--- END: tbl_asset_host ---'; " +
                    "echo '--- START: tbl_client ---'; " +
                    "mysql -uhrm -pP@ssw0rd2022 hrm -e 'SELECT * FROM tbl_client;'; " +
                    "echo '--- END: tbl_client ---'\"";
    private static final Integer PORT = xxx;

/**
 * 获取数据
 */
public static void getData(String host, String username, String password, String dockerCommand, Integer port, String filePath) {
        try {
            // 创建 JSch 实例并连接到 SSH
            JSch jsch = new JSch();
            Session session = jsch.getSession(username, host, port);
            session.setPassword(password);
            // 设置 SSH 的一些配置,避免首次连接时询问是否接受 host key
            session.setConfig("StrictHostKeyChecking", "no");
            session.connect();
            //执行
            ChannelExec sudoChannel = (ChannelExec) session.openChannel("exec");
            sudoChannel.setCommand("echo \"" + password + "\" | " + dockerCommand);
            sudoChannel.setInputStream(new ByteArrayInputStream(password.getBytes()));
            //放到文件里
            FileOutputStream fileOutputStream = new FileOutputStream(filePath);
            OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream);
            BufferedWriter bufferedWriter = new BufferedWriter(outputStreamWriter);
            sudoChannel.setOutputStream(fileOutputStream);
            //连接
            sudoChannel.connect();
            InputStream errorStream = sudoChannel.getErrStream();
            BufferedReader errorReader = new BufferedReader(new InputStreamReader(errorStream));
            String line;
            while ((line = errorReader.readLine()) != null) {
                System.err.println("Error: " + line);  // 打印错误信息
            }
            // 等待 sudo 命令执行完成
            // 超时机制:比如 30 秒超时
            int timeout = 5000; // 超过 30 秒就结束
            long startTime = System.currentTimeMillis();
            while (!sudoChannel.isClosed()) {
                // 如果超过超时限制,跳出循环
                if (System.currentTimeMillis() - startTime > timeout) {
                    break;
                }
                Thread.sleep(500);  // 每 0.5 秒检查一次
            }
            //关闭连接
            bufferedWriter.close();
            outputStreamWriter.close();
            sudoChannel.disconnect();
            session.disconnect();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
posted @ 2025-04-25 10:05  小侯学编程  阅读(14)  评论(0)    收藏  举报