Search code examples
sshj2ssh

Unable to execute command on server using j2ssh


I connected to a unix server through ssh and tried to execute a "ls" command and obtain it's output. The code is like this

SessionChannelClient session = client.openSessionChannel();
       session.startShell();
       String cmd = "ls -l";
       session.executeCommand(cmd);           
       ChannelInputStream in = session.getInputStream();
       ChannelOutputStream out = session.getOutputStream();
       IOStreamConnector input = new IOStreamConnector(System.in, session.getOutputStream());
       IOStreamConnector output = new IOStreamConnector(session.getInputStream(), System.out);

After running I was not getting any output in log file. What I found is that the channel request is failing as shown

1019 [main] INFO com.sshtools.j2ssh.connection.ConnectionProtocol - Channel request succeeded 1020 [main] INFO com.sshtools.j2ssh.session.SessionChannelClient - Requesting command execution 1021 [main] INFO com.sshtools.j2ssh.session.SessionChannelClient - Command is ls -l 1021 [main] INFO com.sshtools.j2ssh.connection.ConnectionProtocol - Sending exec request for the session channel 1021 [main] INFO com.sshtools.j2ssh.transport.TransportProtocolCommon - Sending SSH_MSG_CHANNEL_REQUEST 1021 [main] INFO com.sshtools.j2ssh.connection.ConnectionProtocol - Waiting for channel request reply 1032 [Transport protocol 1] INFO com.sshtools.j2ssh.transport.TransportProtocolCommon - Received SSH_MSG_CHANNEL_EXTENDED_DATA 1033 [ssh-connection 1] DEBUG com.sshtools.j2ssh.transport.Service - Routing SSH_MSG_CHANNEL_EXTENDED_DATA 1033 [ssh-connection 1] DEBUG com.sshtools.j2ssh.transport.Service - Finished processing SSH_MSG_CHANNEL_EXTENDED_DATA 1075 [Transport protocol 1] INFO com.sshtools.j2ssh.transport.TransportProtocolCommon - Received SSH_MSG_CHANNEL_FAILURE 1075 [main] INFO com.sshtools.j2ssh.connection.ConnectionProtocol - Channel request failed

Why is this happening ?


Solution

  • You can use startShell or executeCommand, but not both.

    executeCommand is only intended to invoke a particular shell of your choosing, e.g. /bin/bash. In most cases you can just use startShell to start the default shell.

    This is all you need to execute the command you provided:

    final String cmd = "ls -l" + '\n';       
    session.startShell();
    session.getOutputStream().write(cmd.getBytes());
    

    Or, alternatively, using executeCommand:

    final String cmd = "ls -l";    
    session.executeCommand(String.format("sh -c \"%s\"", cmd));
    

    You can hook up to the stdout & stderr InputStreams in the same manner to see the result of your command. Further, note that a SessionChannelClient can only process one command. You'll need to instantiate another one to send a second command.

    I suggest you download the J2SSH documentation. Inside you'll find j2ssh-getting-started.htm which explains the basic operation of the API.