0

I have a simple SSH client which is written in C (libssh) and I need to rewrite the client in Java (jsch). Here is the C code:

#include <libssh/libssh.h>
#include <stdlib.h>
#include <stdio.h> 
#include <string.h>
#include <errno.h>

char TEST[] = "HELLO WORLD!";

int main(){
  ssh_session my_ssh_session;
  int rc;
  int port = 22;
  int verbosity = SSH_LOG_NOLOG;
  char *password, *user;
  ssh_channel channel;

  my_ssh_session = ssh_new();
  if(my_ssh_session == NULL)
  exit(-1);

 ssh_options_set(my_ssh_session, SSH_OPTIONS_HOST, "192.168.1.1");
 ssh_options_set(my_ssh_session, SSH_OPTIONS_PORT, &port);
 ssh_options_set(my_ssh_session, SSH_OPTIONS_LOG_VERBOSITY, &verbosity);   

 rc = ssh_connect(my_ssh_session);
 if(rc != SSH_OK){
 fprintf(stderr, "Error: %s\n", ssh_get_error(my_ssh_session));  
 ssh_free(my_ssh_session);
 exit(-1);
 }

user = "user";
password = "pass";  

 rc = ssh_userauth_password(my_ssh_session, user, password);
 if(rc != SSH_AUTH_SUCCESS){
  fprintf(stderr, "Error authenticating with password: %s\n",
  ssh_get_error(my_ssh_session));
  ssh_disconnect(my_ssh_session);
  ssh_free(my_ssh_session);
  exit(-1);
 }

channel = ssh_channel_new(my_ssh_session);
if(channel == NULL)
return SSH_ERROR;

rc = ssh_channel_open_session(channel);
if(rc != SSH_OK){
 ssh_channel_free(channel);
 return rc;
 }

ssh_channel_write(channel, TEST, sizeof(TEST)); // This line need to be      implemeneted in Java using JSCH librarary.

ssh_channel_send_eof(channel);
ssh_channel_close(channel);
ssh_channel_free(channel);

ssh_disconnect(my_ssh_session);
ssh_free(my_ssh_session);
}

I have figured out the client authentification in Java. The client can create Session and Channel but I am not able to write data in to the SSH channel like in C. I have tried to getOutputStream() on channel and write data in to the stream but the server don't receive any data. I've tried also different settings (sheel,direct-tcpi, etc... ) of openChannel(String type ) method. Here is Java code:

  JSch jsch=new JSch();
  Session session=jsch.getSession(user, host, port);
  session.setPassword(password);
  localUserInfo lui=new localUserInfo();
  session.setUserInfo(lui);
  session.connect();

  Channel channel = (Channel)session.openChannel("session" );

  channel.connect();

  OutputStream out = channel.getOutputStream();
  out.write(buffer);

Do you have any idea how to write in Java using jsch a similar functionality which offers ssh_channel_write(channel, TEST, sizeof(TEST)) from libssh?

thanks

georgeawg
  • 48,608
  • 13
  • 72
  • 95
  • Sounds more like it's the `ssh_channel_send_eof(channel); ssh_channel_close(channel); ssh_channel_free(channel);` lines that need to be implemented. You have to flush your streams for the data to be actually transmitted. `out.flush();` might be sufficient. – that other guy Aug 07 '18 at 22:12

1 Answers1

1

It is highly likely that the data is being added to the buffer, but is never being flushed out to the client.

Try using a PrintWriter, or simple flush your streams.

out.write("abc");
out.flush();

or

PrintWriter writer = new PrintWriter(channel.getOutputStream(), true);
writer.println("abc");
Matt Clark
  • 27,671
  • 19
  • 68
  • 123