Search in sources :

Example 1 with SSHClient

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();
}
Also used : StringWriter(java.io.StringWriter) Command(net.schmizz.sshj.connection.channel.direct.Session.Command) SSHClient(net.schmizz.sshj.SSHClient) Session(net.schmizz.sshj.connection.channel.direct.Session)

Example 2 with SSHClient

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();
            }
        }
    }
}
Also used : SSHClient(net.schmizz.sshj.SSHClient) SFTPClient(net.schmizz.sshj.sftp.SFTPClient) RemoteResourceInfo(net.schmizz.sshj.sftp.RemoteResourceInfo) File(java.io.File) RemoteResourceFilter(net.schmizz.sshj.sftp.RemoteResourceFilter) FileAttributes(net.schmizz.sshj.sftp.FileAttributes)

Example 3 with SSHClient

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;
    }
}
Also used : PromiscuousVerifier(net.schmizz.sshj.transport.verification.PromiscuousVerifier) SSHClient(net.schmizz.sshj.SSHClient)

Aggregations

SSHClient (net.schmizz.sshj.SSHClient)3 File (java.io.File)1 StringWriter (java.io.StringWriter)1 Session (net.schmizz.sshj.connection.channel.direct.Session)1 Command (net.schmizz.sshj.connection.channel.direct.Session.Command)1 FileAttributes (net.schmizz.sshj.sftp.FileAttributes)1 RemoteResourceFilter (net.schmizz.sshj.sftp.RemoteResourceFilter)1 RemoteResourceInfo (net.schmizz.sshj.sftp.RemoteResourceInfo)1 SFTPClient (net.schmizz.sshj.sftp.SFTPClient)1 PromiscuousVerifier (net.schmizz.sshj.transport.verification.PromiscuousVerifier)1