use of ch.ethz.ssh2.SCPClient in project cloudstack by apache.
the class ScpTemplateDownloader method download.
@Override
public long download(boolean resume, DownloadCompleteCallback callback) {
if (_status == Status.ABORTED || _status == Status.UNRECOVERABLE_ERROR || _status == Status.DOWNLOAD_FINISHED) {
return 0;
}
_resume = resume;
_start = System.currentTimeMillis();
URI uri;
try {
uri = new URI(_downloadUrl);
} catch (URISyntaxException e1) {
_status = Status.UNRECOVERABLE_ERROR;
return 0;
}
String username = uri.getUserInfo();
String queries = uri.getQuery();
String password = null;
if (queries != null) {
String[] qs = queries.split("&");
for (String q : qs) {
String[] tokens = q.split("=");
if (tokens[0].equalsIgnoreCase("password")) {
password = tokens[1];
break;
}
}
}
int port = uri.getPort();
if (port == -1) {
port = 22;
}
File file = new File(_toFile);
com.trilead.ssh2.Connection sshConnection = new com.trilead.ssh2.Connection(uri.getHost(), port);
try {
if (_storage != null) {
file.createNewFile();
_storage.setWorldReadableAndWriteable(file);
}
sshConnection.connect(null, 60000, 60000);
if (!sshConnection.authenticateWithPassword(username, password)) {
throw new CloudRuntimeException("Unable to authenticate");
}
SCPClient scp = new SCPClient(sshConnection);
String src = uri.getPath();
_status = Status.IN_PROGRESS;
scp.get(src, _toDir);
if (!file.exists()) {
_status = Status.UNRECOVERABLE_ERROR;
s_logger.debug("unable to scp the file " + _downloadUrl);
return 0;
}
_status = Status.DOWNLOAD_FINISHED;
_totalBytes = file.length();
String downloaded = "(download complete)";
_errorString = "Downloaded " + _remoteSize + " bytes " + downloaded;
_downloadTime += System.currentTimeMillis() - _start;
return _totalBytes;
} catch (Exception e) {
s_logger.warn("Unable to download " + _downloadUrl, e);
_status = TemplateDownloader.Status.UNRECOVERABLE_ERROR;
_errorString = e.getMessage();
return 0;
} finally {
sshConnection.close();
if (_status == Status.UNRECOVERABLE_ERROR && file.exists()) {
file.delete();
}
if (callback != null) {
callback.downloadComplete(_status);
}
}
}
use of ch.ethz.ssh2.SCPClient 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 ch.ethz.ssh2.SCPClient in project cosmic by MissionCriticalCloud.
the class CitrixResourceBase method setupServer.
/* return : if setup is needed */
public boolean setupServer(final Connection conn, final Host host) {
final String packageVersion = CitrixResourceBase.class.getPackage().getImplementationVersion();
final String version = this.getClass().getName() + "-" + (packageVersion == null ? Long.toString(System.currentTimeMillis()) : packageVersion);
try {
/* push patches to XenServer */
final Host.Record hr = host.getRecord(conn);
final Iterator<String> it = hr.tags.iterator();
while (it.hasNext()) {
final String tag = it.next();
if (tag.startsWith("vmops-version-")) {
if (tag.contains(version)) {
s_logger.info(logX(host, "Host " + hr.address + " is already setup."));
return false;
} else {
it.remove();
}
}
}
final com.trilead.ssh2.Connection sshConnection = new com.trilead.ssh2.Connection(hr.address, 22);
try {
sshConnection.connect(null, 60000, 60000);
if (!sshConnection.authenticateWithPassword(_username, _password.peek())) {
throw new CloudRuntimeException("Unable to authenticate");
}
final String cmd = "mkdir -p /opt/cloud/bin /var/log/cloud";
if (!SSHCmdHelper.sshExecuteCmd(sshConnection, cmd)) {
throw new CloudRuntimeException("Cannot create directory /opt/cloud/bin on XenServer hosts");
}
final SCPClient scp = new SCPClient(sshConnection);
final List<File> files = getPatchFiles();
if (files == null || files.isEmpty()) {
throw new CloudRuntimeException("Can not find patch file");
}
for (final File file : files) {
final String path = file.getParentFile().getAbsolutePath() + "/";
final Properties props = PropertiesUtil.loadFromFile(file);
for (final Map.Entry<Object, Object> entry : props.entrySet()) {
final String k = (String) entry.getKey();
final String v = (String) entry.getValue();
assert k != null && k.length() > 0 && v != null && v.length() > 0 : "Problems with " + k + "=" + v;
final String[] tokens = v.split(",");
String f = null;
if (tokens.length == 3 && tokens[0].length() > 0) {
if (tokens[0].startsWith("/")) {
f = tokens[0];
} else if (tokens[0].startsWith("~")) {
final String homedir = System.getenv("HOME");
f = homedir + tokens[0].substring(1) + k;
} else {
f = path + tokens[0] + '/' + k;
}
} else {
f = path + k;
}
final String directoryPath = tokens[tokens.length - 1];
f = f.replace('/', File.separatorChar);
String permissions = "0755";
if (tokens.length == 3) {
permissions = tokens[1];
} else if (tokens.length == 2) {
permissions = tokens[0];
}
if (!new File(f).exists()) {
s_logger.warn("We cannot locate " + f);
continue;
}
if (s_logger.isDebugEnabled()) {
s_logger.debug("Copying " + f + " to " + directoryPath + " on " + hr.address + " with permission " + permissions);
}
if (!SSHCmdHelper.sshExecuteCmd(sshConnection, "mkdir -m 700 -p " + directoryPath)) {
s_logger.debug("Unable to create destination path: " + directoryPath + " on " + hr.address + ".");
}
try {
scp.put(f, directoryPath, permissions);
} catch (final IOException e) {
final String msg = "Unable to copy file " + f + " to path " + directoryPath + " with permissions " + permissions;
s_logger.debug(msg);
throw new CloudRuntimeException("Unable to setup the server: " + msg, e);
}
}
}
} catch (final IOException e) {
throw new CloudRuntimeException("Unable to setup the server correctly", e);
} finally {
sshConnection.close();
}
hr.tags.add("vmops-version-" + version);
host.setTags(conn, hr.tags);
return true;
} catch (final XenAPIException e) {
final String msg = "XenServer setup failed due to " + e.toString();
s_logger.warn(msg, e);
throw new CloudRuntimeException("Unable to get host information " + e.toString(), e);
} catch (final XmlRpcException e) {
final String msg = "XenServer setup failed due to " + e.getMessage();
s_logger.warn(msg, e);
throw new CloudRuntimeException("Unable to get host information ", e);
}
}
use of ch.ethz.ssh2.SCPClient in project Payara by payara.
the class InstallNodeSshCommand method copyToHostsInternal.
private void copyToHostsInternal(File zipFile, ArrayList<String> binDirFiles) throws IOException, InterruptedException, CommandException {
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
boolean prompt = promptPass;
for (String host : hosts) {
sshLauncher.init(getRemoteUser(), host, getRemotePort(), sshpassword, getSshKeyFile(), sshkeypassphrase, logger);
if (getSshKeyFile() != null && !sshLauncher.checkConnection()) {
// key auth failed, so use password auth
prompt = true;
}
if (prompt) {
String sshpass = null;
if (sshPasswords.containsKey(host))
sshpass = String.valueOf(sshPasswords.get(host));
else
sshpass = getSSHPassword(host);
// re-initialize
sshLauncher.init(getRemoteUser(), host, getRemotePort(), sshpass, getSshKeyFile(), sshkeypassphrase, logger);
prompt = false;
}
String sshInstallDir = getInstallDir().replace('\\', '/');
SFTPClient sftpClient = sshLauncher.getSFTPClient();
SCPClient scpClient = sshLauncher.getSCPClient();
try {
if (!sftpClient.exists(sshInstallDir)) {
sftpClient.mkdirs(sshInstallDir, 0755);
}
} catch (IOException ioe) {
logger.info(Strings.get("mkdir.failed", sshInstallDir, host));
throw new IOException(ioe);
}
// delete the sshInstallDir contents if non-empty
try {
// get list of file in DAS sshInstallDir
List<String> files = getListOfInstallFiles(sshInstallDir);
deleteRemoteFiles(sftpClient, files, sshInstallDir, getForce());
} catch (IOException ex) {
logger.finer("Failed to remove sshInstallDir contents");
throw new IOException(ex);
}
String zip = zipFile.getCanonicalPath();
try {
logger.log(Level.INFO, "Copying {0} ({1} bytes) to {2}:{3}", new Object[] { zip, zipFile.length(), host, sshInstallDir });
// Looks like we need to quote the paths to scp in case they
// contain spaces.
scpClient.put(zipFile.getAbsolutePath(), FileUtils.quoteString(sshInstallDir));
if (logger.isLoggable(Level.FINER))
logger.log(Level.FINER, "Copied {0} to {1}:{2}", new Object[] { zip, host, sshInstallDir });
} catch (IOException ex) {
logger.info(Strings.get("cannot.copy.zip.file", zip, host));
throw new IOException(ex);
}
try {
logger.log(Level.INFO, "Installing {0} into {1}:{2}", new Object[] { getArchiveName(), host, sshInstallDir });
String unzipCommand = "cd '" + sshInstallDir + "'; jar -xvf " + getArchiveName();
int status = sshLauncher.runCommand(unzipCommand, outStream);
if (status != 0) {
logger.info(Strings.get("jar.failed", host, outStream.toString()));
throw new CommandException("Remote command output: " + outStream.toString());
}
if (logger.isLoggable(Level.FINER))
logger.log(Level.FINER, "Installed {0} into {1}:{2}", new Object[] { getArchiveName(), host, sshInstallDir });
} catch (IOException ioe) {
logger.info(Strings.get("jar.failed", host, outStream.toString()));
throw new IOException(ioe);
}
try {
logger.log(Level.INFO, "Removing {0}:{1}/{2}", new Object[] { host, sshInstallDir, getArchiveName() });
sftpClient.rm(sshInstallDir + "/" + getArchiveName());
if (logger.isLoggable(Level.FINER)) {
logger.log(Level.FINER, "Removed {0}:{1}/{2}", new Object[] { host, sshInstallDir, getArchiveName() });
}
} catch (IOException ioe) {
logger.info(Strings.get("remove.glassfish.failed", host, sshInstallDir));
throw new IOException(ioe);
}
// unjarring doesn't retain file permissions, hence executables need
// to be fixed with proper permissions
logger.log(Level.INFO, "Fixing file permissions of all bin files under {0}:{1}", new Object[] { host, sshInstallDir });
try {
if (binDirFiles.isEmpty()) {
// binDirFiles can be empty if the archive isn't a fresh one
searchAndFixBinDirectoryFiles(sshInstallDir, sftpClient);
} else {
for (String binDirFile : binDirFiles) {
sftpClient.chmod((sshInstallDir + "/" + binDirFile), 0755);
}
}
if (logger.isLoggable(Level.FINER))
logger.log(Level.FINER, "Fixed file permissions of all bin files under {0}:{1}", new Object[] { host, sshInstallDir });
} catch (IOException ioe) {
logger.info(Strings.get("fix.permissions.failed", host, sshInstallDir));
throw new IOException(ioe);
}
logger.log(Level.INFO, "Fixing file permissions for nadmin file under {0}:{1}/{2}/lib", new Object[] { host, sshInstallDir, SystemPropertyConstants.getComponentName() });
try {
sftpClient.chmod((sshInstallDir + "/" + SystemPropertyConstants.getComponentName() + "/lib/nadmin"), 0755);
if (logger.isLoggable(Level.FINER))
logger.log(Level.FINER, "Fixed file permission for nadmin under {0}:{1}/{2}/lib/nadmin", new Object[] { host, sshInstallDir, SystemPropertyConstants.getComponentName() });
} catch (IOException ioe) {
logger.info(Strings.get("fix.permissions.failed", host, sshInstallDir));
throw new IOException(ioe);
}
sftpClient.close();
}
}
use of ch.ethz.ssh2.SCPClient in project CloudStack-archive by CloudStack-extras.
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 " + _account.get());
Thread.sleep(300000);
}
s_logger.info("Attempting to SSH into windows host " + host + " with retry attempt: " + retry + " for account " + _account.get());
Connection conn = new Connection(host);
conn.connect(null, 60000, 60000);
s_logger.info("User " + _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 + " + _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 " + _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();
}
}
}
}
Aggregations