For all coming via google to this question because they are looking for a way to list remote files but can not access the remote server via ssh (common case for backup servers) you could use 'sftp'.
I find my most frequent use of this is to get the ls result, a simple list of files without all the permissions and dates and such, and keep it in a local file.
You can run anything you want within the quotes. All output ends up in your local text file. Or if you want to run a big nasty script on the server and capture all of its output, even errors:
As mentioned, if you can SSH into the host, you can do whatever you like.
You can use either ssh user@host or ssh alias(defined in ~/.ssh/config)
Examples
Edit: Fixed some examples by single quoting the entire command to be sent. Thanks @dave_thompson_085 for pointing to the problem.
$ ssh user@host 'ls /dir/file'
You mentioned that you don't want to receive the errors from ls if the file does not exist.
One option is to just discard the error messages
$ ssh user@host 'ls /dir/file 2>/dev/null'
This will preserve the return as well. So $? will be 0 only if the ls command found /dir/file. This is nice because it works for files, folders, symlinks or anything that can be listed by ls.
Another option is to check if the file exists before listing.
$ ssh user@host '[ -f /dir/file ] && ls /dir/file'
You could also test and list a folder, but the syntax changes
The way quoting and expansion works can be tricky though. If you use double quotes outside the main command (instead of single quotes as above), variables will be expanded BEFORE connecting ssh, unless you escape the $. It is normally easier to use single quotes to avoid that, but your mileage may vary. Some times you need stuff to expand locally AND remotelly, which can be tricky.
You could always do this:
That will SSH to the host, run ls, dump the output back to you and immediately disconnect.
To list all files in a directory:
For something like find directory/path -ls
For all coming via google to this question because they are looking for a way to list remote files but can not access the remote server via ssh (common case for backup servers) you could use 'sftp'.
Example:
Start an interactive session in a specific remote directory:
Yes. SSH and do an
ls
:You could easily script this to be more flexible, or use the host:path syntax
scp
uses.The above answers do not contemplate when you need to add a password. To include password and username in a single command, install
sshpass
.For mac:
$ brew install hudochenkov/sshpass/sshpass
For linux:
sudo apt-get install sshpass -y
Then:
$ sshpass -p your_password ssh user@hostname ls /path/to/dir/
You can also save output:
$ sshpass -p your_password ssh user@hostname ls /path/to/dir/ > log.txt
In python3:
I find my most frequent use of this is to get the ls result, a simple list of files without all the permissions and dates and such, and keep it in a local file.
You can run anything you want within the quotes. All output ends up in your local text file. Or if you want to run a big nasty script on the server and capture all of its output, even errors:
As mentioned, if you can SSH into the host, you can do whatever you like.
You can use either
ssh user@host
orssh alias
(defined in~/.ssh/config
)Examples
Edit: Fixed some examples by single quoting the entire command to be sent. Thanks @dave_thompson_085 for pointing to the problem.
You mentioned that you don't want to receive the errors from
ls
if the file does not exist.One option is to just discard the error messages
This will preserve the return as well. So
$?
will be 0 only if the ls command found /dir/file. This is nice because it works for files, folders, symlinks or anything that can be listed by ls.Another option is to check if the file exists before listing.
You could also test and list a folder, but the syntax changes
You could chain and test for ANY as well
You could also just check if the item exists and nothing else
You may want to include a test for symlinks with
[ -L /my/target ]
as well.You could run conditional codes remotelly
Or locally if you move the commands outside the command string
The way quoting and expansion works can be tricky though. If you use double quotes outside the main command (instead of single quotes as above), variables will be expanded BEFORE connecting ssh, unless you escape the
$
. It is normally easier to use single quotes to avoid that, but your mileage may vary. Some times you need stuff to expand locally AND remotelly, which can be tricky.