public Pair<Boolean, String> executeSshWrapper(final String hostIp, final int port, final String username, final File pemFile, final String hostPasswd, final String command) throws Exception { final Pair<Boolean, String> result = SshHelper.sshExecute(hostIp, port, username, pemFile, hostPasswd, command, 60000, 60000, TIMEOUT); return result; }
private static void deleteCertFile(final String nsIp, final String username, final String password, final String certFilename) throws Exception { SshHelper.sshExecute(nsIp, SSH_PORT, username, null, password, "shell rm " + SSL_CERT_PATH + certFilename); }
public static Pair<Boolean, String> sshExecute(String host, int port, String user, File pemKeyFile, String password, String command) throws Exception { return sshExecute(host, port, user, pemKeyFile, password, command, DEFAULT_CONNECT_TIMEOUT, DEFAULT_KEX_TIMEOUT, 120000); }
private static void deleteKeyFile(final String nsIp, final String username, final String password, final String keyFilename) throws Exception { SshHelper.sshExecute(nsIp, SSH_PORT, username, null, password, "shell rm " + SSL_CERT_PATH + keyFilename); }
public static Pair<Boolean, String> sshExecute(String host, int port, String user, File pemKeyFile, String password, String command, Duration connectTimeout, Duration kexTimeout, Duration waitTime) throws Exception { return sshExecute(host, port, user, pemKeyFile, password, command, (int)connectTimeout.getMillis(), (int)kexTimeout.getMillis(), (int)waitTime.getMillis()); }
@Override public ExecutionResult executeInVR(final String routerIP, final String script, final String args, final Duration timeout) { Pair<Boolean, String> result; String cmdline = "/opt/cloud/bin/router_proxy.sh " + script + " " + routerIP + " " + args; // semicolon need to be escape for bash cmdline = cmdline.replaceAll(";", "\\\\;"); try { s_logger.debug("Executing command in VR: " + cmdline); result = SshHelper.sshExecute(_host.getIp(), 22, _username, null, _password.peek(), cmdline, VRScripts.CONNECTION_TIMEOUT, VRScripts.CONNECTION_TIMEOUT, timeout); } catch (final Exception e) { return new ExecutionResult(false, e.getMessage()); } return new ExecutionResult(result.first(), result.second()); }
protected String networkUsage(final String privateIpAddress, final String option, final String ethName) { String args = null; if (option.equals("get")) { args = "-g"; } else if (option.equals("create")) { args = "-c"; } else if (option.equals("reset")) { args = "-r"; } else if (option.equals("addVif")) { args = "-a"; args += ethName; } else if (option.equals("deleteVif")) { args = "-d"; args += ethName; } try { if (s_logger.isTraceEnabled()) { s_logger.trace("Executing /opt/cloud/bin/netusage.sh " + args + " on DomR " + privateIpAddress); } final Pair<Boolean, String> result = SshHelper.sshExecute(privateIpAddress, DEFAULT_DOMR_SSHPORT, "root", getSystemVMKeyFile(), null, "/opt/cloud/bin/netusage.sh " + args); if (!result.first()) { return null; } return result.second(); } catch (final Throwable e) { s_logger.error("Unable to execute NetworkUsage command on DomR (" + privateIpAddress + "), domR may not be ready yet. failure due to " + e); } return null; }
final Pair<Boolean, String> result = SshHelper.sshExecute(routerIp, DEFAULT_DOMR_SSHPORT, "root", getSystemVMKeyFile(), null, "ls /proc/sys/net/ipv4/conf"); s_logger.debug("Run domr script " + cmd); final Pair<Boolean, String> result2 = SshHelper.sshExecute(routerIp, DEFAULT_DOMR_SSHPORT, "root", getSystemVMKeyFile(), null,
protected Answer execute(final PingTestCommand cmd) { if (s_logger.isInfoEnabled()) { s_logger.info("Executing resource PingTestCommand: " + s_gson.toJson(cmd)); } final String controlIp = cmd.getRouterIp(); final String args = " -c 1 -n -q " + cmd.getPrivateIp(); try { final Pair<Boolean, String> result = SshHelper.sshExecute(controlIp, DEFAULT_DOMR_SSHPORT, "root", getSystemVMKeyFile(), null, "/bin/ping" + args); if (result.first()) { return new Answer(cmd); } } catch (final Exception e) { s_logger.error("Unable to execute ping command on DomR (" + controlIp + "), domR may not be ready yet. failure due to " + e.getMessage()); } return new Answer(cmd, false, "PingTestCommand failed"); }
@Override public ExecutionResult executeInVR(final String routerIP, final String script, final String args, final Duration timeout) { Pair<Boolean, String> result; //TODO: Password should be masked, cannot output to log directly if (s_logger.isDebugEnabled()) { s_logger.debug("Run command on VR: " + routerIP + ", script: " + script + " with args: " + args); } try { result = SshHelper.sshExecute(routerIP, DEFAULT_DOMR_SSHPORT, "root", getSystemVMKeyFile(), null, "/opt/cloud/bin/" + script + " " + args, VRScripts.CONNECTION_TIMEOUT, VRScripts.CONNECTION_TIMEOUT, timeout); } catch (final Exception e) { final String msg = "Command failed due to " + e ; s_logger.error(msg); result = new Pair<Boolean, String>(false, msg); } if (s_logger.isDebugEnabled()) { s_logger.debug(script + " execution result: " + result.first().toString()); } return new ExecutionResult(result.first(), result.second()); }
protected Answer execute(final VmDataCommand cmd) { if (s_logger.isInfoEnabled()) { s_logger.info("Executing resource VmDataCommand: " + s_gson.toJson(cmd)); } final String controlIp = getRouterSshControlIp(cmd); final Map<String, List<String[]>> data = new HashMap<String, List<String[]>>(); data.put(cmd.getVmIpAddress(), cmd.getVmData()); String json = new Gson().toJson(data); s_logger.debug("VM data JSON IS:" + json); json = Base64.encodeBase64String(json.getBytes(Charset.forName("UTF-8"))); final String command = String.format("%s%s %s %s", "/opt/cloud/bin/", VRScripts.VMDATA, "-d", json); try { final Pair<Boolean, String> result = SshHelper.sshExecute(controlIp, DEFAULT_DOMR_SSHPORT, "root", getSystemVMKeyFile(), null, command); if (!result.first()) { s_logger.error("vm_data command on domain router " + controlIp + " failed. messge: " + result.second()); return new Answer(cmd, false, "VmDataCommand failed due to " + result.second()); } if (s_logger.isInfoEnabled()) { s_logger.info("vm_data command on domain router " + controlIp + " completed"); } } catch (final Throwable e) { final String msg = "VmDataCommand failed due to " + e.getMessage(); s_logger.error(msg, e); return new Answer(cmd, false, msg); } return new Answer(cmd); }
protected Answer execute(final SetMonitorServiceCommand cmd) { if (s_logger.isInfoEnabled()) { s_logger.info("Executing resource SetMonitorServiceCommand: " + s_gson.toJson(cmd)); } final String controlIp = getRouterSshControlIp(cmd); final String config = cmd.getConfiguration(); final String args = String.format(" %s %s", "-c", config); final String command = String.format("%s%s %s", "/opt/cloud/bin/", VRScripts.MONITOR_SERVICE, args); try { final Pair<Boolean, String> result = SshHelper.sshExecute(controlIp, DEFAULT_DOMR_SSHPORT, "root", getSystemVMKeyFile(), null, command); if (!result.first()) { final String msg= "monitor_service.sh failed on domain router " + controlIp + " failed " + result.second(); s_logger.error(msg); return new Answer(cmd, false, msg); } return new Answer(cmd); } catch (final Throwable e) { s_logger.error("Unexpected exception: " + e.toString(), e); return new Answer(cmd, false, "SetMonitorServiceCommand failed due to " + e); } }
final Pair<Boolean, String> result = SshHelper.sshExecute(routerIp, DEFAULT_DOMR_SSHPORT, "root", getSystemVMKeyFile(), null, "ip address | grep DOWN| cut -f2 -d :"); s_logger.debug("Run domr script " + cmd); final Pair<Boolean, String> result2 = SshHelper.sshExecute(routerIp, DEFAULT_DOMR_SSHPORT, "root", getSystemVMKeyFile(), null,
final Pair<Boolean, String> result = SshHelper.sshExecute(controlIp, DEFAULT_DOMR_SSHPORT, "root", getSystemVMKeyFile(), null, command);
protected Answer execute(final VpnUsersCfgCommand cmd) { final String controlIp = getRouterSshControlIp(cmd); for (final VpnUsersCfgCommand.UsernamePassword userpwd : cmd.getUserpwds()) { final StringBuffer argsBuf = new StringBuffer(); if (!userpwd.isAdd()) { argsBuf.append(" -U ").append(userpwd.getUsername()); } else { argsBuf.append(" -u ").append(userpwd.getUsernamePassword()); } try { if (s_logger.isDebugEnabled()) { s_logger.debug("Executing /opt/cloud/bin/vpn_lt2p.sh "); } final Pair<Boolean, String> result = SshHelper.sshExecute(controlIp, DEFAULT_DOMR_SSHPORT, "root", getSystemVMKeyFile(), null, "/opt/cloud/bin/vpn_l2tp.sh " + argsBuf.toString()); if (!result.first()) { s_logger.error("VpnUserCfg command on domR failed, message: " + result.second()); return new Answer(cmd, false, "VpnUserCfg command failed due to " + result.second()); } } catch (final Throwable e) { if (e instanceof RemoteException) { s_logger.warn(e.getMessage()); } final String msg = "VpnUserCfg command failed due to " + e.getMessage(); s_logger.error(msg, e); return new Answer(cmd, false, msg); } } return new Answer(cmd); } private SetStaticRouteAnswer execute(final SetStaticRouteCommand cmd) {
result = SshHelper.sshExecute(controlIp, DEFAULT_DOMR_SSHPORT, "root", getSystemVMKeyFile(), null, cmdline.toString());
protected Answer execute(final CheckRouterCommand cmd) { final String command = String.format("%s%s", "/opt/cloud/bin/", VRScripts.RVR_CHECK); if (s_logger.isDebugEnabled()) { s_logger.debug("Executing resource CheckRouterCommand: " + s_gson.toJson(cmd)); s_logger.debug("Run command on domR " + cmd.getAccessDetail(NetworkElementCommand.ROUTER_IP) + command); } Pair<Boolean, String> result; try { final String controlIp = getRouterSshControlIp(cmd); result = SshHelper.sshExecute(controlIp, DEFAULT_DOMR_SSHPORT, "root", getSystemVMKeyFile(), null, command); if (!result.first()) { s_logger.error("check router command on domR " + cmd.getAccessDetail(NetworkElementCommand.ROUTER_IP) + " failed, message: " + result.second()); return new CheckRouterAnswer(cmd, "CheckRouter failed due to " + result.second()); } if (s_logger.isDebugEnabled()) { s_logger.debug("check router command on domain router " + cmd.getAccessDetail(NetworkElementCommand.ROUTER_IP) + " completed"); } } catch (final Throwable e) { final String msg = "CheckRouterCommand failed due to " + e.getMessage(); s_logger.error(msg, e); return new CheckRouterAnswer(cmd, msg); } return new CheckRouterAnswer(cmd, result.second(), true); }
protected Answer execute(final GetDomRVersionCmd cmd) { final String command = String.format("%s%s", "/opt/cloud/bin/", VRScripts.VERSION); if (s_logger.isDebugEnabled()) { s_logger.debug("Executing resource GetDomRVersionCmd: " + s_gson.toJson(cmd)); s_logger.debug("Run command on domR " + cmd.getAccessDetail(NetworkElementCommand.ROUTER_IP) + command); } Pair<Boolean, String> result; try { final String controlIp = getRouterSshControlIp(cmd); result = SshHelper.sshExecute(controlIp, DEFAULT_DOMR_SSHPORT, "root", getSystemVMKeyFile(), null, command); if (!result.first()) { s_logger.error("GetDomRVersionCmd on domR " + cmd.getAccessDetail(NetworkElementCommand.ROUTER_IP) + " failed, message: " + result.second()); return new GetDomRVersionAnswer(cmd, "GetDomRVersionCmd failed due to " + result.second()); } if (s_logger.isDebugEnabled()) { s_logger.debug("GetDomRVersionCmd on domain router " + cmd.getAccessDetail(NetworkElementCommand.ROUTER_IP) + " completed"); } } catch (final Throwable e) { final String msg = "GetDomRVersionCmd failed due to " + e; s_logger.error(msg, e); return new GetDomRVersionAnswer(cmd, msg); } final String[] lines = result.second().split("&"); if (lines.length != 2) { return new GetDomRVersionAnswer(cmd, result.second()); } return new GetDomRVersionAnswer(cmd, result.second(), lines[0], lines[1]); }
protected SetSourceNatAnswer execute(final SetSourceNatCommand cmd) { if (s_logger.isInfoEnabled()) { s_logger.info("Executing resource SetSourceNatCommand " + s_gson.toJson(cmd)); } final String routerName = cmd.getAccessDetail(NetworkElementCommand.ROUTER_NAME); final String routerIp = getRouterSshControlIp(cmd); final IpAddressTO pubIp = cmd.getIpAddress(); try { final int ethDeviceNum = findRouterEthDeviceIndex(routerName, routerIp, pubIp.getVifMacAddress()); String args = ""; args += " -A "; args += " -l "; args += pubIp.getPublicIp(); args += " -c "; args += "eth" + ethDeviceNum; final String command = String.format("%s%s %s", "/opt/cloud/bin/", VRScripts.VPC_SOURCE_NAT, args); final Pair<Boolean, String> result = SshHelper.sshExecute(routerIp, DEFAULT_DOMR_SSHPORT, "root", getSystemVMKeyFile(), null, command); if (!result.first()) { final String msg = "SetupGuestNetworkCommand on domain router " + routerIp + " failed. message: " + result.second(); s_logger.error(msg); return new SetSourceNatAnswer(cmd, false, msg); } return new SetSourceNatAnswer(cmd, true, "success"); } catch (final Exception e) { final String msg = "Ip SNAT failure due to " + e.toString(); s_logger.error(msg, e); return new SetSourceNatAnswer(cmd, false, msg); } }
protected Answer execute(final SavePasswordCommand cmd) { if (s_logger.isInfoEnabled()) { s_logger.info("Executing resource SavePasswordCommand. vmName: " + cmd.getVmName() + ", vmIp: " + cmd.getVmIpAddress() + ", password: " + StringUtils.getMaskedPasswordForDisplay(cmd.getPassword())); } final String controlIp = getRouterSshControlIp(cmd); final String password = cmd.getPassword(); final String vmIpAddress = cmd.getVmIpAddress(); // Run save_password_to_domr.sh final String command = String.format("%s%s %s %s %s %s", "/opt/cloud/bin/", VRScripts.PASSWORD, "-v", vmIpAddress, "-p", password); if (s_logger.isDebugEnabled()) { final String debugCommand = String.format("%s%s %s %s %s %s", "/opt/cloud/bin/", VRScripts.PASSWORD, "-v", vmIpAddress, "-p", StringUtils.getMaskedPasswordForDisplay(cmd.getPassword())); s_logger.debug("Run command on domain router " + controlIp + debugCommand); } try { final Pair<Boolean, String> result = SshHelper.sshExecute(controlIp, DEFAULT_DOMR_SSHPORT, "root", getSystemVMKeyFile(), null, command); if (!result.first()) { s_logger.error("savepassword command on domain router " + controlIp + " failed, message: " + result.second()); return new Answer(cmd, false, "SavePassword failed due to " + result.second()); } if (s_logger.isInfoEnabled()) { s_logger.info("savepassword command on domain router " + controlIp + " completed"); } } catch (final Throwable e) { final String msg = "SavePasswordCommand failed due to " + e; s_logger.error(msg, e); return new Answer(cmd, false, msg); } return new Answer(cmd); }