Search in sources :

Example 16 with Channel

use of com.jcraft.jsch.Channel 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 17 with Channel

use of com.jcraft.jsch.Channel 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 18 with Channel

use of com.jcraft.jsch.Channel in project javautils by jiadongpo.

the class SFTPClient method login.

public void login(String password) throws Exception {
    this.password = password;
    s.setPassword(this.getPassword());
    try {
        java.util.Properties config = new java.util.Properties();
        config.put("StrictHostKeyChecking", "no");
        // set compression property
        // zlib, none
        String compress = getCompression();
        if (compress != null) {
            config.put(COMPRESSION_S2C, compress);
            config.put(COMPRESSION_C2S, compress);
        }
        s.setConfig(config);
        s.connect();
        Channel channel = s.openChannel("sftp");
        channel.connect();
        c = (ChannelSftp) channel;
    // 设置一个编辑,控制中文乱码问题
    // c.setFilenameEncoding("GBK");
    } catch (JSchException e) {
        throw new Exception(e);
    }
}
Also used : JSchException(com.jcraft.jsch.JSchException) Channel(com.jcraft.jsch.Channel) FileSystemException(org.apache.commons.vfs2.FileSystemException) IOException(java.io.IOException) SftpException(com.jcraft.jsch.SftpException) JSchException(com.jcraft.jsch.JSchException)

Example 19 with Channel

use of com.jcraft.jsch.Channel in project keepass2android by PhilippC.

the class SftpStorage method init.

ChannelSftp init(String filename) throws JSchException, UnsupportedEncodingException {
    jsch = new JSch();
    ConnectionInfo ci = splitStringToConnectionInfo(filename);
    Session session = jsch.getSession(ci.username, ci.host, ci.port);
    UserInfo ui = new SftpUserInfo(ci.password);
    session.setUserInfo(ui);
    session.connect();
    Channel channel = session.openChannel("sftp");
    channel.connect();
    ChannelSftp c = (ChannelSftp) channel;
    logDebug("success: init Sftp");
    return c;
}
Also used : ChannelSftp(com.jcraft.jsch.ChannelSftp) Channel(com.jcraft.jsch.Channel) UserInfo(com.jcraft.jsch.UserInfo) JSch(com.jcraft.jsch.JSch) Session(com.jcraft.jsch.Session)

Example 20 with Channel

use of com.jcraft.jsch.Channel in project ant by apache.

the class ScpFromMessage method execute.

/**
 * Carry out the transfer.
 * @throws IOException on i/o errors
 * @throws JSchException on errors detected by scp
 */
@Override
public void execute() throws IOException, JSchException {
    String command = "scp -f ";
    if (isRecursive) {
        command += "-r ";
    }
    if (getCompressed()) {
        command += "-C ";
    }
    command += remoteFile;
    final Channel channel = openExecChannel(command);
    try {
        // get I/O streams for remote scp
        final OutputStream out = channel.getOutputStream();
        final InputStream in = channel.getInputStream();
        channel.connect();
        sendAck(out);
        startRemoteCpProtocol(in, out, localFile);
    } finally {
        if (channel != null) {
            channel.disconnect();
        }
    }
    log("done\n");
}
Also used : InputStream(java.io.InputStream) Channel(com.jcraft.jsch.Channel) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Aggregations

Channel (com.jcraft.jsch.Channel)63 InputStream (java.io.InputStream)38 ChannelExec (com.jcraft.jsch.ChannelExec)33 IOException (java.io.IOException)33 JSch (com.jcraft.jsch.JSch)32 JSchException (com.jcraft.jsch.JSchException)30 Session (com.jcraft.jsch.Session)27 FileInputStream (java.io.FileInputStream)19 ChannelSftp (com.jcraft.jsch.ChannelSftp)18 OutputStream (java.io.OutputStream)17 BufferedReader (java.io.BufferedReader)15 File (java.io.File)13 InputStreamReader (java.io.InputStreamReader)13 Properties (java.util.Properties)13 SftpException (com.jcraft.jsch.SftpException)12 FileOutputStream (java.io.FileOutputStream)10 UserInfo (com.jcraft.jsch.UserInfo)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 GFacException (org.apache.airavata.gfac.core.GFacException)4 SSHApiException (org.apache.airavata.gfac.core.SSHApiException)3