Search in sources :

Example 46 with JSchException

use of com.jcraft.jsch.JSchException in project bamboobsc by billchen198318.

the class SFtpClientUtils method getRemoteFileList.

@SuppressWarnings("unchecked")
public static Vector<LsEntry> getRemoteFileList(String user, String password, String addr, int port, String cwd) throws JSchException, SftpException, Exception {
    Session session = getSession(user, password, addr, port);
    Vector<LsEntry> lsVec = null;
    Channel channel = session.openChannel("sftp");
    channel.connect();
    ChannelSftp sftpChannel = (ChannelSftp) channel;
    try {
        //sftpChannel.lpwd()
        lsVec = (Vector<LsEntry>) sftpChannel.ls(cwd);
    } catch (Exception e) {
        e.printStackTrace();
        throw e;
    } finally {
        sftpChannel.exit();
        channel.disconnect();
        session.disconnect();
    }
    return lsVec;
}
Also used : ChannelSftp(com.jcraft.jsch.ChannelSftp) Channel(com.jcraft.jsch.Channel) LsEntry(com.jcraft.jsch.ChannelSftp.LsEntry) SftpException(com.jcraft.jsch.SftpException) JSchException(com.jcraft.jsch.JSchException) Session(com.jcraft.jsch.Session)

Example 47 with JSchException

use of com.jcraft.jsch.JSchException in project Lucee by lucee.

the class SFTPClientImpl method connect.

@Override
public void connect() throws SocketException, IOException {
    try {
        session = jsch.getSession(username, host.getHostAddress(), port);
        java.util.Properties config = new java.util.Properties();
        config.put("StrictHostKeyChecking", "no");
        session.setConfig(config);
        UserInfo ui = new UserInfoImpl(password, null);
        session.setUserInfo(ui);
        if (timeout > 0)
            session.setTimeout(timeout);
        session.connect();
        Channel channel = session.openChannel("sftp");
        channel.connect();
        channelSftp = (ChannelSftp) channel;
        // check fingerprint
        if (!StringUtil.isEmpty(fingerprint)) {
            if (!fingerprint.equalsIgnoreCase(fingerprint())) {
                disconnect();
                throw new IOException("given fingerprint is not a match.");
            }
        }
        handleSucess();
    } catch (JSchException e) {
        handleFail(e, stopOnError);
    }
}
Also used : JSchException(com.jcraft.jsch.JSchException) Channel(com.jcraft.jsch.Channel) UserInfo(com.jcraft.jsch.UserInfo) IOException(java.io.IOException)

Example 48 with JSchException

use of com.jcraft.jsch.JSchException in project n2a by frothga.

the class Connection method receive.

public static Result receive(String remote, File dst) throws JSchException {
    connect();
    Result result = new Result();
    FileOutputStream fos = null;
    OutputStream out = null;
    Channel channel = null;
    try {
        // exec 'scp -f rfile' remotely
        String command = "scp -f '" + remote + "'";
        channel = session.openChannel("exec");
        ((ChannelExec) channel).setCommand(command);
        // get I/O streams for remote scp
        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 = 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 + (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
            File outputFile;
            if (dst.isDirectory()) {
                outputFile = new File(dst, file);
            } else {
                outputFile = dst;
            }
            fos = new FileOutputStream(outputFile);
            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;
                }
            }
            if (checkAck(in) != 0) {
                System.exit(0);
            }
            // send '\0'
            buf[0] = 0;
            out.write(buf, 0, 1);
            out.flush();
        }
    } catch (Exception e) {
        result.error = true;
    } finally {
        if (fos != null) {
            try {
                fos.close();
            } catch (Exception e) {
            }
        }
        if (out != null) {
            try {
                out.close();
            } catch (Exception e) {
            }
        }
        if (channel != null) {
            try {
                channel.disconnect();
            } catch (Exception e) {
            }
        }
    }
    return result;
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileOutputStream(java.io.FileOutputStream) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) FileOutputStream(java.io.FileOutputStream) Channel(com.jcraft.jsch.Channel) File(java.io.File) ChannelExec(com.jcraft.jsch.ChannelExec) IOException(java.io.IOException) JSchException(com.jcraft.jsch.JSchException)

Example 49 with JSchException

use of com.jcraft.jsch.JSchException in project tigervnc by TigerVNC.

the class GSSContextKrb5 method create.

public void create(String user, String host) throws JSchException {
    try {
        // RFC 1964
        Oid krb5 = new Oid("1.2.840.113554.1.2.2");
        // Kerberos Principal Name Form
        Oid principalName = new Oid("1.2.840.113554.1.2.2.1");
        GSSManager mgr = GSSManager.getInstance();
        GSSCredential crd = null;
        /*
      try{
        GSSName _user=mgr.createName(user, principalName);
        crd=mgr.createCredential(_user,
                                 GSSCredential.DEFAULT_LIFETIME,
                                 krb5,
                                 GSSCredential.INITIATE_ONLY);
      }
      catch(GSSException crdex){
      }
      */
        String cname = host;
        try {
            cname = InetAddress.getByName(cname).getCanonicalHostName();
        } catch (UnknownHostException e) {
        }
        GSSName _host = mgr.createName("host/" + cname, principalName);
        context = mgr.createContext(_host, krb5, crd, GSSContext.DEFAULT_LIFETIME);
        // RFC4462  3.4.  GSS-API Session
        // 
        // When calling GSS_Init_sec_context(), the client MUST set
        // integ_req_flag to "true" to request that per-message integrity
        // protection be supported for this context.  In addition,
        // deleg_req_flag MAY be set to "true" to request access delegation, if
        // requested by the user.
        // 
        // Since the user authentication process by its nature authenticates
        // only the client, the setting of mutual_req_flag is not needed for
        // this process.  This flag SHOULD be set to "false".
        // TODO: OpenSSH's sshd does accepts 'false' for mutual_req_flag
        // context.requestMutualAuth(false);
        context.requestMutualAuth(true);
        context.requestConf(true);
        // for MIC
        context.requestInteg(true);
        context.requestCredDeleg(true);
        context.requestAnonymity(false);
        return;
    } catch (GSSException ex) {
        throw new JSchException(ex.toString());
    }
}
Also used : JSchException(com.jcraft.jsch.JSchException) GSSName(org.ietf.jgss.GSSName) UnknownHostException(java.net.UnknownHostException) GSSException(org.ietf.jgss.GSSException) GSSCredential(org.ietf.jgss.GSSCredential) GSSManager(org.ietf.jgss.GSSManager) Oid(org.ietf.jgss.Oid)

Example 50 with JSchException

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

the class ScpExec method init.

@Override
protected IStatus init() {
    try {
        channel = LinuxtoolsProcessFactory.execRemote(cmd, System.out, System.err, remoteOptions.userName, remoteOptions.hostName, remoteOptions.password, remoteOptions.port, envVars);
        errorGobbler = new StreamGobbler(channel.getExtInputStream());
        inputGobbler = new StreamGobbler(channel.getInputStream());
        transferListeners();
        return Status.OK_STATUS;
    } catch (final JSchException | IOException e) {
        return new Status(IStatus.ERROR, ConsoleLogPlugin.PLUGIN_ID, Messages.ScpExec_FileTransferFailed, e);
    }
}
Also used : JSchException(com.jcraft.jsch.JSchException) IStatus(org.eclipse.core.runtime.IStatus) Status(org.eclipse.core.runtime.Status) StreamGobbler(org.eclipse.linuxtools.systemtap.structures.runnable.StreamGobbler) IOException(java.io.IOException)

Aggregations

JSchException (com.jcraft.jsch.JSchException)52 IOException (java.io.IOException)25 Session (com.jcraft.jsch.Session)20 JSch (com.jcraft.jsch.JSch)15 Channel (com.jcraft.jsch.Channel)11 ChannelSftp (com.jcraft.jsch.ChannelSftp)11 ChannelExec (com.jcraft.jsch.ChannelExec)9 SftpException (com.jcraft.jsch.SftpException)9 InputStream (java.io.InputStream)7 ArrayList (java.util.ArrayList)7 UserInfo (com.jcraft.jsch.UserInfo)6 File (java.io.File)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 OutputStream (java.io.OutputStream)5 Properties (java.util.Properties)5 MachineException (org.eclipse.che.api.machine.server.exception.MachineException)5 SSHShell (com.microsoft.azure.management.samples.SSHShell)4 FileInputStream (java.io.FileInputStream)4 IStatus (org.eclipse.core.runtime.IStatus)4 Status (org.eclipse.core.runtime.Status)4