Search in sources :

Example 46 with JSch

use of com.jcraft.jsch.JSch in project jcabi-github by jcabi.

the class RtDeployKeysITCase method key.

/**
 * Generates a random public key for test.
 *
 * @return The encoded SSH public key.
 * @throws Exception If a problem occurs.
 */
private static String key() throws Exception {
    final ByteArrayOutputStream stream = new ByteArrayOutputStream();
    try {
        final KeyPair kpair = KeyPair.genKeyPair(new JSch(), KeyPair.DSA);
        kpair.writePublicKey(stream, "");
        kpair.dispose();
    } finally {
        stream.close();
    }
    return new String(stream.toByteArray());
}
Also used : KeyPair(com.jcraft.jsch.KeyPair) ByteArrayOutputStream(java.io.ByteArrayOutputStream) JSch(com.jcraft.jsch.JSch)

Example 47 with JSch

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

the class SFTPClientImpl method connect.

public void connect(String user, String host, int port, String password) throws SFTPException {
    if (jsch != null) {
        throw new SFTPException("It is already connected!");
    }
    jsch = new JSch();
    try {
        session = jsch.getSession(user, host, port);
        if (configProperties != null) {
            session.setConfig(configProperties);
        }
        if (proxy != null) {
            session.setProxy(proxy);
        }
        if (timeout >= 0) {
            session.setTimeout(timeout);
        }
        if (serverAliveInterval >= 0) {
            session.setServerAliveInterval(serverAliveInterval);
        }
        if (serverAliveCountMax >= 0) {
            session.setServerAliveCountMax(serverAliveCountMax);
        }
        if (password != null) {
            session.setPassword(password);
        }
        session.connect();
        channel = (ChannelSftp) session.openChannel("sftp");
        if (fileNameEncoding != null) {
            channel.setFilenameEncoding(fileNameEncoding);
        }
        channel.connect();
        if (homeDir != null) {
            channel.lcd(homeDir.getPath());
        }
    } catch (JSchException e) {
        if (channel != null) {
            channel.disconnect();
            channel = null;
        }
        if (session != null) {
            session.disconnect();
            session = null;
        }
        jsch = null;
        throw new SFTPException("It failed to connect!", e);
    } catch (SftpException e) {
        if (channel != null) {
            channel.disconnect();
            channel = null;
        }
        if (session != null) {
            session.disconnect();
            session = null;
        }
        jsch = null;
        throw new SFTPException("It failed to connect!", e);
    }
}
Also used : JSchException(com.jcraft.jsch.JSchException) SftpException(com.jcraft.jsch.SftpException) JSch(com.jcraft.jsch.JSch) SFTPException(jp.ossc.nimbus.service.sftp.SFTPException)

Example 48 with JSch

use of com.jcraft.jsch.JSch in project syndesis by syndesisio.

the class SftpVerifierExtension method verifyCredentials.

private void verifyCredentials(ResultBuilder builder, Map<String, Object> parameters) {
    final String host = (String) parameters.get("host");
    final int port = Integer.parseInt((String) parameters.get("port"));
    final String userName = (String) parameters.get("username");
    final String password = (parameters.get("password") == null) ? "" : (String) parameters.get("password");
    JSch jsch = new JSch();
    Session session = null;
    try {
        session = jsch.getSession(userName, host, port);
        session.setConfig("StrictHostKeyChecking", "no");
        session.setPassword(password);
        session.connect();
    } catch (JSchException e) {
        builder.error(ResultErrorBuilder.withCodeAndDescription(VerificationError.StandardCode.AUTHENTICATION, e.getMessage()).build());
    } finally {
        if (session != null) {
            session.disconnect();
            jsch = null;
        }
    }
}
Also used : JSchException(com.jcraft.jsch.JSchException) JSch(com.jcraft.jsch.JSch) Session(com.jcraft.jsch.Session)

Example 49 with JSch

use of com.jcraft.jsch.JSch in project AJSC by att.

the class GenerateKeys method generateKeyPair.

public static void generateKeyPair(com.att.cdp.zones.model.KeyPair kp) throws IOException, JSchException, ZoneException {
    KeyPair kpair;
    kpair = KeyPair.genKeyPair(new JSch(), KeyPair.RSA);
    // String publicKeyFilename = PUBLIC_KEY;
    // String privateKeyFilename = PRIVATE_KEY;
    OutputStream os = new ByteArrayOutputStream();
    kpair.writePrivateKey(os);
    String str = "";
    try {
        str = reformatSSHKey(os.toString());
    } catch (Exception e) {
        LOG.error(e.getMessage());
    }
    kp.setPrivateKey(str);
    os = new ByteArrayOutputStream();
    kpair.writePublicKey(os, "");
    kp.setPublicKey(os.toString());
    kpair.getFingerPrint();
    kpair.dispose();
}
Also used : KeyPair(com.jcraft.jsch.KeyPair) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) JSch(com.jcraft.jsch.JSch) ZoneException(com.att.cdp.exceptions.ZoneException) IOException(java.io.IOException) JSchException(com.jcraft.jsch.JSchException)

Example 50 with JSch

use of com.jcraft.jsch.JSch in project onebusaway-application-modules by camsys.

the class GtfsRealtimeSource method readFeedFromUrl.

/**
 * This method was added to allow accessing an SFTP feed, since the Java URL
 * class does not yet support the SFTP protocol.
 *
 * @param url of the SFTP feed to read
 * @return a {@link FeedMessage} constructed from the protocol buffer content
 *         of the specified url
 * @throws IOException
 */
private FeedMessage readFeedFromUrl(String url) throws IOException {
    Session session = null;
    Channel channel = null;
    ChannelSftp downloadChannelSftp = null;
    InputStream in = null;
    JSch jsch = new JSch();
    // Parse SFTP URL
    int idx = url.indexOf("//") + 2;
    int idx2 = url.indexOf(":", idx);
    String user = url.substring(idx, idx2);
    idx = idx2 + 1;
    idx2 = url.indexOf("@");
    String pw = url.substring(idx, idx2);
    url = url.substring(idx2 + 1);
    idx = url.indexOf(":");
    String host = url.substring(0, idx);
    String rdir = "";
    idx = url.indexOf("/") + 1;
    idx2 = url.lastIndexOf("/");
    if (idx2 > idx) {
        rdir = url.substring(idx, idx2);
    } else {
        idx2 = idx - 1;
    }
    String rfile = url.substring(idx2 + 1);
    try {
        session = jsch.getSession(user, host, 22);
        session.setPassword(pw);
        session.setConfig("StrictHostKeyChecking", "no");
        // Set timeout to 10 seconds
        session.connect(10000);
        channel = session.openChannel("sftp");
        channel.connect();
        downloadChannelSftp = (ChannelSftp) channel;
        downloadChannelSftp.cd(downloadChannelSftp.getHome() + "/" + rdir);
        File downloadFile = new File(downloadChannelSftp.getHome() + "/" + rfile);
        in = downloadChannelSftp.get(downloadFile.getName());
        return FeedMessage.parseFrom(in, _registry);
    } catch (JSchException ex) {
        _log.error("connection issue with sftp url " + url);
        return getDefaultFeedMessage();
    } catch (SftpException e) {
        _log.error("connection issue with sftp");
        e.printStackTrace();
        return getDefaultFeedMessage();
    } finally {
        try {
            if (channel != null)
                channel.disconnect();
            if (session != null)
                session.disconnect();
            if (in != null)
                in.close();
        } catch (IOException ex) {
            _log.error("error closing url stream " + url);
        }
    }
}
Also used : JSchException(com.jcraft.jsch.JSchException) ChannelSftp(com.jcraft.jsch.ChannelSftp) InputStream(java.io.InputStream) Channel(com.jcraft.jsch.Channel) SftpException(com.jcraft.jsch.SftpException) ServiceAlertLocalizedString(org.onebusaway.transit_data_federation.impl.service_alerts.ServiceAlertLocalizedString) IOException(java.io.IOException) JSch(com.jcraft.jsch.JSch) File(java.io.File) Session(com.jcraft.jsch.Session)

Aggregations

JSch (com.jcraft.jsch.JSch)130 Session (com.jcraft.jsch.Session)72 JSchException (com.jcraft.jsch.JSchException)51 IOException (java.io.IOException)50 Channel (com.jcraft.jsch.Channel)35 File (java.io.File)29 InputStream (java.io.InputStream)29 Properties (java.util.Properties)27 ChannelExec (com.jcraft.jsch.ChannelExec)26 ChannelSftp (com.jcraft.jsch.ChannelSftp)22 KeyPair (com.jcraft.jsch.KeyPair)19 BufferedReader (java.io.BufferedReader)16 UserInfo (com.jcraft.jsch.UserInfo)15 InputStreamReader (java.io.InputStreamReader)14 ByteArrayOutputStream (java.io.ByteArrayOutputStream)13 FileInputStream (java.io.FileInputStream)11 OutputStream (java.io.OutputStream)11 SftpException (com.jcraft.jsch.SftpException)10 FS (org.eclipse.jgit.util.FS)8 FileOutputStream (java.io.FileOutputStream)7