use of net.schmizz.sshj.SSHClient in project asterixdb by apache.
the class StartDataGeneratorAction method main.
public static void main(String[] args) throws Exception {
SSHClient sshClient = new SSHClient();
sshClient.loadKnownHosts();
sshClient.connect("asterix-1.ics.uci.edu");
sshClient.authPublickey("zheilbro", "/Users/zheilbron/.ssh/id_rsa");
Session session = sshClient.startSession();
Command lsCmd = session.exec("ls");
StringWriter sw = new StringWriter();
IOUtils.copy(lsCmd.getInputStream(), sw);
IOUtils.copy(lsCmd.getErrorStream(), sw);
System.out.println(sw.toString());
session.close();
sw.close();
sshClient.close();
}
use of net.schmizz.sshj.SSHClient in project vcell by virtualcell.
the class SimDataConnection method downloadSimData.
public static File downloadSimData(File tempSimDir, String vcellClusterPassword, String userName, KeyValue simIDKey, Integer jobIndex, boolean isOld) throws Exception {
String simDataLogFileName = SimulationData.createCanonicalSimLogFileName(simIDKey, jobIndex, isOld);
SSHClient ssh = null;
SFTPClient sftpClient = null;
if (tempSimDir == null) {
tempSimDir = File.createTempFile("SimID_" + simIDKey + "_" + jobIndex + "_", ".dir");
if (!tempSimDir.delete()) {
throw new Exception("Couldn't make local dir " + tempSimDir);
}
if (!tempSimDir.mkdir()) {
throw new Exception("Couldn't make local dir " + tempSimDir);
}
}
if (!tempSimDir.exists()) {
throw new Exception("Directory not exist " + tempSimDir);
}
File localSimDir = new File(tempSimDir, userName);
if (!localSimDir.exists()) {
if (!localSimDir.mkdir()) {
throw new Exception("Couldn't make local dir " + localSimDir);
}
}
System.out.println("Local download dir '" + localSimDir + "'");
final String compareStr = "SimID_" + simIDKey.toString() + (isOld ? "." : "_" + jobIndex + "_");
System.out.println(compareStr);
RemoteResourceFilter remoteResourceFilter = new RemoteResourceFilter() {
@Override
public boolean accept(RemoteResourceInfo arg0) {
return arg0.getName().startsWith(compareStr);
}
};
try {
System.out.println("Creating ssh connection...");
ssh = createSSHClient(dataServer, "vcell", vcellClusterPassword);
System.out.println("Creating ftpClient...");
sftpClient = ssh.newSFTPClient();
sftpClient.getFileTransfer().setPreserveAttributes(false);
String primaryFullPath = primaryPath + userName + "/" + simDataLogFileName;
System.out.println("Seeking primary location..." + primaryFullPath);
FileAttributes fileAttributes = sftpClient.statExistence(primaryFullPath);
boolean bExist = (fileAttributes == null ? false : true);
if (bExist) {
System.out.println("Downloading primary location...");
List<RemoteResourceInfo> remoteResourceInfos = sftpClient.ls(primaryPath + userName, remoteResourceFilter);
download(sftpClient, localSimDir, remoteResourceInfos);
// printRemoteResourceInfo(remoteResourceInfos);
// sftpClient.get(primaryFullPath, logF.getAbsolutePath());
// System.out.println("Downloaded data from "+dataServer+":"+primaryFullPath+" to "+logF);
} else {
String secondaryFullPath = secondaryPath + userName + "/" + simDataLogFileName;
System.out.println("Seeking secondary location..." + secondaryFullPath);
fileAttributes = sftpClient.statExistence(secondaryFullPath);
bExist = (fileAttributes == null ? false : true);
if (bExist) {
System.out.println("Downloading secondary location...");
List<RemoteResourceInfo> remoteResourceInfos = sftpClient.ls(secondaryPath + userName, remoteResourceFilter);
download(sftpClient, localSimDir, remoteResourceInfos);
// printRemoteResourceInfo(remoteResourceInfos);
// sftpClient.get(secondaryFullPath, logF.getAbsolutePath());
// System.out.println("Downloaded data from "+dataServer+":"+secondaryFullPath+" to "+logF);
} else {
System.out.println("Data Not Found for simid " + simIDKey);
}
}
return localSimDir;
} finally {
if (sftpClient != null) {
try {
sftpClient.close();
} catch (Exception e) {
e.printStackTrace();
}
}
if (ssh != null) {
try {
ssh.disconnect();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
use of net.schmizz.sshj.SSHClient in project vcell by virtualcell.
the class SimDataConnection method createSSHClient.
private static SSHClient createSSHClient(String host, String user, String password) throws Exception {
SSHClient sshClient = null;
try {
sshClient = new SSHClient();
// !!! only use the PromiscuousVerifier if the VMs you are connecting to are known and trusted !!!
sshClient.addHostKeyVerifier(new PromiscuousVerifier());
sshClient.connect(host);
sshClient.authPassword(user, password);
return sshClient;
} catch (Exception e) {
if (sshClient != null) {
try {
sshClient.disconnect();
} catch (Exception e2) {
e.printStackTrace();
}
}
throw e;
}
}
Aggregations