Search in sources :

Example 21 with ChannelExec

use of com.jcraft.jsch.ChannelExec in project airavata by apache.

the class SSHUtils method scpThirdParty.

/**
 * This method will copy a remote file to a local directory
 *
 * @param sourceFile remote file path, this has to be a full qualified path
 * @param sourceSession JSch session for source
 * @param destinationFile This is the local file to copy, this can be a directory too
 * @param destinationSession JSch Session for target
 * @return returns the final local file path of the new file came from the remote resource
 */
public static void scpThirdParty(String sourceFile, Session sourceSession, String destinationFile, Session destinationSession, boolean ignoreEmptyFile) throws IOException, JSchException {
    OutputStream sout = null;
    InputStream sin = null;
    OutputStream dout = null;
    InputStream din = null;
    try {
        String prefix = null;
        // exec 'scp -f sourceFile'
        String sourceCommand = "scp -f " + sourceFile;
        Channel sourceChannel = sourceSession.openChannel("exec");
        ((ChannelExec) sourceChannel).setCommand(sourceCommand);
        StandardOutReader sourceStdOutReader = new StandardOutReader();
        ((ChannelExec) sourceChannel).setErrStream(sourceStdOutReader.getStandardError());
        // get I/O streams for remote scp
        sout = sourceChannel.getOutputStream();
        sin = sourceChannel.getInputStream();
        sourceChannel.connect();
        boolean ptimestamp = true;
        // exec 'scp -t destinationFile'
        String command = "scp " + (ptimestamp ? "-p" : "") + " -t " + destinationFile;
        Channel targetChannel = destinationSession.openChannel("exec");
        StandardOutReader targetStdOutReader = new StandardOutReader();
        ((ChannelExec) targetChannel).setErrStream(targetStdOutReader.getStandardError());
        ((ChannelExec) targetChannel).setCommand(command);
        // get I/O streams for remote scp
        dout = targetChannel.getOutputStream();
        din = targetChannel.getInputStream();
        targetChannel.connect();
        if (checkAck(din) != 0) {
            String error = "Error Reading input Stream";
            log.error(error);
            throw new Exception(error);
        }
        byte[] buf = new byte[1024];
        // send '\0'
        buf[0] = 0;
        sout.write(buf, 0, 1);
        sout.flush();
        log.info("Initiating transfer from:" + sourceFile + " To: " + destinationFile + ", Ignore Empty file : " + ignoreEmptyFile);
        while (true) {
            int c = checkAck(sin);
            if (c != 'C') {
                break;
            }
            // read '0644 '
            sin.read(buf, 0, 5);
            long fileSize = 0L;
            while (true) {
                if (sin.read(buf, 0, 1) < 0) {
                    // error
                    break;
                }
                if (buf[0] == ' ')
                    break;
                fileSize = fileSize * 10L + (long) (buf[0] - '0');
            }
            String fileName = null;
            for (int i = 0; ; i++) {
                sin.read(buf, i, 1);
                if (buf[i] == (byte) 0x0a) {
                    fileName = new String(buf, 0, i);
                    break;
                }
            }
            // FIXME: Remove me after fixing file transfer issue
            if (fileSize == 0L) {
                log.warn("*****Zero byte file*****. Transferring from:" + sourceFile + " To: " + destinationFile + ", File Size : " + fileSize + ", Ignore Empty file : " + ignoreEmptyFile);
            } else {
                log.info("Transferring from:" + sourceFile + " To: " + destinationFile + ", File Size : " + fileSize + ", Ignore Empty file : " + ignoreEmptyFile);
            }
            if (fileSize == 0L && !ignoreEmptyFile) {
                String error = "Input file is empty...";
                log.error(error);
                throw new JSchException(error);
            }
            String initData = "C0644 " + fileSize + " " + fileName + "\n";
            assert dout != null;
            dout.write(initData.getBytes());
            dout.flush();
            // send '\0' to source
            buf[0] = 0;
            sout.write(buf, 0, 1);
            sout.flush();
            int rLength;
            while (true) {
                if (buf.length < fileSize)
                    rLength = buf.length;
                else
                    rLength = (int) fileSize;
                // read content of the source File
                rLength = sin.read(buf, 0, rLength);
                if (rLength < 0) {
                    // error
                    break;
                }
                // write to destination file
                dout.write(buf, 0, rLength);
                fileSize -= rLength;
                if (fileSize == 0L)
                    break;
            }
            // send '\0' to target
            buf[0] = 0;
            dout.write(buf, 0, 1);
            dout.flush();
            if (checkAck(din) != 0) {
                String error = "Error Reading input Stream";
                log.error(error);
                throw new Exception(error);
            }
            dout.close();
            dout = null;
            if (checkAck(sin) != 0) {
                String error = "Error transfering the file content";
                log.error(error);
                throw new Exception(error);
            }
            // send '\0'
            buf[0] = 0;
            sout.write(buf, 0, 1);
            sout.flush();
        }
    } catch (Exception e) {
        log.error(e.getMessage(), e);
        throw new JSchException(e.getMessage());
    } finally {
        try {
            if (dout != null)
                dout.close();
        } catch (Exception ee) {
            log.error("", ee);
        }
        try {
            if (din != null)
                din.close();
        } catch (Exception ee) {
            log.error("", ee);
        }
        try {
            if (sout != null)
                sout.close();
        } catch (Exception ee) {
            log.error("", ee);
        }
        try {
            if (din != null)
                din.close();
        } catch (Exception ee) {
            log.error("", ee);
        }
    }
}
Also used : JSchException(com.jcraft.jsch.JSchException) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) Channel(com.jcraft.jsch.Channel) ChannelExec(com.jcraft.jsch.ChannelExec) GFacException(org.apache.airavata.gfac.core.GFacException) SSHApiException(org.apache.airavata.gfac.core.SSHApiException) IOException(java.io.IOException) JSchException(com.jcraft.jsch.JSchException)

Example 22 with ChannelExec

use of com.jcraft.jsch.ChannelExec in project airavata by apache.

the class SSHUtils method scpFrom.

/**
 * This method will copy a remote file to a local directory
 *
 * @param remoteFile remote file path, this has to be a full qualified path
 * @param localFile  This is the local file to copy, this can be a directory too
 * @return returns the final local file path of the new file came from the remote resource
 */
public static void scpFrom(String remoteFile, String localFile, Session session) throws IOException, JSchException, SSHApiException {
    FileOutputStream fos = null;
    try {
        String prefix = null;
        if (new File(localFile).isDirectory()) {
            prefix = localFile + File.separator;
        }
        // exec 'scp -f remotefile' remotely
        String command = "scp -f " + remoteFile;
        Channel channel = session.openChannel("exec");
        ((ChannelExec) channel).setCommand(command);
        StandardOutReader stdOutReader = new StandardOutReader();
        ((ChannelExec) channel).setErrStream(stdOutReader.getStandardError());
        // get I/O streams for remote scp
        OutputStream out = channel.getOutputStream();
        InputStream in = channel.getInputStream();
        if (!channel.isClosed()) {
            channel.connect();
        }
        byte[] buf = new byte[1024];
        // send '\0'
        buf[0] = 0;
        out.write(buf, 0, 1);
        out.flush();
        while (true) {
            int c = checkAck(in);
            if (c != 'C') {
                break;
            }
            // read '0644 '
            in.read(buf, 0, 5);
            long filesize = 0L;
            while (true) {
                if (in.read(buf, 0, 1) < 0) {
                    // error
                    break;
                }
                if (buf[0] == ' ')
                    break;
                filesize = filesize * 10L + (long) (buf[0] - '0');
            }
            String file = null;
            for (int i = 0; ; i++) {
                in.read(buf, i, 1);
                if (buf[i] == (byte) 0x0a) {
                    file = new String(buf, 0, i);
                    break;
                }
            }
            // System.out.println("filesize="+filesize+", file="+file);
            // send '\0'
            buf[0] = 0;
            out.write(buf, 0, 1);
            out.flush();
            // read a content of lfile
            fos = new FileOutputStream(prefix == null ? localFile : prefix + file);
            int foo;
            while (true) {
                if (buf.length < filesize)
                    foo = buf.length;
                else
                    foo = (int) filesize;
                foo = in.read(buf, 0, foo);
                if (foo < 0) {
                    // error
                    break;
                }
                fos.write(buf, 0, foo);
                filesize -= foo;
                if (filesize == 0L)
                    break;
            }
            fos.close();
            fos = null;
            if (checkAck(in) != 0) {
                String error = "Error transfering the file content";
                log.error(error);
                throw new SSHApiException(error);
            }
            // send '\0'
            buf[0] = 0;
            out.write(buf, 0, 1);
            out.flush();
        }
        stdOutReader.onOutput(channel);
        if (stdOutReader.getStdErrorString().contains("scp:")) {
            throw new SSHApiException(stdOutReader.getStdErrorString());
        }
    } catch (Exception e) {
        log.error(e.getMessage(), e);
    } finally {
        try {
            if (fos != null)
                fos.close();
        } catch (Exception ee) {
        }
    }
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Channel(com.jcraft.jsch.Channel) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) SSHApiException(org.apache.airavata.gfac.core.SSHApiException) ChannelExec(com.jcraft.jsch.ChannelExec) GFacException(org.apache.airavata.gfac.core.GFacException) SSHApiException(org.apache.airavata.gfac.core.SSHApiException) IOException(java.io.IOException) JSchException(com.jcraft.jsch.JSchException) FileOutputStream(java.io.FileOutputStream) File(java.io.File)

Example 23 with ChannelExec

use of com.jcraft.jsch.ChannelExec in project airavata by apache.

the class SSHUtils method scpTo.

/**
 * This will copy a local file to a remote location
 *
 * @param remoteFile remote location you want to transfer the file, this cannot be a directory, if user pass
 *                   a dirctory we do copy it to that directory but we simply return the directory name
 *                   todo handle the directory name as input and return the proper final output file name
 * @param localFile  Local file to transfer, this can be a directory
 * @return returns the final remote file path, so that users can use the new file location
 */
public static String scpTo(String localFile, String remoteFile, Session session) throws IOException, JSchException, SSHApiException {
    FileInputStream fis = null;
    String prefix = null;
    if (new File(localFile).isDirectory()) {
        prefix = localFile + File.separator;
    }
    boolean ptimestamp = true;
    // exec 'scp -t rfile' remotely
    String command = "scp " + (ptimestamp ? "-p" : "") + " -t " + remoteFile;
    Channel channel = session.openChannel("exec");
    StandardOutReader stdOutReader = new StandardOutReader();
    ((ChannelExec) channel).setErrStream(stdOutReader.getStandardError());
    ((ChannelExec) channel).setCommand(command);
    // get I/O streams for remote scp
    OutputStream out = channel.getOutputStream();
    InputStream in = channel.getInputStream();
    channel.connect();
    if (checkAck(in) != 0) {
        String error = "Error Reading input Stream";
        log.error(error);
        throw new SSHApiException(error);
    }
    File _lfile = new File(localFile);
    if (ptimestamp) {
        command = "T" + (_lfile.lastModified() / 1000) + " 0";
        // The access time should be sent here,
        // but it is not accessible with JavaAPI ;-<
        command += (" " + (_lfile.lastModified() / 1000) + " 0\n");
        out.write(command.getBytes());
        out.flush();
        if (checkAck(in) != 0) {
            String error = "Error Reading input Stream";
            log.error(error);
            throw new SSHApiException(error);
        }
    }
    // send "C0644 filesize filename", where filename should not include '/'
    long filesize = _lfile.length();
    command = "C0644 " + filesize + " ";
    if (localFile.lastIndexOf('/') > 0) {
        command += localFile.substring(localFile.lastIndexOf('/') + 1);
    } else {
        command += localFile;
    }
    command += "\n";
    out.write(command.getBytes());
    out.flush();
    if (checkAck(in) != 0) {
        String error = "Error Reading input Stream";
        log.error(error);
        throw new SSHApiException(error);
    }
    // send a content of localFile
    fis = new FileInputStream(localFile);
    byte[] buf = new byte[1024];
    while (true) {
        int len = fis.read(buf, 0, buf.length);
        if (len <= 0)
            break;
        // out.flush();
        out.write(buf, 0, len);
    }
    fis.close();
    fis = null;
    // send '\0'
    buf[0] = 0;
    out.write(buf, 0, 1);
    out.flush();
    if (checkAck(in) != 0) {
        String error = "Error Reading input Stream";
        log.error(error);
        throw new SSHApiException(error);
    }
    out.close();
    stdOutReader.onOutput(channel);
    channel.disconnect();
    if (stdOutReader.getStdErrorString().contains("scp:")) {
        throw new SSHApiException(stdOutReader.getStdErrorString());
    }
    // since remote file is always a file  we just return the file
    return remoteFile;
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Channel(com.jcraft.jsch.Channel) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) SSHApiException(org.apache.airavata.gfac.core.SSHApiException) FileInputStream(java.io.FileInputStream) ChannelExec(com.jcraft.jsch.ChannelExec) File(java.io.File)

Example 24 with ChannelExec

use of com.jcraft.jsch.ChannelExec in project airavata by apache.

the class SSHUtils method makeDirectory.

public static void makeDirectory(String path, Session session) throws IOException, JSchException, GFacException {
    // exec 'scp -t rfile' remotely
    String command = "mkdir -p " + path;
    Channel channel = session.openChannel("exec");
    StandardOutReader stdOutReader = new StandardOutReader();
    ((ChannelExec) channel).setCommand(command);
    ((ChannelExec) channel).setErrStream(stdOutReader.getStandardError());
    try {
        channel.connect();
    } catch (JSchException e) {
        channel.disconnect();
        // session.disconnect();
        log.error("Unable to retrieve command output. Command - " + command + " on server - " + session.getHost() + ":" + session.getPort() + " connecting user name - " + session.getUserName());
        throw e;
    }
    stdOutReader.onOutput(channel);
    if (stdOutReader.getStdErrorString().contains("mkdir:")) {
        throw new GFacException(stdOutReader.getStdErrorString());
    }
    channel.disconnect();
}
Also used : JSchException(com.jcraft.jsch.JSchException) GFacException(org.apache.airavata.gfac.core.GFacException) Channel(com.jcraft.jsch.Channel) ChannelExec(com.jcraft.jsch.ChannelExec)

Example 25 with ChannelExec

use of com.jcraft.jsch.ChannelExec in project cdap by caskdata.

the class DefaultSSHSession method copy.

@Override
public void copy(InputStream input, String targetPath, String targetName, long size, int permission, @Nullable Long lastAccessTime, @Nullable Long lastModifiedTime) throws IOException {
    boolean preserveTimestamp = lastAccessTime != null && lastModifiedTime != null;
    String command = "scp " + (preserveTimestamp ? "-p" : "") + " -t " + targetPath;
    try {
        Channel channel = session.openChannel("exec");
        ((ChannelExec) channel).setCommand(command);
        // otherwise data received from remote server might get dropped, resulting in reading from the input stream hanged
        try (OutputStream out = channel.getOutputStream();
            InputStream in = channel.getInputStream()) {
            channel.connect();
            checkAck(in);
            if (preserveTimestamp) {
                command = String.format("T%d 0 %d 0\n", TimeUnit.MILLISECONDS.toSeconds(lastModifiedTime), TimeUnit.MILLISECONDS.toSeconds(lastAccessTime));
                out.write(command.getBytes(StandardCharsets.UTF_8));
                out.flush();
                checkAck(in);
            }
            // send "C[4 digits permissions] [file_size] filename", where filename should not include '/'
            command = String.format("C%04o %d %s\n", permission, size, targetName);
            out.write(command.getBytes(StandardCharsets.UTF_8));
            out.flush();
            checkAck(in);
            // send a content of file
            ByteStreams.copy(input, out);
            // send '\0'
            out.write(0);
            out.flush();
            checkAck(in);
        } finally {
            channel.disconnect();
        }
    } catch (JSchException e) {
        throw new IOException(e);
    }
}
Also used : JSchException(com.jcraft.jsch.JSchException) InputStream(java.io.InputStream) Channel(com.jcraft.jsch.Channel) OutputStream(java.io.OutputStream) IOException(java.io.IOException) ChannelExec(com.jcraft.jsch.ChannelExec)

Aggregations

ChannelExec (com.jcraft.jsch.ChannelExec)99 InputStream (java.io.InputStream)63 IOException (java.io.IOException)59 JSchException (com.jcraft.jsch.JSchException)50 Channel (com.jcraft.jsch.Channel)48 Session (com.jcraft.jsch.Session)31 JSch (com.jcraft.jsch.JSch)29 FileInputStream (java.io.FileInputStream)26 InputStreamReader (java.io.InputStreamReader)26 BufferedReader (java.io.BufferedReader)23 OutputStream (java.io.OutputStream)22 File (java.io.File)17 Properties (java.util.Properties)16 FileOutputStream (java.io.FileOutputStream)15 ByteArrayOutputStream (java.io.ByteArrayOutputStream)7 SftpException (com.jcraft.jsch.SftpException)6 GFacException (org.apache.airavata.gfac.core.GFacException)6 ArrayList (java.util.ArrayList)5 Charset (java.nio.charset.Charset)4 UserInfo (com.jcraft.jsch.UserInfo)3