0

I'm trying to read data from the server with SSH protocol. For this, I'm using the j2ssh library. My server connects with the other server in ssh without any problem. The problem is when I try to read any data from the shell command line. Whatever "command" I send to program "read = in.read(buffer)" never get any data, I tried with "ls" with "cat filename.txt" and other commands.

Only one command works fine and is "tail -f filename.txt". With this command, I can see the buffer is not empty, this contain the text of file, but the tail command does not close and while listening, sends the program in loop.

Can Anyone help me to know why I can't get any data from othere command?

This is my code:

 private String exec(String cmd) throws SSHHandlerException {
    String result = null;

    session = ssh.openSessionChannel();

    if(session.startShell())
    {
        session.getOutputStream().write((cmd+"\n").getBytes());
        session.getOutputStream().close();
        result = read(session,log);
    }
    session.close();
    ssh.disconnect();
    return result;
}

private static String read(SessionChannelClient session, ProcessLogger log) throws Exception{
    byte buffer[] = new byte[255];
    int read;
    StringBuffer out=new StringBuffer();
    InputStream in = session.getInputStream();

    while((read = in.read(buffer)) > 0) {
        out.append(new String(buffer, 0, read));
    }

    return out.toString();
}
Simone
  • 29
  • 1
  • 7

1 Answers1

1

If your goal is to transfer files, you should be using an SFTP client instead. SFTP is exactly what you're looking for: a file transfer protocol on top of SSH. It's much, much more efficient than using some command on the host and redirecting the stream.

J2SSH has an SftpClient implementation that can be constructed with an SshClient. Just use one of the get methods. Javadocs are here.

Edit after learning that you're not trying to transfer files:

You need to request a pseudo-terminal before you start the shell. From the docs:

The remote process may require a pseudo terminal. Call this method before executing a command or starting a shell.

Also, because it appears that you're using a Linux environment, I would recommend using terminal type "xterm" rather than their example of "vt100".

The reason that tail was working and not the other commands was because you were calling tail interactively. The interactive command creates its own pseudo-terminal of sorts. If, instead, you call tail -n 16 filename.txt then you will get the same results as with the other commands because it won't be interactive.

William Rosenbloom
  • 2,506
  • 1
  • 14
  • 37
  • Thank for the comment. My goal is not to read a file, I need to read, after send command, the shell "response". I first try with easy commands "ls", "cat" etc but I can't read anything with this commands and I don't know why. – Simone Dec 11 '20 at 13:22
  • @Simone oh, so you were only using cat and tail as examples of commands? – William Rosenbloom Dec 11 '20 at 13:29