Search in sources :

Example 26 with ChannelSftp

use of com.jcraft.jsch.ChannelSftp in project GNS by MobilityFirst.

the class SFTPUpload method localFileNewer.

/**
   *
   * @param user
   * @param host
   * @param keyFile
   * @param fileToTransfer
   * @param sftpWorkingDirectory
   * @return true if the local file is newer
   */
public static boolean localFileNewer(String user, String host, File keyFile, String fileToTransfer, String sftpWorkingDirectory) {
    if (verbose) {
        System.out.println("Local File Newer Check " + fileToTransfer + " to " + host + "@" + user + " " + sftpWorkingDirectory);
    }
    try {
        ChannelSftp channelSftp = authenticateSftp(user, host, keyFile);
        Path paths = Paths.get(fileToTransfer);
        String localDir = paths.getParent().toString();
        channelSftp.cd(sftpWorkingDirectory);
        channelSftp.lcd(localDir);
        SftpATTRS remoteAttributes = channelSftp.stat(paths.getFileName().toString());
        long localTime = new File(fileToTransfer).lastModified();
        long remoteTime = remoteAttributes.getMTime() * 1000L;
        if (verbose) {
            System.out.println("L: " + localDir + " R: " + sftpWorkingDirectory + "\n" + "Local time = " + localTime + " Remote time = " + remoteTime);
        }
        if (verbose) {
            System.out.println("Result " + (localTime > remoteTime));
        }
        return localTime > remoteTime;
    } catch (JSchException | SftpException e) {
        System.out.println("Exception while checking for file newer:" + e);
        return false;
    }
}
Also used : Path(java.nio.file.Path) JSchException(com.jcraft.jsch.JSchException) ChannelSftp(com.jcraft.jsch.ChannelSftp) SftpATTRS(com.jcraft.jsch.SftpATTRS) SftpException(com.jcraft.jsch.SftpException) File(java.io.File)

Example 27 with ChannelSftp

use of com.jcraft.jsch.ChannelSftp in project GNS by MobilityFirst.

the class SFTPUpload method uploadFile.

/**
   *
   * @param user
   * @param host
   * @param keyFile
   * @param fileToTransfer
   * @param sftpWorkingDirectory
   */
public static void uploadFile(String user, String host, File keyFile, String fileToTransfer, String sftpWorkingDirectory) {
    if (verbose) {
        System.out.println("Upload file from " + fileToTransfer + " to " + host + "@" + user + " " + sftpWorkingDirectory);
    }
    try {
        ChannelSftp channelSftp = authenticateSftp(user, host, keyFile);
        File f = new File(fileToTransfer);
        channelSftp.put(new FileInputStream(f), f.getName());
    } catch (JSchException | SftpException | FileNotFoundException e) {
        System.out.println("Exception while uploading file:" + e);
    }
}
Also used : JSchException(com.jcraft.jsch.JSchException) ChannelSftp(com.jcraft.jsch.ChannelSftp) SftpException(com.jcraft.jsch.SftpException) FileNotFoundException(java.io.FileNotFoundException) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 28 with ChannelSftp

use of com.jcraft.jsch.ChannelSftp in project azure-tools-for-java by Microsoft.

the class SparkSubmitHelper method sftpFileToEmulator.

public String sftpFileToEmulator(String localFile, String folderPath, IClusterDetail clusterDetail) throws IOException, HDIException, JSchException, SftpException {
    EmulatorClusterDetail emulatorClusterDetail = (EmulatorClusterDetail) clusterDetail;
    final File file = new File(localFile);
    try (FileInputStream fileInputStream = new FileInputStream(file)) {
        try (BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream)) {
            String sshEndpoint = emulatorClusterDetail.getSSHEndpoint();
            URL url = new URL(sshEndpoint);
            String host = url.getHost();
            int port = url.getPort();
            JSch jsch = new JSch();
            Session session = jsch.getSession(emulatorClusterDetail.getHttpUserName(), host, port);
            session.setPassword(emulatorClusterDetail.getHttpPassword());
            java.util.Properties config = new java.util.Properties();
            config.put("StrictHostKeyChecking", "no");
            session.setConfig(config);
            session.connect();
            ChannelSftp channel = (ChannelSftp) session.openChannel("sftp");
            channel.connect();
            String[] folders = folderPath.split("/");
            for (String folder : folders) {
                if (folder.length() > 0) {
                    try {
                        channel.cd(folder);
                    } catch (SftpException e) {
                        channel.mkdir(folder);
                        channel.cd(folder);
                    }
                }
            }
            channel.put(bufferedInputStream, file.getName());
            channel.disconnect();
            session.disconnect();
            return file.getName();
        }
    }
}
Also used : EmulatorClusterDetail(com.microsoft.azure.hdinsight.sdk.cluster.EmulatorClusterDetail) SftpException(com.jcraft.jsch.SftpException) JSch(com.jcraft.jsch.JSch) FileInputStream(java.io.FileInputStream) URL(java.net.URL) ChannelSftp(com.jcraft.jsch.ChannelSftp) BufferedInputStream(java.io.BufferedInputStream) File(java.io.File) Session(com.jcraft.jsch.Session)

Example 29 with ChannelSftp

use of com.jcraft.jsch.ChannelSftp in project voltdb by VoltDB.

the class ExportOnServerVerifier method checkForMoreExportFiles.

private void checkForMoreExportFiles() throws Exception {
    Comparator<String> comparator = new Comparator<String>() {

        @Override
        public int compare(String f1, String f2) {
            f1 = f1.substring(f1.lastIndexOf('/') + 1);
            f2 = f2.substring(f2.lastIndexOf('/') + 1);
            Matcher m1 = EXPORT_FILENAME_REGEXP.matcher(f1);
            Matcher m2 = EXPORT_FILENAME_REGEXP.matcher(f2);
            if (m1.matches() && !m2.matches())
                return -1;
            else if (m2.matches() && !m1.matches())
                return 1;
            long first_ts = Long.parseLong(m1.group(2));
            long second_ts = Long.parseLong(m2.group(2));
            if (first_ts != second_ts) {
                return (int) (first_ts - second_ts);
            } else {
                long first_txnid = Long.parseLong(m1.group(1));
                long second_txnid = Long.parseLong(m2.group(1));
                if (first_txnid < second_txnid) {
                    return -1;
                } else if (first_txnid > second_txnid) {
                    return 1;
                } else {
                    return 0;
                }
            }
        }
    };
    checkForMoreFilesRemote(comparator);
    for (Pair<ChannelSftp, String> p : m_exportFiles) {
        System.out.println("" + p.getFirst().getSession().getHost() + " : " + p.getSecond());
    }
}
Also used : ChannelSftp(com.jcraft.jsch.ChannelSftp) Matcher(java.util.regex.Matcher) Comparator(java.util.Comparator)

Example 30 with ChannelSftp

use of com.jcraft.jsch.ChannelSftp in project voltdb by VoltDB.

the class ExportOnServerVerifier method openNextExportFile.

Pair<BufferedReader, Runnable> openNextExportFile() throws Exception {
    if (m_exportFiles.isEmpty()) {
        checkForMoreExportFiles();
    }
    Pair<ChannelSftp, String> remotePair = m_exportFiles.poll();
    if (remotePair == null)
        return null;
    final ChannelSftp channel = remotePair.getFirst();
    final String path = remotePair.getSecond();
    System.out.println("INFO export: Opening export file: " + channel.getSession().getHost() + "@" + path);
    final BufferedReader reader = new BufferedReader(new InputStreamReader(channel.get(path)), 4096 * 32);
    Runnable r = new Runnable() {

        @Override
        public void run() {
            try {
                reader.close();
                channel.rm(path);
            } catch (Exception e) {
                Throwables.propagate(e);
            }
        }
    };
    return Pair.of(reader, r);
}
Also used : ChannelSftp(com.jcraft.jsch.ChannelSftp) InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) SftpException(com.jcraft.jsch.SftpException) IOException(java.io.IOException) JSchException(com.jcraft.jsch.JSchException)

Aggregations

ChannelSftp (com.jcraft.jsch.ChannelSftp)42 SftpException (com.jcraft.jsch.SftpException)24 IOException (java.io.IOException)15 JSchException (com.jcraft.jsch.JSchException)12 Session (com.jcraft.jsch.Session)12 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)8 File (java.io.File)7 CoreException (org.eclipse.core.runtime.CoreException)7 IStatus (org.eclipse.core.runtime.IStatus)7 Status (org.eclipse.core.runtime.Status)7 Channel (com.jcraft.jsch.Channel)6 JSch (com.jcraft.jsch.JSch)6 Path (org.apache.hadoop.fs.Path)6 InputStream (java.io.InputStream)5 SftpATTRS (com.jcraft.jsch.SftpATTRS)4 FileInputStream (java.io.FileInputStream)3 FileNotFoundException (java.io.FileNotFoundException)3 LinkedList (java.util.LinkedList)3 LsEntry (com.jcraft.jsch.ChannelSftp.LsEntry)2 OutputStream (java.io.OutputStream)2