Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is there any way to set the current directory when executing command in a session #140

Open
agjini opened this issue Jan 19, 2024 · 3 comments

Comments

@agjini
Copy link

agjini commented Jan 19, 2024

I'm executing remote commands through a Session using Session::command()

But I don't find a way to set the current directory for a given command. Even the cd command does not keep the directory changed for subsequent commands.

I am looking for something like std::process::Command::current_dir() method.

Is there any way to do this ?

@NobodyXu
Copy link
Member

You can change the .ssh/config by using tempfile to create a temporary one and try something like:

Host myhost
  HostName IP
  User ubuntu
  IdentityFile ~/.ssh/id_rsa
  RemoteCommand cd /path/to/directory; $SHELL -il

@agjini
Copy link
Author

agjini commented Jan 20, 2024

Thanks for the idea. But it's such a manipulation before connecting a session. And the whole session is bound to this directory.

I've read in the source code that openssh doesn't support setting current directory (yet ?) :

command.rs:67

as only underlying shell support it. But is there a way to enable it on case bash for example is present on the remote host?

Thanks for your light

@NobodyXu
Copy link
Member

Thanks for the idea. But it's such a manipulation before connecting a session. And the whole session is bound to this directory.

You can specify an alternative ssh config using SessionBuilder, before connecting.

You can then use crate tempfile to create a named temp file and write the ssh config into it, then pass it to SessionBuilder::config_file

I've read in the source code that openssh doesn't support setting current directory (yet ?)

No, AFAIK ssh doesn't support such option.

as only underlying shell support it. But is there a way to enable it on case bash for example is present on the remote host?

You could write a bash script, or use Session::shell

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants