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;
}
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);
}
}
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;
}
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;
}
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;
}
Aggregations