File operations over SSH

Hi everybody,

I want to rewrite a Bash script in Rust. The script is simple - it just creates/removes/modify directories and files and std::fs / tokio::fs has all the functionality I need.

The only problem is that the script works against a local Linux or a remote Linux host over SSH. The Bash script does all the work simply by executing touch, mkdir, etc. locally or remotely as ssh "$foobar" mkdir...

I tried openssh module and it works well.

But I wonder is there a smarter way that doesn't involve executing commands like this?

session.command("mkdir").arg("/tmp/foobar").output().await

In other words something like std::fs that would work remotely.

Thank you.

There is the openssh-sftp-client crate which allows some filesystem operations

It has to be used with the openssh crate.

1 Like

Using DirBuilder

DirBuilder::new().recursive(true).create(path)
/// this creates directory if not exist and overide the dir and creates new on same path

I haven't thought about SFTP, that's a good tip, thank you. The only problem is that SFTP is often disabled (I guess for security reasons).

Thank you. That's a good way to create a dir on a localhost but unless I'm missing something, it's not for remote use.

Really? That would break scp too. Nowadays scp internally uses sftp as protocol rather than it's own custom thing.

I was thinking that too. Disabling the SFTP module of sshd while leaving full shell access is kinda... Weird

It is strange but I remember that scp/sshfs doesn't always work and that's a tell.

But it's worth to try.

I've just found russh-sftp so that's what I'm going to try now. Thank you.