Java SSH 长连接池:高效监控 Linux 主机
{/u0022title/u0022:/u0022Java SSH /u957f/u8fde/u63a5/u6c60/uff1a/u9ad8/u6548/u76d1/u6d4b Linux /u4e3b/u673a/u0022,/u0022description/u0022:/u0022/u4f7f/u7528 JSch /u5efa/u9020 SSH /u8fde/u63a5/u6c60/uff0c/u5b9e/u73b0 Linux /u4e3b/u673a/u76d1/u6d4b/u7684/u957f/u8fde/u63a5/uff0c/u63d0/u9ad8/u6548/u7387/u5e76/u4f4e/u964d/u8d44/u6e90/u6d88/u8017/u3002/u5305/u542b/u8fde/u63a5/u6c60/u7ba1/u7406/u3001/u8fde/u63a5/u5931/u6548/u5904/u7406/u3001/u544a/u8b66/u673a/u5236/u4ee5/u53ca/u4ee3/u7801/u793a/u4f8b/u3002/u0022,/u0022keywords/u0022:/u0022Java, SSH, /u957f/u8fde/u63a5, /u8fde/u63a5/u6c60, /u76d1/u6d4b, Linux, JSch, /u544a/u8b66, /u4ee3/u7801/u793a/u4f8b/u0022,/u0022content/u0022:/u0022/u8fd9/u662f/u4e00/u7bc7/u5173/u4e8e/u5982/u4f55/u4f7f/u7528 Java /u548c JSch /u5efa/u9020/u5e76/u7ba1/u7406 SSH /u957f/u8fde/u63a5/u6c60/u4e3a Linux /u4e3b/u673a/u76d1/u6d4b/u63d0/u4f9b/u652f/u6301/u7684/u6587/u7ae0/u3002/u8fd9/u79cd/u8bbe/u8ba1/u53ef/u4ee5/u63d0/u9ad8/u6548/u7387/u5e76/u4f4e/u964d/u8d44/u6e90/u6d88/u8017/uff0c/u56e0/u4e3a/u5b83/u51cf/u5c11/u4e86/u521d/u59cb/u5316/u8fde/u63a5/u7684/u65f6/u95f4/u548c/u8d44/u6e90/u3002/u5728/u8fd9/u7bc7/u6587/u7ae0/u4e2d/uff0c/u6211/u4eec/u4f1a/u8ba8/u8bba/u5982/u4f55/u5efa/u9020/u4e00/u4e2a/u8fde/u63a5/u6c60/uff0c/u5982/u4f55/u7ba1/u7406/u8fde/u63a5/uff0c/u5982/u4f55/u5904/u7406/u5931/u6548/u7684/u8fde/u63a5/uff0c/u5e76/u4e14/u600e/u4e48/u521b/u5efa/u544a/u8b66/u4ee5/u53ca/u5904/u7406/u544a/u8b66/u3002/n/n/u9996/u5148/uff0c/u6211/u4eec/u9700/u8981/u5f15/u5165 JSch /u4f9d/u8d56/u5305/uff0c/u53ef/u4ee5/u901a/u8fc7 Maven /u8fdb/u884c/u5f15/u5165/uff1a/n/nxml/n<dependency>/n <groupId>com.jcraft</groupId>/n <artifactId>jsch</artifactId>/n <version>0.1.55</version>/n</dependency>/n/n/n/u7136/u540e/uff0c/u521b/u5efa/u4e00/u4e2a SSH /u8fde/u63a5/u6c60/u7ba1/u7406/u5668/u7684/u7c7b/uff0c/u7528/u4e8e/u521d/u59cb/u5316/u5e76/u7ba1/u7406/u8fde/u63a5/u6c60/uff1a/n/njava/nimport com.jcraft.jsch.JSch;/nimport com.jcraft.jsch.JSchException;/nimport com.jcraft.jsch.Session;/nimport java.util.concurrent.BlockingQueue;/nimport java.util.concurrent.LinkedBlockingQueue;/n/npublic class SSHConnectionPoolManager {/n private static final int MAX_CONNECTIONS = 10; // /u6700/u5927/u8fde/u63a5/u6570/n private static final int MAX_RETRIES = 3; // /u6700/u5927/u91cd/u8bd5/u6b21/u6570/n private static final int RETRY_DELAY = 5000; // /u91cd/u8bd5/u5ef6/u8fdf/u65f6/u95f4/uff08/u6beb/u79d2/uff09/n /n private final String host;/n private final int port;/n private final String username;/n private final String password;/n private final BlockingQueue<Session> connectionPool;/n /n public SSHConnectionPoolManager(String host, int port, String username, String password) {/n this.host = host;/n this.port = port;/n this.username = username;/n this.password = password;/n this.connectionPool = new LinkedBlockingQueue<>(MAX_CONNECTIONS);/n initializePool();/n }/n /n private void initializePool() {/n for (int i = 0; i < MAX_CONNECTIONS; i++) {/n Session session = createSession();/n if (session != null) {/n connectionPool.offer(session);/n }/n }/n }/n /n private Session createSession() {/n try {/n JSch jsch = new JSch();/n Session session = jsch.getSession(username, host, port);/n session.setPassword(password);/n session.setConfig(/'StrictHostKeyChecking/', /'no/');/n session.connect();/n return session;/n } catch (JSchException e) {/n e.printStackTrace();/n }/n return null;/n }/n /n public Session getConnection() {/n Session session = connectionPool.poll();/n if (session != null && !session.isConnected()) {/n session = null;/n }/n if (session == null) {/n session = createSession();/n }/n return session;/n }/n /n public void releaseConnection(Session session) {/n if (session != null && session.isConnected()) {/n connectionPool.offer(session);/n }/n }/n}/n/n/n/u7136/u540e/uff0c/u521b/u5efa/u4e00/u4e2a/u76d1/u6d4b/u7c7b/uff0c/u7528/u4e8e/u91c7/u96c6/u4fe1/u606f/u5e76/u5904/u7406/u8fde/u63a5/u5931/u6548/u548c/u544a/u8b66/u6062/u590d/u7684/u60c5/u51b5/uff1a/n/njava/nimport com.jcraft.jsch.ChannelExec;/nimport com.jcraft.jsch.JSchException;/nimport com.jcraft.jsch.Session;/nimport java.io.BufferedReader;/nimport java.io.IOException;/nimport java.io.InputStreamReader;/n/npublic class Monitor {/n private static final int RETRY_DELAY = 5000; // /u91cd/u8bd5/u5ef6/u8fdf/u65f6/u95f4/uff08/u6beb/u79d2/uff09/n private static final int MAX_RETRIES = 3; // /u6700/u5927/u91cd/u8bd5/u6b21/u6570/n /n private final SSHConnectionPoolManager connectionPoolManager;/n /n public Monitor(String host, int port, String username, String password) {/n this.connectionPoolManager = new SSHConnectionPoolManager(host, port, username, password);/n }/n /n public void collectInformation() {/n Session session = connectionPoolManager.getConnection();/n if (session == null) {/n System.out.println(/'Failed to get a connection from the pool./');/n return;/n }/n /n try {/n ChannelExec channel = (ChannelExec) session.openChannel(/'exec/');/n channel.setCommand(/'your_command_here/');/n channel.connect();/n /n BufferedReader reader = new BufferedReader(new InputStreamReader(channel.getInputStream()));/n String line;/n while ((line = reader.readLine()) != null) {/n // /u5904/u7406/u91c7/u96c6/u5230/u7684/u4fe1/u606f/n }/n /n channel.disconnect();/n connectionPoolManager.releaseConnection(session);/n } catch (IOException | JSchException e) {/n handleConnectionFailure(session);/n }/n }/n /n private void handleConnectionFailure(Session session) {/n if (session != null) {/n session.disconnect();/n }/n connectionPoolManager.releaseConnection(session);/n /n System.out.println(/'Failed to connect to the host. Generating an alert.../');/n // /u751f/u6210/u544a/u8b66/n /n int retries = 0;/n while (retries < MAX_RETRIES) {/n try {/n Thread.sleep(RETRY_DELAY);/n session = connectionPoolManager.getConnection();/n if (session != null) {/n System.out.println(/'Connection recovered. Resuming monitoring.../');/n return;/n }/n retries++;/n } catch (InterruptedException e) {/n e.printStackTrace();/n }/n }/n /n System.out.println(/'Failed to recover the connection. Alert remains active./');/n }/n}/n/n/n/u4f7f/u7528/u793a/u4f8b/uff1a/n/njava/npublic class Main {/n public static void main(String[] args) {/n String host = /'your_host/';/n int port = 22;/n String username = /'your_username/';/n String password = /'your_password/';/n /n Monitor monitor = new Monitor(host, port, username, password);/n monitor.collectInformation();/n }/n}/n/n/n/u4e0a/u8ff0/u4ee3/u7801/u4e2d/uff0cSSHConnectionPoolManager/u7c7b/u7528/u4e8e/u521d/u59cb/u5316/u5e76/u7ba1/u7406/u8fde/u63a5/u6c60/uff0cgetConnection/u65b9/u6cd5/u7528/u4e8e/u4ece/u8fde/u63a5/u6c60/u83b7/u53d6/u8fde/u63a5/uff0creleaseConnection/u65b9/u6cd5/u7528/u4e8e/u9664/u6389/u8fde/u63a5/u3002/n/nMonitor/u7c7b/u7528/u4e8e/u91c7/u96c6/u4fe1/u606f/uff0ccollectInformation/u65b9/u6cd5/u4e2d/u5148/u4ece/u8fde/u63a5/u6c60/u83b7/u53d6/u8fde/u63a5/uff0c/u5982/u679c/u83b7/u53d6/u5931/u8d25/u5219/u6253/u5370/u9519/u8bef/u4fe1/u606f/u3002/u5982/u679c/u8fde/u63a5/u6210/u529f/uff0c/u5219/u6267/u884c/u6307/u5b9a/u7684/u547d/u4ee4/u8fdb/u884c/u4fe1/u606f/u91c7/u96c6/u3002/u5982/u679c/u8fde/u63a5/u5931/u8d25/uff0c/u5219/u8c03/u7528handleConnectionFailure/u65b9/u6cd5/u5904/u7406/u8fde/u63a5/u5931/u6548/uff0c/u5305/u62ec/u65ad/u5f00/u8fde/u63a5/u3001/u9664/u6389/u8fde/u63a5/u3001/u751f/u6210/u544a/u8b66/u548c/u5c1d/u8bd5/u6062/u590d/u8fde/u63a5/u3002/n/n/u8bf7/u6ce8/u610f/uff0c/u4e0a/u8ff0/u4ee3/u7801/u53ea/u662f/u793a/u4f8b/uff0c/u5177/u4f53/u7684/u5b9e/u73b0/u65b9/u5f0f/u53ef/u80fd/u56e0/u9879/u76ee/u9700/u6c42/u800c/u6709/u6709/u5dee/u522b/u3002/u540c/u65f6/uff0c/u9700/u8981/u6839/u636e/u5b9e/u9645/u60c5/u51b5/u8fdb/u884c/u5f02/u5e38/u5904/u7406/u3001/u65e5/u5fd7/u8bb0/u5f55/u7b49/u64cd/u4f5c/u3002/u002
原文地址: https://www.cveoy.top/t/topic/pSEP 著作权归作者所有。请勿转载和采集!