use of ch.ethz.ssh2.SCPClient in project Payara by payara.
the class LogFilterForInstance method downloadAllInstanceLogFiles.
public void downloadAllInstanceLogFiles(ServiceLocator habitat, Server targetServer, Domain domain, Logger logger, String instanceName, String tempDirectoryOnServer, String instanceLogFileDirectory) throws IOException {
// method is used from collect-log-files command
// for Instance it's going through this loop. This will use ssh utility to get file from instance machine(remote machine) and
// store in tempDirectoryOnServer which is used to create zip file.
// Right now user needs to go through this URL to setup and configure ssh http://wikis.sun.com/display/GlassFish/3.1SSHSetup
SSHLauncher sshL = getSSHL(habitat);
String sNode = targetServer.getNodeRef();
Nodes nodes = domain.getNodes();
Node node = nodes.getNode(sNode);
if (node.getType().equals("SSH")) {
sshL.init(node, logger);
List<String> allInstanceLogFileName = getInstanceLogFileNames(habitat, targetServer, domain, logger, instanceName, instanceLogFileDirectory);
boolean noFileFound = true;
String sourceDir = getLoggingDirectoryForNode(instanceLogFileDirectory, node, sNode, instanceName);
SFTPClient sftpClient = sshL.getSFTPClient();
try {
List instanceLogFileNames = sftpClient.ls(sourceDir);
for (int i = 0; i < instanceLogFileNames.size(); i++) {
SFTPv3DirectoryEntry file = (SFTPv3DirectoryEntry) instanceLogFileNames.get(i);
String fileName = file.filename;
// code to remove . and .. file which is return from sftpclient ls method
if (!file.attributes.isDirectory() && !fileName.equals(".") && !fileName.equals("..") && fileName.contains(".log") && !fileName.contains(".log.")) {
noFileFound = false;
break;
}
}
} catch (Exception e) {
// if directory doesn't present or missing on remote machine. It happens due to bug 16451
noFileFound = true;
}
if (noFileFound) {
// this loop is used when user has changed value for server.log but not restarted the server.
sourceDir = getLoggingDirectoryForNodeWhenNoFilesFound(instanceLogFileDirectory, node, sNode, instanceName);
}
String[] remoteFileNames = new String[allInstanceLogFileName.size()];
for (int i = 0; i < allInstanceLogFileName.size(); i++) {
remoteFileNames[i] = sourceDir + File.separator + allInstanceLogFileName.get(i);
}
sftpClient.close();
SCPClient scpClient = sshL.getSCPClient();
scpClient.get(remoteFileNames, tempDirectoryOnServer);
} else if (node.getType().equals("DCOM")) {
List instanceLogFileNames = getInstanceLogFileNames(habitat, targetServer, domain, logger, instanceName, instanceLogFileDirectory);
String sourceDir = getLoggingDirectoryForNode(instanceLogFileDirectory, node, sNode, instanceName);
try {
DcomInfo info = new DcomInfo(node);
WindowsRemoteFileSystem wrfs = new WindowsRemoteFileSystem(info.getHost(), info.getUser(), info.getPassword());
for (int i = 0; i < instanceLogFileNames.size(); i++) {
String logFileName = (String) instanceLogFileNames.get(i);
WindowsRemoteFile wrf = new WindowsRemoteFile(wrfs, sourceDir + File.separator + logFileName);
File instanceLogFile = new File(tempDirectoryOnServer + File.separator + logFileName);
wrf.copyTo(instanceLogFile);
}
} catch (WindowsException ex) {
throw new IOException("Unable to download instance log file from DCOM Instance Node");
}
}
}
use of ch.ethz.ssh2.SCPClient in project warn-report by saaavsaaa.
the class LinuxExecUtil method upload.
public void upload(final String localFile, final String remoteTargetDirectory) throws IOException {
SCPClient clt = conn.createSCPClient();
clt.put(localFile, remoteTargetDirectory);
}
use of ch.ethz.ssh2.SCPClient in project cosmic by MissionCriticalCloud.
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;
}
use of ch.ethz.ssh2.SCPClient in project cosmic by MissionCriticalCloud.
the class ScpTemplateDownloader method download.
@Override
public long download(final boolean resume, final DownloadCompleteCallback callback) {
if (_status == Status.ABORTED || _status == Status.UNRECOVERABLE_ERROR || _status == Status.DOWNLOAD_FINISHED) {
return 0;
}
_resume = resume;
_start = System.currentTimeMillis();
final URI uri;
try {
uri = new URI(_downloadUrl);
} catch (final URISyntaxException e1) {
_status = Status.UNRECOVERABLE_ERROR;
return 0;
}
final String username = uri.getUserInfo();
final String queries = uri.getQuery();
String password = null;
if (queries != null) {
final String[] qs = queries.split("&");
for (final String q : qs) {
final String[] tokens = q.split("=");
if (tokens[0].equalsIgnoreCase("password")) {
password = tokens[1];
break;
}
}
}
int port = uri.getPort();
if (port == -1) {
port = 22;
}
final File file = new File(_toFile);
final 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");
}
final SCPClient scp = new SCPClient(sshConnection);
final 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();
final String downloaded = "(download complete)";
_errorString = "Downloaded " + _remoteSize + " bytes " + downloaded;
_downloadTime += System.currentTimeMillis() - _start;
return _totalBytes;
} catch (final 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 nimbus by nimbus-org.
the class SCPClientImpl method connect.
public void connect(String user, String host, int port, String password) throws SCPException {
if (connection != null) {
throw new SCPException("It is already connected!");
}
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.authenticateWithPassword(user, password)) {
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 connect!", e);
}
}
Aggregations