use of ch.ethz.ssh2.SCPClient in project nimbus by nimbus-org.
the class SCPClientImpl method connect.
public void connect(String user, String host, int port, File pemFile, String passphrase) throws SCPException {
if (connection != null) {
throw new SCPException("It is already connected!");
}
if (!pemFile.exists()) {
throw new SCPException("The pemFile not exists! path=" + pemFile);
}
FileInputStream fis = null;
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
String pem = null;
try {
fis = new FileInputStream(pemFile);
int length = 0;
byte[] buf = new byte[1024];
while ((length = fis.read(buf)) != -1) {
baos.write(buf, 0, length);
}
pem = new String(baos.toByteArray());
} catch (IOException e) {
throw new SCPException("It failed to read pemFile! path=" + pemFile, e);
} finally {
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
}
}
}
connection = new Connection(host, port);
try {
if (isTcpNoDelay != null) {
connection.setTCPNoDelay(isTcpNoDelay.booleanValue());
}
if (serverHostKeyAlgorithms != null) {
connection.setServerHostKeyAlgorithms(serverHostKeyAlgorithms);
}
connection.connect(null, connectionTimeout, keyExchangeTimeout);
if (!connection.authenticateWithPublicKey(user, pem.toCharArray(), passphrase)) {
throw new SCPException("It failed to authenticate!");
}
scpClient = connection.createSCPClient();
} catch (IOException e) {
scpClient = null;
connection.close();
connection = null;
throw new SCPException("It failed to authenticate!", e);
}
}
use of ch.ethz.ssh2.SCPClient in project cloudstack by apache.
the class TestClientWithAPI method sshWinTest.
private static String sshWinTest(String host) {
if (host == null) {
s_logger.info("Did not receive a host back from test, ignoring win ssh test");
return null;
}
// We will retry 5 times before quitting
int retry = 1;
while (true) {
try {
if (retry > 0) {
s_logger.info("Retry attempt : " + retry + " ...sleeping 300 seconds before next attempt. Account is " + s_account.get());
Thread.sleep(300000);
}
s_logger.info("Attempting to SSH into windows host " + host + " with retry attempt: " + retry + " for account " + s_account.get());
Connection conn = new Connection(host);
conn.connect(null, 60000, 60000);
s_logger.info("User " + s_account.get() + " ssHed successfully into windows host " + host);
boolean success = false;
boolean isAuthenticated = conn.authenticateWithPassword("Administrator", "password");
if (isAuthenticated == false) {
return "Authentication failed";
} else {
s_logger.info("Authentication is successfull");
}
try {
SCPClient scp = new SCPClient(conn);
scp.put("wget.exe", "wget.exe", "C:\\Users\\Administrator", "0777");
s_logger.info("Successfully put wget.exe file");
} catch (Exception ex) {
s_logger.error("Unable to put wget.exe " + ex);
}
if (conn == null) {
s_logger.error("Connection is null");
}
Session sess = conn.openSession();
s_logger.info("User + " + s_account.get() + " executing : wget http://" + downloadUrl);
String downloadCommand = "wget http://" + downloadUrl + " && dir dump.bin";
sess.execCommand(downloadCommand);
InputStream stdout = sess.getStdout();
InputStream stderr = sess.getStderr();
byte[] buffer = new byte[8192];
while (true) {
if ((stdout.available() == 0) && (stderr.available() == 0)) {
int conditions = sess.waitForCondition(ChannelCondition.STDOUT_DATA | ChannelCondition.STDERR_DATA | ChannelCondition.EOF, 120000);
if ((conditions & ChannelCondition.TIMEOUT) != 0) {
s_logger.info("Timeout while waiting for data from peer.");
return null;
}
if ((conditions & ChannelCondition.EOF) != 0) {
if ((conditions & (ChannelCondition.STDOUT_DATA | ChannelCondition.STDERR_DATA)) == 0) {
break;
}
}
}
while (stdout.available() > 0) {
success = true;
int len = stdout.read(buffer);
if (// this check is somewhat paranoid
len > 0)
s_logger.info(new String(buffer, 0, len));
}
while (stderr.available() > 0) {
/* int len = */
stderr.read(buffer);
}
}
sess.close();
conn.close();
if (success) {
return null;
} else {
retry++;
if (retry == MAX_RETRY_WIN) {
return "SSH Windows Network test fail for account " + s_account.get();
}
}
} catch (Exception e) {
s_logger.error(e);
retry++;
if (retry == MAX_RETRY_WIN) {
return "SSH Windows Network test fail with error " + e.getMessage();
}
}
}
}
use of ch.ethz.ssh2.SCPClient in project cloudstack by apache.
the class StressTestDirectAttach method sshWinTest.
private static String sshWinTest(String host) {
if (host == null) {
s_logger.info("Did not receive a host back from test, ignoring win ssh test");
return null;
}
// We will retry 5 times before quitting
int retry = 1;
while (true) {
try {
if (retry > 0) {
s_logger.info("Retry attempt : " + retry + " ...sleeping 300 seconds before next attempt. Account is " + s_account.get());
Thread.sleep(300000);
}
s_logger.info("Attempting to SSH into windows host " + host + " with retry attempt: " + retry + " for account " + s_account.get());
Connection conn = new Connection(host);
conn.connect(null, 60000, 60000);
s_logger.info("User " + s_account.get() + " ssHed successfully into windows host " + host);
boolean success = false;
boolean isAuthenticated = conn.authenticateWithPassword("Administrator", "password");
if (isAuthenticated == false) {
return "Authentication failed";
} else {
s_logger.info("Authentication is successfull");
}
try {
SCPClient scp = new SCPClient(conn);
scp.put("wget.exe", "wget.exe", "C:\\Users\\Administrator", "0777");
s_logger.info("Successfully put wget.exe file");
} catch (Exception ex) {
s_logger.error("Unable to put wget.exe " + ex);
}
if (conn == null) {
s_logger.error("Connection is null");
}
Session sess = conn.openSession();
s_logger.info("User + " + s_account.get() + " executing : wget http://192.168.1.250/dump.bin");
sess.execCommand("wget http://192.168.1.250/dump.bin && dir dump.bin");
InputStream stdout = sess.getStdout();
InputStream stderr = sess.getStderr();
byte[] buffer = new byte[8192];
while (true) {
if ((stdout.available() == 0) && (stderr.available() == 0)) {
int conditions = sess.waitForCondition(ChannelCondition.STDOUT_DATA | ChannelCondition.STDERR_DATA | ChannelCondition.EOF, 120000);
if ((conditions & ChannelCondition.TIMEOUT) != 0) {
s_logger.info("Timeout while waiting for data from peer.");
return null;
}
if ((conditions & ChannelCondition.EOF) != 0) {
if ((conditions & (ChannelCondition.STDOUT_DATA | ChannelCondition.STDERR_DATA)) == 0) {
break;
}
}
}
while (stdout.available() > 0) {
success = true;
int len = stdout.read(buffer);
if (// this check is somewhat paranoid
len > 0)
s_logger.info(new String(buffer, 0, len));
}
while (stderr.available() > 0) {
/* int len = */
stderr.read(buffer);
}
}
sess.close();
conn.close();
if (success) {
Thread.sleep(120000);
return null;
} else {
retry++;
if (retry == MAX_RETRY_WIN) {
return "SSH Windows Network test fail for account " + s_account.get();
}
}
} catch (Exception e) {
s_logger.error(e);
retry++;
if (retry == MAX_RETRY_WIN) {
return "SSH Windows Network test fail with error " + e.getMessage();
}
}
}
}
use of ch.ethz.ssh2.SCPClient in project cloudstack by apache.
the class TestClient method sshWinTest.
private static String sshWinTest(String host) {
if (host == null) {
s_logger.info("Did not receive a host back from test, ignoring win ssh test");
return null;
}
// We will retry 5 times before quitting
int retry = 0;
while (true) {
try {
if (retry > 0) {
s_logger.info("Retry attempt : " + retry + " ...sleeping 300 seconds before next attempt");
Thread.sleep(300000);
}
s_logger.info("Attempting to SSH into windows host " + host + " with retry attempt: " + retry);
Connection conn = new Connection(host);
conn.connect(null, 60000, 60000);
s_logger.info("SSHed successfully into windows host " + host);
boolean success = false;
boolean isAuthenticated = conn.authenticateWithPassword("vmops", "vmops");
if (isAuthenticated == false) {
return "Authentication failed";
}
SCPClient scp = new SCPClient(conn);
scp.put("wget.exe", "");
Session sess = conn.openSession();
s_logger.info("Executing : wget http://172.16.0.220/dump.bin");
sess.execCommand("wget http://172.16.0.220/dump.bin && dir dump.bin");
InputStream stdout = sess.getStdout();
InputStream stderr = sess.getStderr();
byte[] buffer = new byte[8192];
while (true) {
if ((stdout.available() == 0) && (stderr.available() == 0)) {
int conditions = sess.waitForCondition(ChannelCondition.STDOUT_DATA | ChannelCondition.STDERR_DATA | ChannelCondition.EOF, 120000);
if ((conditions & ChannelCondition.TIMEOUT) != 0) {
s_logger.info("Timeout while waiting for data from peer.");
return null;
}
if ((conditions & ChannelCondition.EOF) != 0) {
if ((conditions & (ChannelCondition.STDOUT_DATA | ChannelCondition.STDERR_DATA)) == 0) {
break;
}
}
}
while (stdout.available() > 0) {
success = true;
int len = stdout.read(buffer);
if (// this check is somewhat paranoid
len > 0)
s_logger.info(new String(buffer, 0, len));
}
while (stderr.available() > 0) {
int len = stderr.read(buffer);
}
}
sess.close();
conn.close();
if (success) {
return null;
} else {
retry++;
if (retry == MAX_RETRY_WIN) {
return "SSH Windows Network test fail";
}
}
} catch (Exception e) {
retry++;
if (retry == MAX_RETRY_WIN) {
return "SSH Windows Network test fail with error " + e.getMessage();
}
}
}
}
use of ch.ethz.ssh2.SCPClient in project cloudstack by apache.
the class CitrixResourceBase method copyConfigDriveIsoToHost.
public boolean copyConfigDriveIsoToHost(final Connection conn, final SR sr, final String vmName) {
final String vmIso = "/tmp/" + vmName + "/configDrive/" + vmName + ".iso";
// scp file into the host
final com.trilead.ssh2.Connection sshConnection = new com.trilead.ssh2.Connection(_host.getIp(), 22);
try {
sshConnection.connect(null, 60000, 60000);
if (!sshConnection.authenticateWithPassword(_username, _password.peek())) {
throw new CloudRuntimeException("Unable to authenticate");
}
s_logger.debug("scp config drive iso file " + vmIso + " to host " + _host.getIp() + " path " + _configDriveIsopath);
final SCPClient scp = new SCPClient(sshConnection);
final String p = "0755";
scp.put(vmIso, _configDriveIsopath, p);
sr.scan(conn);
s_logger.debug("copied config drive iso to host " + _host);
} catch (final IOException e) {
s_logger.debug("failed to copy configdrive iso " + vmIso + " to host " + _host, e);
return false;
} catch (final XmlRpcException e) {
s_logger.debug("Failed to scan config drive iso SR " + _configDriveSRName + _host.getIp() + " in host " + _host, e);
return false;
} finally {
sshConnection.close();
// clean up the config drive files
final String configDir = "/tmp/" + vmName;
try {
deleteLocalFolder(configDir);
s_logger.debug("Successfully cleaned up config drive directory " + configDir + " after copying it to host ");
} catch (final Exception e) {
s_logger.debug("Failed to delete config drive folder :" + configDir + " for VM " + vmName + " " + e.getMessage());
}
}
return true;
}
Aggregations