Search in sources :

Example 46 with Channel

use of com.jcraft.jsch.Channel in project linuxtools by eclipse.

the class ScpClient method transfer.

public void transfer(String fromFile, String toFile) throws IOException, JSchException {
    String rfile = toFile;
    String lfile = fromFile;
    // $NON-NLS-1$
    String command = "scp -t " + rfile;
    // $NON-NLS-1$
    Channel channel = session.openChannel("exec");
    ((ChannelExec) channel).setCommand(command);
    // get I/O streams for remote scp
    try (OutputStream out = channel.getOutputStream();
        InputStream in = channel.getInputStream()) {
        channel.connect();
        if (checkAck(in) != 0) {
            // $NON-NLS-1$
            System.out.println("err");
        }
        // send "C0644 filesize filename", where filename should not include
        // '/'
        long filesize = (new File(lfile)).length();
        // $NON-NLS-1$ //$NON-NLS-2$
        command = "C0644 " + filesize + " ";
        if (lfile.lastIndexOf('/') > 0) {
            command += lfile.substring(lfile.lastIndexOf('/') + 1);
        } else {
            command += lfile;
        }
        // $NON-NLS-1$
        command += "\n";
        out.write(command.getBytes());
        out.flush();
        if (checkAck(in) != 0) {
            // $NON-NLS-1$
            System.out.println("err");
        }
        // send a content of lfile
        byte[] buf = new byte[1024];
        try (FileInputStream fis = new FileInputStream(lfile)) {
            while (true) {
                int len = fis.read(buf, 0, buf.length);
                if (len <= 0) {
                    break;
                }
                out.write(buf, 0, len);
            }
        }
        // send '\0'
        buf[0] = 0;
        out.write(buf, 0, 1);
        out.flush();
        if (checkAck(in) != 0) {
            // $NON-NLS-1$
            System.out.println("err");
        }
    }
    channel.disconnect();
    session.disconnect();
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Channel(com.jcraft.jsch.Channel) OutputStream(java.io.OutputStream) File(java.io.File) ChannelExec(com.jcraft.jsch.ChannelExec) FileInputStream(java.io.FileInputStream)

Example 47 with Channel

use of com.jcraft.jsch.Channel in project OsmAnd-tools by osmandapp.

the class IndexUploader method uploadToSSH.

public void uploadToSSH(File f, String description, String size, String date, UploadSSHCredentials cred) throws IOException, JSchException {
    log.info("Uploading file " + f.getName() + " " + size + " MB " + date + " of " + description);
    // Upload to ftp
    JSch jSch = new JSch();
    boolean knownHosts = false;
    if (cred.knownHosts != null) {
        jSch.setKnownHosts(cred.knownHosts);
        knownHosts = true;
    }
    if (cred.privateKey != null) {
        jSch.addIdentity(cred.privateKey);
    }
    String serverName = cred.url;
    if (serverName.startsWith("ssh://")) {
        serverName = serverName.substring("ssh://".length());
    }
    Session session = jSch.getSession(cred.user, serverName);
    if (cred.password != null) {
        session.setPassword(cred.password);
    }
    if (!knownHosts) {
        java.util.Properties config = new java.util.Properties();
        config.put("StrictHostKeyChecking", "no");
        session.setConfig(config);
    }
    String rfile = cred.path + "/" + f.getName();
    String lfile = f.getAbsolutePath();
    session.connect();
    // exec 'scp -t rfile' remotely
    String command = "scp -p -t \"" + rfile + "\"";
    Channel channel = session.openChannel("exec");
    ((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) {
        channel.disconnect();
        session.disconnect();
        return;
    }
    // send "C0644 filesize filename", where filename should not include '/'
    long filesize = (new File(lfile)).length();
    command = "C0644 " + filesize + " ";
    if (lfile.lastIndexOf('/') > 0) {
        command += lfile.substring(lfile.lastIndexOf('/') + 1);
    } else {
        command += lfile;
    }
    command += "\n";
    out.write(command.getBytes());
    out.flush();
    if (checkAck(in) != 0) {
        channel.disconnect();
        session.disconnect();
        return;
    }
    // send a content of lfile
    FileInputStream fis = new FileInputStream(lfile);
    byte[] buf = new byte[1024];
    try {
        int len;
        while ((len = fis.read(buf, 0, buf.length)) > 0) {
            // out.flush();
            out.write(buf, 0, len);
        }
    } finally {
        fis.close();
    }
    fis = null;
    // send '\0'
    buf[0] = 0;
    out.write(buf, 0, 1);
    out.flush();
    if (checkAck(in) != 0) {
        channel.disconnect();
        session.disconnect();
        return;
    }
    out.close();
    channel.disconnect();
    session.disconnect();
    log.info("Finish uploading file index");
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Channel(com.jcraft.jsch.Channel) ZipOutputStream(java.util.zip.ZipOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) CodedOutputStream(com.google.protobuf.CodedOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) JSch(com.jcraft.jsch.JSch) ChannelExec(com.jcraft.jsch.ChannelExec) FileInputStream(java.io.FileInputStream) RandomAccessFile(java.io.RandomAccessFile) ZipFile(java.util.zip.ZipFile) File(java.io.File) Session(com.jcraft.jsch.Session)

Example 48 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;
    } 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 49 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;
    } 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 50 with Channel

use of com.jcraft.jsch.Channel in project CommandHelper by EngineHub.

the class SSHWrapper method SCPFrom.

private static void SCPFrom(String remote, File local, Session session) throws IOException, JSchException {
    // exec 'scp -f rfile' remotely
    String command = "scp -f " + remote;
    Channel channel = session.openChannel("exec");
    ((ChannelExec) channel).setCommand(command);
    // get I/O streams for remote scp
    OutputStream out = channel.getOutputStream();
    InputStream in = channel.getInputStream();
    channel.connect();
    byte[] buf = new byte[1024];
    // send '\0'
    buf[0] = 0;
    out.write(buf, 0, 1);
    out.flush();
    while (true) {
        int c = checkAckFrom(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;
            }
        }
        // send '\0'
        buf[0] = 0;
        out.write(buf, 0, 1);
        out.flush();
        String prefix = null;
        if (local.isDirectory()) {
            prefix = local.getPath() + File.separator;
        }
        // read a content of lfile
        FileOutputStream fos = new FileOutputStream(prefix == null ? local.getPath() : 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;
        checkAckFrom(in);
        // send '\0'
        buf[0] = 0;
        out.write(buf, 0, 1);
        out.flush();
    }
    channel.disconnect();
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Channel(com.jcraft.jsch.Channel) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) ChannelExec(com.jcraft.jsch.ChannelExec)

Aggregations

Channel (com.jcraft.jsch.Channel)63 InputStream (java.io.InputStream)38 ChannelExec (com.jcraft.jsch.ChannelExec)33 JSch (com.jcraft.jsch.JSch)33 IOException (java.io.IOException)32 JSchException (com.jcraft.jsch.JSchException)30 Session (com.jcraft.jsch.Session)28 FileInputStream (java.io.FileInputStream)20 ChannelSftp (com.jcraft.jsch.ChannelSftp)18 OutputStream (java.io.OutputStream)18 BufferedReader (java.io.BufferedReader)15 File (java.io.File)14 InputStreamReader (java.io.InputStreamReader)13 Properties (java.util.Properties)13 SftpException (com.jcraft.jsch.SftpException)12 FileOutputStream (java.io.FileOutputStream)11 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