Search in sources :

Example 16 with JSchException

use of com.jcraft.jsch.JSchException in project ats-framework by Axway.

the class SftpClient method doConnect.

private void doConnect(String publicKeyAlias) throws FileTransferException {
    // make new SFTP object for every new connection
    disconnect();
    this.jsch = new JSch();
    /* NOTE: Due to logging being global (static), if one thread enables debug mode, all threads will log messages,
         * and if another thread disables it, logging will be stopped for all threads,
         * until at least one thread enables it again
         */
    if (isDebugMode()) {
        JSch.setLogger(new SftpListener());
        debugProgressMonitor = new SftpFileTransferProgressMonitor();
    } else {
        debugProgressMonitor = null;
    }
    try {
        if (username != null) {
            this.session = jsch.getSession(this.username, this.hostname, this.port);
            this.session.setPassword(this.password);
        } else {
            this.jsch.addIdentity(this.keystoreFile, this.publicKeyAlias, this.keystorePassword.getBytes());
            this.session = this.jsch.getSession(this.username, this.hostname, this.port);
        }
        // skip checking of known hosts and verifying RSA keys
        this.session.setConfig("StrictHostKeyChecking", "no");
        // make keyboard-interactive last authentication method
        this.session.setConfig("PreferredAuthentications", "publickey,password,keyboard-interactive");
        this.session.setTimeout(this.timeout);
        if (this.ciphers != null && this.ciphers.size() > 0) {
            StringBuilder ciphers = new StringBuilder();
            for (SshCipher cipher : this.ciphers) {
                ciphers.append(cipher.getSshAlgorithmName() + ",");
            }
            this.session.setConfig("cipher.c2s", ciphers.toString());
            this.session.setConfig("cipher.s2c", ciphers.toString());
            this.session.setConfig("CheckCiphers", ciphers.toString());
        }
        if (this.listener != null) {
            this.listener.setResponses(new ArrayList<String>());
            JSch.setLogger((com.jcraft.jsch.Logger) this.listener);
        }
        /* SFTP reference implementation does not support transfer mode.
               Due to that, JSch does not support it either.
               For now setTransferMode() has no effect. It may be left like that,
               implemented to throw new FileTransferException( "Not implemented" )
               or log warning, that SFTP protocol does not support transfer mode change.
            */
        this.session.connect();
        this.channel = (ChannelSftp) this.session.openChannel("sftp");
        this.channel.connect();
    } catch (JSchException e) {
        throw new FileTransferException("Unable to connect!", e);
    }
}
Also used : JSchException(com.jcraft.jsch.JSchException) FileTransferException(com.axway.ats.common.filetransfer.FileTransferException) SshCipher(com.axway.ats.common.filetransfer.SshCipher) SftpListener(com.axway.ats.core.filetransfer.model.ftp.SftpListener) JSch(com.jcraft.jsch.JSch) SftpFileTransferProgressMonitor(com.axway.ats.core.filetransfer.model.ftp.SftpFileTransferProgressMonitor)

Example 17 with JSchException

use of com.jcraft.jsch.JSchException in project GNS by MobilityFirst.

the class Sudo method main.

/**
   *
   * @param arg
   */
public static void main(String[] arg) {
    try {
        JSch jsch = new JSch();
        Session session = SSHClient.authenticateWithKey(jsch, null, null, null);
        UserInfo ui = new UserInfoPrompted();
        session.setUserInfo(ui);
        session.connect();
        String command = JOptionPane.showInputDialog("Enter command, execed with sudo", "printenv SUDO_USER");
        String sudo_pass = null;
        {
            JTextField passwordField = new JPasswordField(8);
            Object[] ob = { passwordField };
            int result = JOptionPane.showConfirmDialog(null, ob, "Enter password for sudo", JOptionPane.OK_CANCEL_OPTION);
            if (result != JOptionPane.OK_OPTION) {
                System.exit(-1);
            }
            sudo_pass = passwordField.getText();
        }
        Channel channel = session.openChannel("exec");
        // man sudo
        //   -S  The -S (stdin) option causes sudo to read the password from the
        //       standard input instead of the terminal device.
        //   -p  The -p (prompt) option allows you to override the default
        //       password prompt and use a custom one.
        ((ChannelExec) channel).setCommand("sudo -S -p '' " + command);
        InputStream in = channel.getInputStream();
        OutputStream out = channel.getOutputStream();
        ((ChannelExec) channel).setErrStream(System.err);
        ((ChannelExec) channel).setPty(true);
        channel.connect();
        out.write((sudo_pass + "\n").getBytes());
        out.flush();
        byte[] tmp = new byte[1024];
        while (true) {
            while (in.available() > 0) {
                int i = in.read(tmp, 0, 1024);
                if (i < 0) {
                    break;
                }
                System.out.print(new String(tmp, 0, i));
            }
            if (channel.isClosed()) {
                System.out.println("exit-status: " + channel.getExitStatus());
                break;
            }
            try {
                Thread.sleep(1000);
            } catch (Exception ee) {
            }
        }
        channel.disconnect();
        session.disconnect();
    } catch (JSchException | HeadlessException | IOException e) {
        System.out.println(e);
    }
}
Also used : JSchException(com.jcraft.jsch.JSchException) UserInfoPrompted(edu.umass.cs.aws.networktools.UserInfoPrompted) HeadlessException(java.awt.HeadlessException) InputStream(java.io.InputStream) Channel(com.jcraft.jsch.Channel) OutputStream(java.io.OutputStream) UserInfo(com.jcraft.jsch.UserInfo) IOException(java.io.IOException) JSch(com.jcraft.jsch.JSch) JTextField(javax.swing.JTextField) ChannelExec(com.jcraft.jsch.ChannelExec) IOException(java.io.IOException) HeadlessException(java.awt.HeadlessException) JSchException(com.jcraft.jsch.JSchException) JPasswordField(javax.swing.JPasswordField) Session(com.jcraft.jsch.Session)

Example 18 with JSchException

use of com.jcraft.jsch.JSchException in project gerrit by GerritCodeReview.

the class SshDaemon method computeHostKeys.

private List<HostKey> computeHostKeys() {
    if (listen.isEmpty()) {
        return Collections.emptyList();
    }
    final List<PublicKey> keys = myHostKeys();
    final List<HostKey> r = new ArrayList<>();
    for (final PublicKey pub : keys) {
        final Buffer buf = new ByteArrayBuffer();
        buf.putRawPublicKey(pub);
        final byte[] keyBin = buf.getCompactData();
        for (final String addr : advertised) {
            try {
                r.add(new HostKey(addr, keyBin));
            } catch (JSchException e) {
                sshDaemonLog.warn(String.format("Cannot format SSHD host key [%s]: %s", pub.getAlgorithm(), e.getMessage()));
            }
        }
    }
    return Collections.unmodifiableList(r);
}
Also used : ByteArrayBuffer(org.apache.sshd.common.util.buffer.ByteArrayBuffer) Buffer(org.apache.sshd.common.util.buffer.Buffer) JSchException(com.jcraft.jsch.JSchException) HostKey(com.jcraft.jsch.HostKey) PublicKey(java.security.PublicKey) ArrayList(java.util.ArrayList) ByteArrayBuffer(org.apache.sshd.common.util.buffer.ByteArrayBuffer)

Example 19 with JSchException

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

the class SFTPConnectionPool method connect.

public ChannelSftp connect(String host, int port, String user, String password, String keyFile) throws IOException {
    // get connection from pool
    ConnectionInfo info = new ConnectionInfo(host, port, user);
    ChannelSftp channel = getFromPool(info);
    if (channel != null) {
        if (channel.isConnected()) {
            return channel;
        } else {
            channel = null;
            synchronized (this) {
                --liveConnectionCount;
                con2infoMap.remove(channel);
            }
        }
    }
    // create a new connection and add to pool
    JSch jsch = new JSch();
    Session session = null;
    try {
        if (user == null || user.length() == 0) {
            user = System.getProperty("user.name");
        }
        if (password == null) {
            password = "";
        }
        if (keyFile != null && keyFile.length() > 0) {
            jsch.addIdentity(keyFile);
        }
        if (port <= 0) {
            session = jsch.getSession(user, host);
        } else {
            session = jsch.getSession(user, host, port);
        }
        session.setPassword(password);
        java.util.Properties config = new java.util.Properties();
        config.put("StrictHostKeyChecking", "no");
        session.setConfig(config);
        session.connect();
        channel = (ChannelSftp) session.openChannel("sftp");
        channel.connect();
        synchronized (this) {
            con2infoMap.put(channel, info);
            liveConnectionCount++;
        }
        return channel;
    } catch (JSchException e) {
        throw new IOException(StringUtils.stringifyException(e));
    }
}
Also used : JSchException(com.jcraft.jsch.JSchException) ChannelSftp(com.jcraft.jsch.ChannelSftp) IOException(java.io.IOException) JSch(com.jcraft.jsch.JSch) Session(com.jcraft.jsch.Session)

Example 20 with JSchException

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

the class SFTPConnectionPool method disconnect.

void disconnect(ChannelSftp channel) throws IOException {
    if (channel != null) {
        // close connection if too many active connections
        boolean closeConnection = false;
        synchronized (this) {
            if (liveConnectionCount > maxConnection) {
                --liveConnectionCount;
                con2infoMap.remove(channel);
                closeConnection = true;
            }
        }
        if (closeConnection) {
            if (channel.isConnected()) {
                try {
                    Session session = channel.getSession();
                    channel.disconnect();
                    session.disconnect();
                } catch (JSchException e) {
                    throw new IOException(StringUtils.stringifyException(e));
                }
            }
        } else {
            returnToPool(channel);
        }
    }
}
Also used : JSchException(com.jcraft.jsch.JSchException) IOException(java.io.IOException) Session(com.jcraft.jsch.Session)

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