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

[jira] [Resolved] (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:all-tabpanel ]

Goldstein Lyor resolved SSHD-922.
---------------------------------
    Resolution: Not A Problem
      Assignee: Goldstein Lyor

The code seems to indicate some confusion as to what a remote command is. {{executeRemoteCommand}} uses an "exec" channel. Such a channel is used to run a *single* command and then disconnect. When the channel is established by the server, some "home" directory is assigned by the *server*. The "cd" command succeeds but then the channel is closed and the server forgets all about it. When "pwd" command is issued, it will show the default home directory - same one from which "cd" was executed. In other words, an "exec" command is "stateless" inasmuch as the server will forget any CWD, TTY settings, etc...

If you wish to maintain the "state" then you must use a "shell" channel - and even then the state (e.g. CWD) will be maintained only for the duration of the shell session/channel. Once it is closed, the server will revert back to its default settings (until the next shell/command).

> 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
>            Assignee: Goldstein Lyor
>            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