user1040229
user1040229

Reputation: 501

Access remote directory in shell script

I have a shell script that accepts directory inputs. One of the directories I want to process is at a remote location. I am able to ssh into the remote host but I want to be able to reference the remote directory in a similar manner as a UNC path on Windows (in Windows you can type "\[remote host][share]" and reference that path in batch files). Is there a clean way to do a similar thing with ssh on Unix? When I call the shell script, I want to be able to type something like:

./run_shell_script.sh ssh://<host>/<path>

instead of running extra commands just to process that one directory? This is being done on a list of directories in a loop, so I don't want to have special code for remote directories.

Upvotes: 1

Views: 1333

Answers (1)

Dmytro Sirenko
Dmytro Sirenko

Reputation: 5083

One of possible solutions is to parse the URI like this:

_uri=ssh://host/path/to/file
_host=`echo $_uri | sed 's|^ssh://\([^/]*\)/.*$|\1|'`
_dir=`echo $_uri | sed "s|^ssh://$_host/\(.*\)\$|\1|"`

and then just to use these variables to ssh:

scp $_host:$_dir $local_dir

Also, sshfs may be interesting for you: it allows to mount a remote host into a directory on the local machine without the NFS administration burden and even root privileges.

Upvotes: 1

Related Questions