Search in sources :

Example 86 with ChannelExec

use of com.jcraft.jsch.ChannelExec in project ant-ivy by apache.

the class Scp method getExecChannel.

/**
 * @return ChannelExec
 * @throws JSchException if something goes wrong
 */
private ChannelExec getExecChannel() throws JSchException {
    ChannelExec channel;
    channel = (ChannelExec) session.openChannel("exec");
    return channel;
}
Also used : ChannelExec(com.jcraft.jsch.ChannelExec)

Example 87 with ChannelExec

use of com.jcraft.jsch.ChannelExec in project ant-ivy by apache.

the class SshRepository method checkExistence.

/**
 * check for existence of file or dir on target system
 *
 * @param filePath
 *            to the object to check
 * @param session
 *            to use
 * @return true: object exists, false otherwise
 */
private boolean checkExistence(String filePath, Session session) throws IOException {
    Message.debug("SShRepository: checkExistence called: " + filePath);
    ChannelExec channel = null;
    channel = getExecChannel(session);
    String fullCmd = replaceArgument(existCommand, filePath);
    channel.setCommand(fullCmd);
    StringBuilder stdOut = new StringBuilder();
    StringBuilder stdErr = new StringBuilder();
    readSessionOutput(channel, stdOut, stdErr);
    return channel.getExitStatus() == 0;
}
Also used : ChannelExec(com.jcraft.jsch.ChannelExec)

Example 88 with ChannelExec

use of com.jcraft.jsch.ChannelExec in project ant-ivy by apache.

the class SshRepository method list.

/*
     * (non-Javadoc)
     *
     * @see org.apache.ivy.repository.Repository#list(java.lang.String)
     */
public List<String> list(String parent) throws IOException {
    Message.debug("SShRepository:list called: " + parent);
    List<String> result = new ArrayList<>();
    Session session = null;
    ChannelExec channel = null;
    session = getSession(parent);
    channel = getExecChannel(session);
    URI parentUri = null;
    try {
        parentUri = new URI(parent);
    } catch (URISyntaxException e) {
        throw new IOException("The uri '" + parent + "' is not valid!", e);
    }
    String fullCmd = replaceArgument(listCommand, parentUri.getPath());
    channel.setCommand(fullCmd);
    StringBuilder stdOut = new StringBuilder();
    StringBuilder stdErr = new StringBuilder();
    readSessionOutput(channel, stdOut, stdErr);
    if (channel.getExitStatus() != 0) {
        Message.error("Ssh ListCommand exited with status != 0");
        Message.error(stdErr.toString());
        return null;
    } else {
        BufferedReader br = new BufferedReader(new StringReader(stdOut.toString()));
        String line = null;
        while ((line = br.readLine()) != null) {
            result.add(line);
        }
    }
    return result;
}
Also used : ArrayList(java.util.ArrayList) BufferedReader(java.io.BufferedReader) StringReader(java.io.StringReader) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) URI(java.net.URI) ChannelExec(com.jcraft.jsch.ChannelExec) Session(com.jcraft.jsch.Session)

Example 89 with ChannelExec

use of com.jcraft.jsch.ChannelExec in project ant-ivy by apache.

the class SshRepository method makePath.

/**
 * Tries to create a directory path on the target system
 *
 * @param path
 *            to create
 * @param session
 *            to use
 */
private void makePath(String path, Session session) throws IOException {
    ChannelExec channel = null;
    String trimmed = path;
    try {
        while (trimmed.length() > 0 && trimmed.charAt(trimmed.length() - 1) == fileSeparator) {
            trimmed = trimmed.substring(0, trimmed.length() - 1);
        }
        if (trimmed.length() == 0 || checkExistence(trimmed, session)) {
            return;
        }
        int nextSlash = trimmed.lastIndexOf(fileSeparator);
        if (nextSlash > 0) {
            String parent = trimmed.substring(0, nextSlash);
            makePath(parent, session);
        }
        channel = getExecChannel(session);
        String mkdir = replaceArgument(createDirCommand, trimmed);
        Message.debug("SShRepository: trying to create path: " + mkdir);
        channel.setCommand(mkdir);
        StringBuilder stdOut = new StringBuilder();
        StringBuilder stdErr = new StringBuilder();
        readSessionOutput(channel, stdOut, stdErr);
    } finally {
        if (channel != null) {
            channel.disconnect();
        }
    }
}
Also used : ChannelExec(com.jcraft.jsch.ChannelExec) TimeoutConstraint(org.apache.ivy.core.settings.TimeoutConstraint)

Example 90 with ChannelExec

use of com.jcraft.jsch.ChannelExec in project nimbus by nimbus-org.

the class SCPClientImpl method mget.

public File[] mget(String remote, String localDir) throws SCPException {
    if (jsch == null) {
        throw new SCPException(scpClientFactoryServiceName, "Connection is not established!");
    }
    if (session == null) {
        throw new SCPException(scpClientFactoryServiceName, "It is not authenticated!");
    }
    if (remote == null || remote.length() == 0) {
        throw new SCPException(scpClientFactoryServiceName, "Path is null.");
    }
    if (localDir == null) {
        localDir = ".";
    }
    File localDirFile = new File(localDir);
    if (homeDir != null && !localDirFile.isAbsolute()) {
        localDirFile = new File(homeDir, localDir);
    }
    List<File> localFiles = new ArrayList<File>();
    Channel channel = null;
    final String cmd = "scp -f " + remote;
    File localFile = null;
    try {
        channel = session.openChannel("exec");
        ((ChannelExec) channel).setCommand(cmd);
        OutputStream os = channel.getOutputStream();
        InputStream is = channel.getInputStream();
        channel.connect();
        byte[] buf = new byte[1024];
        os.write(0);
        os.flush();
        while (true) {
            int c = checkAck(is);
            if (c == -1) {
                if (localFiles.size() == 0) {
                    throw new IOException("Remote SCP terminated unexpectedly.");
                }
                break;
            }
            if (c != 'C') {
                break;
            }
            is.read(buf, 0, 5);
            long fileSize = 0L;
            while (true) {
                if (is.read(buf, 0, 1) < 0) {
                    throw new SCPException(scpClientFactoryServiceName, "Unexpected EOF.");
                }
                if (buf[0] == ' ') {
                    break;
                }
                fileSize = fileSize * 10L + (long) (buf[0] - '0');
            }
            String fileName = null;
            for (int i = 0; ; i++) {
                is.read(buf, i, 1);
                if (buf[i] == (byte) 0x0a) {
                    fileName = new String(buf, 0, i);
                    break;
                }
            }
            buf[0] = 0;
            os.write(buf, 0, 1);
            os.flush();
            localFile = new File(localDirFile, fileName);
            localFiles.add(localFile);
            FileOutputStream fos = new FileOutputStream(localFile);
            try {
                int readLen = 0;
                while (true) {
                    if (buf.length < fileSize) {
                        readLen = buf.length;
                    } else {
                        readLen = (int) fileSize;
                    }
                    readLen = is.read(buf, 0, readLen);
                    if (readLen < 0) {
                        throw new SCPException(scpClientFactoryServiceName, "Unexpected EOF.");
                    }
                    fos.write(buf, 0, readLen);
                    fileSize -= readLen;
                    if (fileSize == 0L) {
                        break;
                    }
                }
            } finally {
                fos.close();
                fos = null;
            }
            checkAck(is);
            localFile = null;
            buf[0] = 0;
            os.write(buf, 0, 1);
            os.flush();
        }
    } catch (IOException e) {
        throw new SCPException(scpClientFactoryServiceName, "It failed to mget! from=" + remote + ", to=" + localFile, e);
    } catch (JSchException e) {
        throw new SCPException(scpClientFactoryServiceName, "It failed to mget! from=" + remote + ", to=" + localFile, e);
    } finally {
        if (channel != null) {
            channel.disconnect();
        }
    }
    return localFiles.toArray(new File[localFiles.size()]);
}
Also used : JSchException(com.jcraft.jsch.JSchException) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) SCPException(jp.ossc.nimbus.service.scp.SCPException) Channel(com.jcraft.jsch.Channel) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) ArrayList(java.util.ArrayList) IOException(java.io.IOException) ChannelExec(com.jcraft.jsch.ChannelExec) FileOutputStream(java.io.FileOutputStream) File(java.io.File) RecurciveSearchFile(jp.ossc.nimbus.io.RecurciveSearchFile)

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