我想模拟ssh服务器传输文件。为此,我希望使用Apache Mina SSHD。若要在系统中传输,请使用JSch。使用私钥。
JSch客户端代码。
JSch jSch = new JSch();
jSch.addIdentity(privateKeyPath, passPhrase);
Session session = jSch.getSession(usernamePlans, remoteHost);
session.setHost(remoteHostPort);
config.setProperty("StrictHostKeyChecking", "no");
session.setConfig(config);
session.setUserInfo(new StorageUserInfo(passPhrase));
session.connect();
Apache Mina SSHD代码
sshd = SshServer.setUpDefaultServer();
sshd.setPort(22999);
sshd.setKeyPairProvider(new SimpleGeneratorHostKeyProvider(privateKeyPath));
sshd.setPasswordAuthenticator(new PasswordAuthenticator() {
public boolean authenticate(String username, String password, ServerSession session) {
// TODO Auto-generated method stub
return true;
}
});
CommandFactory myCommandFactory = new CommandFactory() {
public Command createCommand(String command) {
System.out.println("Command: " + command);
return null;
}
};
sshd.setCommandFactory(new ScpCommandFactory(myCommandFactory));
List<NamedFactory<Command>> namedFactoryList = new ArrayList<>();
namedFactoryList.add(new SftpSubsystem.Factory());
sshd.setSubsystemFactories(namedFactoryList);
sshd.start();
有人能帮我连接JSch和Apache Mina SSHD来传输文件吗?
StackTrace
com.jcraft.jsch.JSchException: java.net.SocketException: Invalid argument or cannot assign requested address
Caused by: java.net.SocketException: Invalid argument or cannot assign requested address
发布于 2016-12-05 18:53:14
您不应该调用setHost()来设置端口。正如文档中明确提到的那样。
setHost
public void setHost(String host)
Sets the host to connect to. This is normally called by JSch.getSession(java.lang.String, java.lang.String), so there is no need to call it, if you don't want to change this host. This should be called before connect().
setPort
public void setPort(int port)
Sets the port on the server to connect to. This is normally called by JSch.getSession(java.lang.String, java.lang.String), so there is no need to call it, if you don't want to change the port. This should be called before connect().
相反,您应该在调用getSession()时提供端口。如下所示
int port=22999;
Session session = jsch.getSession(user, host, port);
https://stackoverflow.com/questions/17260233
复制相似问题