use of com.trilead.ssh2.Connection in project cloudstack by apache.
the class OvmResourceBase method setupServer.
protected void setupServer() throws IOException {
com.trilead.ssh2.Connection sshConnection = new com.trilead.ssh2.Connection(_ip, 22);
sshConnection.connect(null, 60000, 60000);
if (!sshConnection.authenticateWithPassword(_username, _password)) {
throw new CloudRuntimeException("Unable to authenticate");
}
SCPClient scp = new SCPClient(sshConnection);
String configScriptName = "scripts/vm/hypervisor/ovm/configureOvm.sh";
String configScriptPath = Script.findScript("", configScriptName);
if (configScriptPath == null) {
throw new CloudRuntimeException("Unable to find " + configScriptName);
}
scp.put(configScriptPath, "/usr/bin/", "0755");
if (!SSHCmdHelper.sshExecuteCmd(sshConnection, "sh /usr/bin/configureOvm.sh preSetup")) {
throw new CloudRuntimeException("Execute configureOvm.sh preSetup failed on " + _ip);
}
File tmp = new File(configScriptPath);
File scriptDir = new File(tmp.getParent());
File[] scripts = scriptDir.listFiles();
for (int i = 0; i < scripts.length; i++) {
File script = scripts[i];
if (script.getName().equals("configureOvm.sh")) {
continue;
}
if (s_logger.isDebugEnabled()) {
s_logger.debug("Copying " + script.getPath() + " to " + s_ovsAgentPath + " on " + _ip + " with permission 0644");
}
scp.put(script.getPath(), s_ovsAgentPath, "0644");
}
sshConnection = SSHCmdHelper.acquireAuthorizedConnection(_ip, _username, _password);
if (sshConnection == null) {
throw new CloudRuntimeException(String.format("Cannot connect to ovm host(IP=%1$s, username=%2$s, password=%3$s", _ip, _username, _password));
}
if (!SSHCmdHelper.sshExecuteCmd(sshConnection, "sh /usr/bin/configureOvm.sh postSetup")) {
throw new CloudRuntimeException("Execute configureOvm.sh postSetup failed on " + _ip);
}
}
use of com.trilead.ssh2.Connection in project cloudstack by apache.
the class SshHelper method scpFrom.
public static void scpFrom(String host, int port, String user, File permKeyFile, String localTargetDirectory, String remoteTargetFile) throws Exception {
com.trilead.ssh2.Connection conn = null;
com.trilead.ssh2.SCPClient scpClient = null;
try {
conn = new com.trilead.ssh2.Connection(host, port);
conn.connect(null, DEFAULT_CONNECT_TIMEOUT, DEFAULT_KEX_TIMEOUT);
if (!conn.authenticateWithPublicKey(user, permKeyFile, null)) {
String msg = "Failed to authentication SSH user " + user + " on host " + host;
s_logger.error(msg);
throw new Exception(msg);
}
scpClient = conn.createSCPClient();
scpClient.get(remoteTargetFile, localTargetDirectory);
} finally {
if (conn != null) {
conn.close();
}
}
}
use of com.trilead.ssh2.Connection in project cloudstack by apache.
the class SshHelper method sshExecute.
public static Pair<Boolean, String> sshExecute(String host, int port, String user, File pemKeyFile, String password, String command, int connectTimeoutInMs, int kexTimeoutInMs, int waitResultTimeoutInMs) throws Exception {
com.trilead.ssh2.Connection conn = null;
com.trilead.ssh2.Session sess = null;
try {
conn = new com.trilead.ssh2.Connection(host, port);
conn.connect(null, connectTimeoutInMs, kexTimeoutInMs);
if (pemKeyFile == null) {
if (!conn.authenticateWithPassword(user, password)) {
String msg = "Failed to authentication SSH user " + user + " on host " + host;
s_logger.error(msg);
throw new Exception(msg);
}
} else {
if (!conn.authenticateWithPublicKey(user, pemKeyFile, password)) {
String msg = "Failed to authentication SSH user " + user + " on host " + host;
s_logger.error(msg);
throw new Exception(msg);
}
}
sess = openConnectionSession(conn);
sess.execCommand(command);
InputStream stdout = sess.getStdout();
InputStream stderr = sess.getStderr();
byte[] buffer = new byte[8192];
StringBuffer sbResult = new StringBuffer();
int currentReadBytes = 0;
while (true) {
throwSshExceptionIfStdoutOrStdeerIsNull(stdout, stderr);
if ((stdout.available() == 0) && (stderr.available() == 0)) {
int conditions = sess.waitForCondition(ChannelCondition.STDOUT_DATA | ChannelCondition.STDERR_DATA | ChannelCondition.EOF | ChannelCondition.EXIT_STATUS, waitResultTimeoutInMs);
throwSshExceptionIfConditionsTimeout(conditions);
if ((conditions & ChannelCondition.EXIT_STATUS) != 0) {
break;
}
if (canEndTheSshConnection(waitResultTimeoutInMs, sess, conditions)) {
break;
}
}
while ((currentReadBytes = stdout.read(buffer)) != -1) {
sbResult.append(new String(buffer, 0, currentReadBytes));
}
while ((currentReadBytes = stderr.read(buffer)) != -1) {
sbResult.append(new String(buffer, 0, currentReadBytes));
}
}
String result = sbResult.toString();
if (StringUtils.isBlank(result)) {
try {
result = IOUtils.toString(stdout, StandardCharsets.UTF_8);
} catch (IOException e) {
s_logger.error("Couldn't get content of input stream due to: " + e.getMessage());
return new Pair<Boolean, String>(false, result);
}
}
if (sess.getExitStatus() == null) {
// Exit status is NOT available. Returning failure result.
s_logger.error(String.format("SSH execution of command %s has no exit status set. Result output: %s", command, result));
return new Pair<Boolean, String>(false, result);
}
if (sess.getExitStatus() != null && sess.getExitStatus().intValue() != 0) {
s_logger.error(String.format("SSH execution of command %s has an error status code in return. Result output: %s", command, result));
return new Pair<Boolean, String>(false, result);
}
return new Pair<Boolean, String>(true, result);
} finally {
if (sess != null)
sess.close();
if (conn != null)
conn.close();
}
}
use of com.trilead.ssh2.Connection in project cloudstack by apache.
the class SshHelper method scpTo.
public static void scpTo(String host, int port, String user, File pemKeyFile, String password, String remoteTargetDirectory, String localFile, String fileMode, int connectTimeoutInMs, int kexTimeoutInMs) throws Exception {
com.trilead.ssh2.Connection conn = null;
com.trilead.ssh2.SCPClient scpClient = null;
try {
conn = new com.trilead.ssh2.Connection(host, port);
conn.connect(null, connectTimeoutInMs, kexTimeoutInMs);
if (pemKeyFile == null) {
if (!conn.authenticateWithPassword(user, password)) {
String msg = "Failed to authentication SSH user " + user + " on host " + host;
s_logger.error(msg);
throw new Exception(msg);
}
} else {
if (!conn.authenticateWithPublicKey(user, pemKeyFile, password)) {
String msg = "Failed to authentication SSH user " + user + " on host " + host;
s_logger.error(msg);
throw new Exception(msg);
}
}
scpClient = conn.createSCPClient();
if (fileMode != null)
scpClient.put(localFile, remoteTargetDirectory, fileMode);
else
scpClient.put(localFile, remoteTargetDirectory);
} finally {
if (conn != null)
conn.close();
}
}
use of com.trilead.ssh2.Connection in project cloudstack by apache.
the class SshHelper method scpTo.
public static void scpTo(String host, int port, String user, File pemKeyFile, String password, String remoteTargetDirectory, byte[] data, String remoteFileName, String fileMode, int connectTimeoutInMs, int kexTimeoutInMs) throws Exception {
com.trilead.ssh2.Connection conn = null;
com.trilead.ssh2.SCPClient scpClient = null;
try {
conn = new com.trilead.ssh2.Connection(host, port);
conn.connect(null, connectTimeoutInMs, kexTimeoutInMs);
if (pemKeyFile == null) {
if (!conn.authenticateWithPassword(user, password)) {
String msg = "Failed to authentication SSH user " + user + " on host " + host;
s_logger.error(msg);
throw new Exception(msg);
}
} else {
if (!conn.authenticateWithPublicKey(user, pemKeyFile, password)) {
String msg = "Failed to authentication SSH user " + user + " on host " + host;
s_logger.error(msg);
throw new Exception(msg);
}
}
scpClient = conn.createSCPClient();
if (fileMode != null)
scpClient.put(data, remoteFileName, remoteTargetDirectory, fileMode);
else
scpClient.put(data, remoteFileName, remoteTargetDirectory);
} finally {
if (conn != null)
conn.close();
}
}
Aggregations