Search in sources :

Example 31 with SshException

use of com.jn.agileway.ssh.client.SshException in project agileway by fangjinuo.

the class SshjConnection method authenticateWithPublicKey.

@Override
public boolean authenticateWithPublicKey(String user, byte[] pemPrivateKey, String passphrase) throws SshException {
    Preconditions.checkNotNull(sshClient);
    String keyContent = new String(pemPrivateKey);
    List<KeyProvider> keyProviders = Collects.emptyArrayList();
    PasswordFinder passwordFinder = null;
    if (Strings.isNotBlank(passphrase)) {
        passwordFinder = PasswordUtils.createOneOff(passphrase.toCharArray());
    }
    try {
        StringReader reader = new StringReader(keyContent);
        PuTTYKeyFile puTTYKeyFile = new PuTTYKeyFile();
        if (passwordFinder == null) {
            puTTYKeyFile.init(reader);
        } else {
            puTTYKeyFile.init(reader, passwordFinder);
        }
        puTTYKeyFile.getPrivate();
        keyProviders.add(puTTYKeyFile);
    } catch (Throwable ex) {
    // ignore it
    }
    try {
        StringReader reader = new StringReader(keyContent);
        PKCS8KeyFile pkcs8KeyFile = new PKCS8KeyFile();
        if (passwordFinder == null) {
            pkcs8KeyFile.init(reader);
        } else {
            pkcs8KeyFile.init(reader, passwordFinder);
        }
        pkcs8KeyFile.getPrivate();
        keyProviders.add(pkcs8KeyFile);
    } catch (Throwable ex) {
    // ignore it
    }
    try {
        StringReader reader = new StringReader(keyContent);
        OpenSSHKeyFile openSSHKeyFile = new OpenSSHKeyFile();
        if (passwordFinder == null) {
            openSSHKeyFile.init(reader);
        } else {
            openSSHKeyFile.init(reader, passwordFinder);
        }
        openSSHKeyFile.getPrivate();
        keyProviders.add(openSSHKeyFile);
    } catch (Throwable ex) {
    // ignore it
    }
    try {
        Preconditions.checkState(!keyProviders.isEmpty(), "the private key is invalid: " + keyContent);
        sshClient.authPublickey(user, keyProviders);
    } catch (UserAuthException ex) {
        logger.error(ex.getMessage(), ex);
        return false;
    } catch (Throwable ex) {
        throw new SshException(ex);
    }
    return false;
}
Also used : KeyProvider(net.schmizz.sshj.userauth.keyprovider.KeyProvider) PasswordFinder(net.schmizz.sshj.userauth.password.PasswordFinder) PuTTYKeyFile(net.schmizz.sshj.userauth.keyprovider.PuTTYKeyFile) OpenSSHKeyFile(net.schmizz.sshj.userauth.keyprovider.OpenSSHKeyFile) StringReader(java.io.StringReader) PKCS8KeyFile(net.schmizz.sshj.userauth.keyprovider.PKCS8KeyFile) SshException(com.jn.agileway.ssh.client.SshException) UserAuthException(net.schmizz.sshj.userauth.UserAuthException)

Example 32 with SshException

use of com.jn.agileway.ssh.client.SshException in project agileway by fangjinuo.

the class SshjConnection method openSftpSession.

@Override
public SftpSession openSftpSession() throws SshException {
    try {
        SFTPClient client = sshClient.newSFTPClient();
        SshjSftpSession session = new SshjSftpSession(client);
        session.setSshConnection(this);
        return session;
    } catch (Throwable ex) {
        throw new SshException(ex);
    }
}
Also used : SshjSftpSession(com.jn.agileway.ssh.client.impl.sshj.sftp.SshjSftpSession) SFTPClient(net.schmizz.sshj.sftp.SFTPClient) SshException(com.jn.agileway.ssh.client.SshException)

Example 33 with SshException

use of com.jn.agileway.ssh.client.SshException in project agileway by fangjinuo.

the class J2sshForwardingClient method startLocalForwarding.

@Override
public ForwardingChannelInfo startLocalForwarding(String bindToHost, int bindToPort, String destHost, int destPort) throws SshException {
    ForwardingChannelInfo channel = new ForwardingChannelInfo(ForwardingChannelInfo.LOCAL_FORWARDING_CHANNEL, bindToHost, bindToPort, destHost, destPort);
    SshClient sshClient = this.connection.getDelegate();
    com.sshtools.j2ssh.forwarding.ForwardingClient delegate = sshClient.getForwardingClient();
    try {
        if (!delegate.getLocalForwardings().containsKey(ForwardingChannelInfo.id(channel))) {
            delegate.addLocalForwarding(ForwardingChannelInfo.id(channel), bindToHost, bindToPort, destHost, destPort);
        }
        delegate.startLocalForwarding(ForwardingChannelInfo.id(channel));
    } catch (Throwable ex) {
        throw new SshException(ex);
    }
    return channel;
}
Also used : SshClient(com.sshtools.j2ssh.SshClient) ForwardingChannelInfo(com.jn.agileway.ssh.client.channel.forwarding.ForwardingChannelInfo) SshException(com.jn.agileway.ssh.client.SshException)

Example 34 with SshException

use of com.jn.agileway.ssh.client.SshException in project agileway by fangjinuo.

the class J2sshForwardingClient method startRemoteForwarding.

@Override
public ForwardingChannelInfo startRemoteForwarding(String bindToHost, int bindToPort, String destHost, int destPort) throws SshException {
    ForwardingChannelInfo channel = new ForwardingChannelInfo(ForwardingChannelInfo.REMOTE_FORWARDING_CHANNEL, bindToHost, bindToPort, destHost, destPort);
    SshClient sshClient = this.connection.getDelegate();
    com.sshtools.j2ssh.forwarding.ForwardingClient delegate = sshClient.getForwardingClient();
    try {
        if (!delegate.getRemoteForwardings().containsKey(ForwardingChannelInfo.id(channel))) {
            delegate.addRemoteForwarding(ForwardingChannelInfo.id(channel), bindToHost, bindToPort, destHost, destPort);
        }
        delegate.startRemoteForwarding(ForwardingChannelInfo.id(channel));
    } catch (Throwable ex) {
        throw new SshException(ex);
    }
    return channel;
}
Also used : SshClient(com.sshtools.j2ssh.SshClient) ForwardingChannelInfo(com.jn.agileway.ssh.client.channel.forwarding.ForwardingChannelInfo) SshException(com.jn.agileway.ssh.client.SshException)

Example 35 with SshException

use of com.jn.agileway.ssh.client.SshException in project agileway by fangjinuo.

the class Ssh2ForwardingClient method startRemoteForwarding.

@Override
public ForwardingChannelInfo startRemoteForwarding(String bindToHost, int bindToPort, String destHost, int destPort) throws SshException {
    Connection delegate = this.connection.getDelegate();
    ForwardingChannelInfo channel = new ForwardingChannelInfo(ForwardingChannelInfo.REMOTE_FORWARDING_CHANNEL, bindToHost, bindToPort, destHost, destPort);
    try {
        delegate.requestRemotePortForwarding(bindToHost, bindToPort, destHost, destPort);
    } catch (Throwable ex) {
        throw new SshException(ex);
    }
    return channel;
}
Also used : Connection(com.trilead.ssh2.Connection) ForwardingChannelInfo(com.jn.agileway.ssh.client.channel.forwarding.ForwardingChannelInfo) SshException(com.jn.agileway.ssh.client.SshException)

Aggregations

SshException (com.jn.agileway.ssh.client.SshException)40 ForwardingChannelInfo (com.jn.agileway.ssh.client.channel.forwarding.ForwardingChannelInfo)7 RequestFuture (com.sshtools.common.ssh.RequestFuture)4 SshClient (com.sshtools.j2ssh.SshClient)4 Connection (com.trilead.ssh2.Connection)3 InetSocketAddress (java.net.InetSocketAddress)3 SSHClient (net.schmizz.sshj.SSHClient)3 AbstractSshConnection (com.jn.agileway.ssh.client.AbstractSshConnection)2 Ssh2SftpSession (com.jn.agileway.ssh.client.impl.ganymedssh2.sftp.Ssh2SftpSession)2 ToSynergyHostKeyVerifierAdapter (com.jn.agileway.ssh.client.impl.synergy.verifier.ToSynergyHostKeyVerifierAdapter)2 Ssh2SftpSession (com.jn.agileway.ssh.client.impl.trileadssh2.sftp.Ssh2SftpSession)2 SftpSession (com.jn.agileway.ssh.client.sftp.SftpSession)2 SshClient (com.sshtools.client.SshClient)2 SshClientContext (com.sshtools.client.SshClientContext)2 RemotePortForwarder (net.schmizz.sshj.connection.channel.forwarded.RemotePortForwarder)2 Connection (ch.ethz.ssh2.Connection)1 LocalPortForwarder (ch.ethz.ssh2.LocalPortForwarder)1 SFTPv3Client (ch.ethz.ssh2.SFTPv3Client)1 Session (ch.ethz.ssh2.Session)1 ChannelSftp (com.jcraft.jsch.ChannelSftp)1