@Test public void testRemoteInvoke() { HostSystem hostSystem = new HostSystem(); hostSystem.setInstanceId("1"); hostSystem.setHost("10.17.1.120"); hostSystem.setPort(22); hostSystem.setUser("manage"); for(int i = 0; i < 10; i++) { ConnectionSession connectionSession = RemoteInvokeHandler.getSession(keyboxDao.getApplicationKey(), hostSystem); if (connectionSession != null) { String uniqueKey = "aaa-" + i; connectionSession.registerRead(uniqueKey, new SessionBridge() { @Override public void process(String value) { HttpResult result = new HttpResult(value); System.err.println(JSON.toJSONString(result)); } }); connectionSession.unregisterRead(uniqueKey); connectionSession.close(); } } }
public static ConnectionSession getSession(ApplicationKeyDO applicationKeyDO, HostSystem hostSystem) { JSch jSch = new JSch(); String passphrase = applicationKeyDO.getPassphrase(); if (StringUtils.isEmpty(passphrase)) { passphrase = ""; } try { jSch.addIdentity(applicationKeyDO.getSessionId() + "", EncryptionUtil.decrypt(applicationKeyDO.getPrivateKey()).getBytes(), applicationKeyDO.getPublicKey().getBytes(), passphrase.getBytes()); Session session = jSch.getSession(hostSystem.getUser(), hostSystem.getHost(), hostSystem.getPort() == null ? 22 : hostSystem.getPort()); session.setConfig("StrictHostKeyChecking", "no"); session.setConfig("PreferredAuthentications", "publickey,keyboard-interactive,password"); session.setServerAliveInterval(SERVER_ALIVE_INTERVAL); session.connect(SESSION_TIMEOUT); Channel channel = session.openChannel("shell"); ((ChannelShell) channel).setAgentForwarding(true); ((ChannelShell) channel).setPtyType("xterm"); channel.connect(); ConnectionSession connectionSession = new ConnectionSession(channel, session); return connectionSession; } catch (Exception e) { logger.error(e.getMessage(), e); hostSystem.setErrorMsg(e.getMessage()); if (e.getMessage().toLowerCase().contains("userauth fail")) { hostSystem.setStatusCd(HostSystem.PUBLIC_KEY_FAIL_STATUS); } else if (e.getMessage().toLowerCase().contains("auth fail") || e.getMessage().toLowerCase().contains("auth cancel")) { hostSystem.setStatusCd(HostSystem.AUTH_FAIL_STATUS); } else if (e.getMessage().toLowerCase().contains("unknownhostexception")) { hostSystem.setErrorMsg("DNS Lookup Failed"); hostSystem.setStatusCd(HostSystem.HOST_FAIL_STATUS); } else { hostSystem.setStatusCd(HostSystem.GENERIC_FAIL_STATUS); } } return null; }
RemoteInvokeHandler { public static ConnectionSession getSession(ApplicationKeyDO applicationKeyDO, HostSystem hostSystem) { JSch jSch = new JSch(); String passphrase = applicationKeyDO.getPassphrase(); if (StringUtils.isEmpty(passphrase)) { passphrase = ""; } try { jSch.addIdentity(applicationKeyDO.getSessionId() + "", EncryptionUtil.decrypt(applicationKeyDO.getPrivateKey()).getBytes(), applicationKeyDO.getPublicKey().getBytes(), passphrase.getBytes()); Session session = jSch.getSession(hostSystem.getUser(), hostSystem.getHost(), hostSystem.getPort() == null ? 22 : hostSystem.getPort()); session.setConfig("StrictHostKeyChecking", "no"); session.setConfig("PreferredAuthentications", "publickey,keyboard-interactive,password"); session.setServerAliveInterval(SERVER_ALIVE_INTERVAL); session.connect(SESSION_TIMEOUT); Channel channel = session.openChannel("shell"); ((ChannelShell) channel).setAgentForwarding(true); ((ChannelShell) channel).setPtyType("xterm"); channel.connect(); ConnectionSession connectionSession = new ConnectionSession(channel, session); return connectionSession; } catch (Exception e) { logger.error(e.getMessage(), e); hostSystem.setErrorMsg(e.getMessage()); if (e.getMessage().toLowerCase().contains("userauth fail")) { hostSystem.setStatusCd(HostSystem.PUBLIC_KEY_FAIL_STATUS); } else if (e.getMessage().toLowerCase().contains("auth fail") || e.getMessage().toLowerCase().contains("auth cancel")) { hostSystem.setStatusCd(HostSystem.AUTH_FAIL_STATUS); } else if (e.getMessage().toLowerCase().contains("unknownhostexception")) { hostSystem.setErrorMsg("DNS Lookup Failed"); hostSystem.setStatusCd(HostSystem.HOST_FAIL_STATUS); } else { hostSystem.setStatusCd(HostSystem.GENERIC_FAIL_STATUS); } } return null; } }
RemoteInvokeHandler { public static ConnectionSession getSession(ApplicationKeyDO applicationKeyDO, HostSystem hostSystem) { JSch jSch = new JSch(); String passphrase = applicationKeyDO.getPassphrase(); if (StringUtils.isEmpty(passphrase)) { passphrase = ""; } try { jSch.addIdentity(applicationKeyDO.getSessionId() + "", EncryptionUtil.decrypt(applicationKeyDO.getPrivateKey()).getBytes(), applicationKeyDO.getPublicKey().getBytes(), passphrase.getBytes()); Session session = jSch.getSession(hostSystem.getUser(), hostSystem.getHost(), hostSystem.getPort() == null ? 22 : hostSystem.getPort()); session.setConfig("StrictHostKeyChecking", "no"); session.setConfig("PreferredAuthentications", "publickey,keyboard-interactive,password"); session.setServerAliveInterval(SERVER_ALIVE_INTERVAL); session.connect(SESSION_TIMEOUT); Channel channel = session.openChannel("shell"); ((ChannelShell) channel).setAgentForwarding(true); ((ChannelShell) channel).setPtyType("xterm"); channel.connect(); ConnectionSession connectionSession = new ConnectionSession(channel, session); return connectionSession; } catch (Exception e) { logger.error(e.getMessage(), e); hostSystem.setErrorMsg(e.getMessage()); if (e.getMessage().toLowerCase().contains("userauth fail")) { hostSystem.setStatusCd(HostSystem.PUBLIC_KEY_FAIL_STATUS); } else if (e.getMessage().toLowerCase().contains("auth fail") || e.getMessage().toLowerCase().contains("auth cancel")) { hostSystem.setStatusCd(HostSystem.AUTH_FAIL_STATUS); } else if (e.getMessage().toLowerCase().contains("unknownhostexception")) { hostSystem.setErrorMsg("DNS Lookup Failed"); hostSystem.setStatusCd(HostSystem.HOST_FAIL_STATUS); } else { hostSystem.setStatusCd(HostSystem.GENERIC_FAIL_STATUS); } } return null; }  }
RemoteInvokeHandler { public static ConnectionSession getSession(ApplicationKeyDO applicationKeyDO, HostSystem hostSystem) { JSch jSch = new JSch(); String passphrase = applicationKeyDO.getPassphrase(); if (StringUtils.isEmpty(passphrase)) { passphrase = ""; } try { jSch.addIdentity(applicationKeyDO.getSessionId() + "", EncryptionUtil.decrypt(applicationKeyDO.getPrivateKey()).getBytes(), applicationKeyDO.getPublicKey().getBytes(), passphrase.getBytes()); Session session = jSch.getSession(hostSystem.getUser(), hostSystem.getHost(), hostSystem.getPort() == null ? 22 : hostSystem.getPort()); session.setConfig("StrictHostKeyChecking", "no"); session.setConfig("PreferredAuthentications", "publickey,keyboard-interactive,password"); session.setServerAliveInterval(SERVER_ALIVE_INTERVAL); session.connect(SESSION_TIMEOUT); Channel channel = session.openChannel("shell"); ((ChannelShell) channel).setAgentForwarding(true); ((ChannelShell) channel).setPtyType("xterm"); channel.connect(); ConnectionSession connectionSession = new ConnectionSession(channel, session); return connectionSession; } catch (Exception e) { logger.error(e.getMessage(), e); hostSystem.setErrorMsg(e.getMessage()); if (e.getMessage().toLowerCase().contains("userauth fail")) { hostSystem.setStatusCd(HostSystem.PUBLIC_KEY_FAIL_STATUS); } else if (e.getMessage().toLowerCase().contains("auth fail") || e.getMessage().toLowerCase().contains("auth cancel")) { hostSystem.setStatusCd(HostSystem.AUTH_FAIL_STATUS); } else if (e.getMessage().toLowerCase().contains("unknownhostexception")) { hostSystem.setErrorMsg("DNS Lookup Failed"); hostSystem.setStatusCd(HostSystem.HOST_FAIL_STATUS); } else { hostSystem.setStatusCd(HostSystem.GENERIC_FAIL_STATUS); } } return null; }  static ConnectionSession getSession(ApplicationKeyDO applicationKeyDO, HostSystem hostSystem); }
RemoteInvokeHandler { public static ConnectionSession getSession(ApplicationKeyDO applicationKeyDO, HostSystem hostSystem) { JSch jSch = new JSch(); String passphrase = applicationKeyDO.getPassphrase(); if (StringUtils.isEmpty(passphrase)) { passphrase = ""; } try { jSch.addIdentity(applicationKeyDO.getSessionId() + "", EncryptionUtil.decrypt(applicationKeyDO.getPrivateKey()).getBytes(), applicationKeyDO.getPublicKey().getBytes(), passphrase.getBytes()); Session session = jSch.getSession(hostSystem.getUser(), hostSystem.getHost(), hostSystem.getPort() == null ? 22 : hostSystem.getPort()); session.setConfig("StrictHostKeyChecking", "no"); session.setConfig("PreferredAuthentications", "publickey,keyboard-interactive,password"); session.setServerAliveInterval(SERVER_ALIVE_INTERVAL); session.connect(SESSION_TIMEOUT); Channel channel = session.openChannel("shell"); ((ChannelShell) channel).setAgentForwarding(true); ((ChannelShell) channel).setPtyType("xterm"); channel.connect(); ConnectionSession connectionSession = new ConnectionSession(channel, session); return connectionSession; } catch (Exception e) { logger.error(e.getMessage(), e); hostSystem.setErrorMsg(e.getMessage()); if (e.getMessage().toLowerCase().contains("userauth fail")) { hostSystem.setStatusCd(HostSystem.PUBLIC_KEY_FAIL_STATUS); } else if (e.getMessage().toLowerCase().contains("auth fail") || e.getMessage().toLowerCase().contains("auth cancel")) { hostSystem.setStatusCd(HostSystem.AUTH_FAIL_STATUS); } else if (e.getMessage().toLowerCase().contains("unknownhostexception")) { hostSystem.setErrorMsg("DNS Lookup Failed"); hostSystem.setStatusCd(HostSystem.HOST_FAIL_STATUS); } else { hostSystem.setStatusCd(HostSystem.GENERIC_FAIL_STATUS); } } return null; }  static ConnectionSession getSession(ApplicationKeyDO applicationKeyDO, HostSystem hostSystem); static final int SERVER_ALIVE_INTERVAL; static final int SESSION_TIMEOUT; }