Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apache SSHD & JSCH

I want mocking ssh server for transfer file. For do it i wand use Apache Mina SSHD. For transfer in system use JSch. with private key.

JSch client code.

    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 code

    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();

Can anybody help me connect JSch and Apache Mina SSHD for transfer file?

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
like image 539
Rinat Mukhamedgaliev Avatar asked Apr 30 '26 01:04

Rinat Mukhamedgaliev


1 Answers

You should not call setHost() for setting the port. As it is clearly mentioned in the document.

 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().

Instead You should give the port while calling getSession(). Something as given below

int port=22999;
Session session = jsch.getSession(user, host, port);
like image 81
AjayLohani Avatar answered May 02 '26 15:05

AjayLohani



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!