在 J2SSH Maverick 1.55 版本中遇到的“Unsupported type: OPENSSH PRIVATE KEY”异常,哪个版本的J2SSH Maverick支持openssh的密钥呢,最好把代码贴出来看看
时间: 2023-02-08 16:29:49 浏览: 79
J2SSH Maverick 1.55 版本不支持 OpenSSH 私钥。需要使用更高版本的 J2SSH Maverick。
可以使用下面的代码来使用 OpenSSH 私钥登录服务器:
```
import com.sshtools.j2ssh.*;
import com.sshtools.j2ssh.authentication.*;
import com.sshtools.j2ssh.transport.*;
SshClient ssh = new SshClient();
// Connect to the host
ssh.connect("hostname", new Ssh2Client());
// Create a password authentication instance
PasswordAuthenticationClient pwd = new PasswordAuthenticationClient();
pwd.setUsername("username");
pwd.setPassword("password");
// Try the authentication
int result = ssh.authenticate(pwd);
if(result==AuthenticationProtocolState.COMPLETE) {
// Authentication complete, start a session
SessionChannelClient session = ssh.openSessionChannel();
session.startShell();
} else {
// Authentication failed
System.out.println("Authentication failed");
}
```
如果要使用 OpenSSH 私钥登录,可以使用 SshPrivateKeyFile 类来加载私钥文件,并将其作为参数传递给 PublicKeyAuthenticationClient,如下所示:
```
SshPrivateKeyFile pkfile = SshPrivateKeyFile.parse(new File("private_key_file"));
PublicKeyAuthenticationClient pk = new PublicKeyAuthenticationClient();
pk.setUsername("username");
pk.setKey(pkfile.toPrivateKey(null));
int result = ssh.authenticate(pk);
```
请注意,在这种情况下,需要在服务器上配置公钥认证。