Rinat Mukhamedgaliev
Rinat Mukhamedgaliev

Reputation: 5721

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

Upvotes: 3

Views: 4787

Answers (1)

AjayLohani
AjayLohani

Reputation: 952

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

Upvotes: 1

Related Questions