第一次询问stackoverflow,还使用sshj。除了sshj提供的示例之外,我还没有找到任何有用的资源来帮助使用这个API。
我一直在尝试使用sshj进行远程端口转发并遇到此错误。
Exception in thread "main" net.schmizz.sshj.userauth.UserAuthException: Exhausted available authentication methods
我使用VM测试了auth,但没有使用公钥。我将使用它连接到我知道登录的EC2实例。
public void startRemotePortForwardingConnection(LocalPortForwarder.Parameters parameters) throws IOException{
sshClient.connect(parameters.getLocalHost());
this.connectionStatus = CONNECTED;
System.out.print("Connected to localhost" + NEWLINE);
try {
sshClient.authPassword(this.username, this.password);
System.out.print("Authorized with as user " + username + " with password " + password + NEWLINE);
// the local port we should be listening on
RemotePortForwarder.Forward localPortToListenOn = new RemotePortForwarder.Forward(parameters.getLocalPort());
System.out.print("localPortToListenOn initialized" + NEWLINE);
// where we should forward the packets
InetSocketAddress socketAddress = new InetSocketAddress(parameters.getRemoteHost(), parameters.getRemotePort());
SocketForwardingConnectListener remotePortToForwardTo = new SocketForwardingConnectListener(socketAddress);
System.out.print("remotePortToForwardTo initialized" + NEWLINE);
//bind the forwarder to the correct ports
sshClient.getRemotePortForwarder().bind(localPortToListenOn, remotePortToForwardTo);
System.out.print("ports bound together" + NEWLINE);
sshClient.getTransport().setHeartbeatInterval(30);
sshClient.getTransport().join();
}
finally {
sshClient.disconnect();
}
}
可能不是最佳(甚至是正确)的方式。
答案 0 :(得分:0)
我在类似的先前问题上写了一个例子,你可以直接在groovyconsole中运行,它将连接到EC2实例:https://stackoverflow.com/a/15800383/311525