Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When using SSH client, the server has no output #441

Closed
dyw770 opened this issue Dec 4, 2023 · 3 comments
Closed

When using SSH client, the server has no output #441

dyw770 opened this issue Dec 4, 2023 · 3 comments

Comments

@dyw770
Copy link

dyw770 commented Dec 4, 2023

Version

2.11.0

Bug description

package cn.dyw.web.jshell.client;

import org.apache.sshd.client.SshClient;
import org.apache.sshd.client.channel.ClientChannel;
import org.apache.sshd.client.channel.ClientChannelEvent;
import org.apache.sshd.client.future.ConnectFuture;
import org.apache.sshd.client.session.ClientSession;
import org.apache.sshd.common.future.CancelOption;
import org.apache.sshd.common.util.io.input.NoCloseInputStream;
import org.apache.sshd.common.util.io.output.NoCloseOutputStream;
import org.junit.jupiter.api.Test;

import java.net.InetSocketAddress;
import java.util.Collections;
import java.util.concurrent.TimeUnit;

/**
 * @author dyw770
 * @date 2023-11-27
 */

public class SSHDClientTest {

    /**
     * Server has no output
     */
    @Test
    public void testSSHDClient() throws Exception {
        SshClient client = SshClient.setUpDefaultClient();
        client.start();
        ConnectFuture connectFuture = client.connect("root", new InetSocketAddress("172.16.253.101", 22));
        ClientSession session = connectFuture.verify(1000, TimeUnit.SECONDS, CancelOption.CANCEL_ON_TIMEOUT).getSession();
        session.addPasswordIdentity("123456789");
        session.auth().verify(1000, TimeUnit.SECONDS, CancelOption.CANCEL_ON_TIMEOUT);
        ClientChannel channel =session.createShellChannel();
        channel.setOut(new NoCloseOutputStream(System.out));
        channel.setRedirectErrorStream(true);
        channel.setIn(new NoCloseInputStream(System.in));
        channel.open().verify(1000, TimeUnit.SECONDS, CancelOption.CANCEL_ON_TIMEOUT);

        channel.waitFor(Collections.singleton(ClientChannelEvent.CLOSED), 0);
        channel.close();
        session.close();
        client.stop();
    }

    /**
     * Normal server output
     */
    public static void main(String[] args) throws Exception {
        SshClient client = SshClient.setUpDefaultClient();
        client.start();
        ConnectFuture connectFuture = client.connect("root", new InetSocketAddress("172.16.253.101", 22));
        ClientSession session = connectFuture.verify(1000, TimeUnit.SECONDS, CancelOption.CANCEL_ON_TIMEOUT).getSession();
        session.addPasswordIdentity("123456789");
        session.auth().verify(1000, TimeUnit.SECONDS, CancelOption.CANCEL_ON_TIMEOUT);
        ClientChannel channel =session.createShellChannel();
        channel.setOut(new NoCloseOutputStream(System.out));
        channel.setRedirectErrorStream(true);
        channel.setIn(new NoCloseInputStream(System.in));
        channel.open().verify(1000, TimeUnit.SECONDS, CancelOption.CANCEL_ON_TIMEOUT);

        channel.waitFor(Collections.singleton(ClientChannelEvent.CLOSED), 0);
        channel.close();
        session.close();
        client.stop();
    }

}

The result is shown in the figure
image
image

Can you tell me where my mistake lies?
Thinks.

Actual behavior

Server has no output

Expected behavior

Normal server output

Relevant log output

No response

Other information

No response

@tomaswolf
Copy link
Member

Cannot reproduce. Using your code on OS X with two minor modifications:

  1. Connect to my own user on localhost port 22.
  2. Use public-key authentication instead of password authentication.

Both running main or running the unit test work, ls or ls -l do show the expected output.

@dyw770
Copy link
Author

dyw770 commented Dec 11, 2023

Cannot reproduce. Using your code on OS X with two minor modifications:

  1. Connect to my own user on localhost port 22.
  2. Use public-key authentication instead of password authentication.

Both running main or running the unit test work, ls or ls -l do show the expected output.

Thank, I think I need to check my ide.

@dyw770
Copy link
Author

dyw770 commented Dec 11, 2023

Idea needs to add -Deditable.java.test.console=true options

@dyw770 dyw770 closed this as completed Dec 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants