You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@mina.apache.org by "Guillaume Nodet (JIRA)" <ji...@apache.org> on 2019/06/04 14:37:00 UTC

[jira] [Comment Edited] (SSHD-922) CD - change directory is not working. clientSession.executeRemoteCommand("cd " + directory)

    [ https://issues.apache.org/jira/browse/SSHD-922?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16855764#comment-16855764 ] 

Guillaume Nodet edited comment on SSHD-922 at 6/4/19 2:36 PM:
--------------------------------------------------------------

I doubt this is supposed to work.

The {{executeRemoteCommand}} is on the SSH session, not on the channel.  This means that there's no shell session to back the state of your command, so each time you use {{executeRemoteCommand}}, it will start from a clean environment, thus loosing the effect of the {{cd}} command.

In order to achieve what you want, you need to grab the input / output stream from the channel and use them to send commands and read the output.  Note that those are streams and there's no easy way to correlate the input to the output (you could launch a background task printing messages regularly for example), so you'll have to read the output yourself from the stream.


was (Author: gnt):
I doubt this is supposed to work.

The `_executeRemoteCommand_` is on the SSH session, not on the channel.  This means that there's no shell session to back the state of your command, so each time you use `_executeRemoteCommand`_, it will start from a clean environment, thus loosing the effect of the `cd` command.

In order to achieve what you want, you need to grab the input / output stream from the channel and use them to send commands and read the output.  Note that those are streams and there's no easy way to correlate the input to the output (you could launch a background task printing messages regularly for example), so you'll have to read the output yourself from the stream.

> CD - change directory is not working. clientSession.executeRemoteCommand("cd " + directory)
> -------------------------------------------------------------------------------------------
>
>                 Key: SSHD-922
>                 URL: https://issues.apache.org/jira/browse/SSHD-922
>             Project: MINA SSHD
>          Issue Type: Bug
>    Affects Versions: 2.2.0
>         Environment: Trying to connect to a Linux VM from Windows OS
>            Reporter: Zabee Ulla
>            Priority: Blocker
>
> I am writing an SSHD client to execute BASH commands on a remove Linux VM. My functionality requires to change directories at several points. 
> I have written and using below code and not finding change directory working. I tried a few other Linux commands and am able to execute them perfectly except change directory. 
> This is a blocker for me to embed and implement Apache SSHD client for my application. 
> Please note that "pwd" output is same before and after _+*session.executeRemoteCommand("cd " + argDirectory);*+_'s execution.
> /**
>  * An implementation of an SFTP client that uses the Apache Mina library.
>  */
>  public class MinaSftp {
> _public void changeRemoteDirectory(String argDirectory)_
>  _throws IOException {_
>  _try {_
>  _System.out.println("Present working direcotry :\n " + _session.executeRemoteCommand("pwd"));_
> _+*String output = _session.executeRemoteCommand("cd " + argDirectory);*+_
> _System.out.println("Present working direcotry :\n " + _session.executeRemoteCommand("pwd"));_
>  _System.out.println("Change directory output: " + output);_
>  _}_
>  _catch (IOException ex) {_
>  _handleException(ex, action);_
>  _}_
>  _}_
> private static Collection<ClientChannelEvent> ccEvents = Arrays.asList(ClientChannelEvent.CLOSED);
> private String _username = "userName";
>  private String _password = "password";
>  private String _host = "aValidHostName";
>  private ClientSession _session;
>  private ClientChannel _channel;
>  private SshClient _client;
> // Some valid port number
>  private int portNumber = 8999;
>  
> public void connect()
>  throws IOException {
> _client = SshClient.setUpDefaultClient();
>  _client.start();
> ConnectFuture connectFuture = _client.connect(_username, _host, portNumber);
>  connectFuture.await();
>  _session = connectFuture.getSession();
>  _channel = _session.createChannel(ClientChannel.CHANNEL_SHELL);
>  _session.addPasswordIdentity(_password);
> // TODO : fix timeout
>  _session.auth().verify(Integer.MAX_VALUE);
> if (_session.isAuthenticated())
> { System.out.println("Authentication successful"); }
> else
> { System.out.println("Authentication failed"); }
> _channel.waitFor(ccEvents, 200);
>  }
> public void disconnect() {
>  LOG.debug("Disconnecting from the SFTP host.");
> // Disconnecting the session disconnects all of the connected channels as well.
>  if (_channel != null) {
>  try
> { _channel.close(); }
> catch (Exception e)
> { // Do nothing. This is okay. }
> }
>  if (_session != null)
> { _session.close(false); }
> if (_client != null)
> { _client.stop(); }
> }
> private void handleException(Exception argEx, String argAction)
>  throws IOException
> { throw LOG.throwing(new IOException(argAction + " on the SFTP host threw an exception.", argEx)); }
> }



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@mina.apache.org
For additional commands, e-mail: dev-help@mina.apache.org