Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have ssh access to other Suse box and I want to list particular directory on the suse box. It will be done from other suse box. I will run this from php later on.

Is something like that possible?

share|improve this question

2 Answers 2

up vote 11 down vote accepted

You can have ssh return the output of any remote command simply by sending the command as the last argument to your ssh comand:

ssh user@host 'ls /path/to/dir'

If you have key based authentication setup this can be done without entering a password.

However parsing the output of ls is always a bad idea, and it sounds like you might have a use case for something a little fancier. There is a file system called SSHFS that allows you to mount virtually any file system that you can ssh into. You could mount the remote directory so that your php script could operate on it as if it was a set of local files and directories.

share|improve this answer
    
to run SSHFS do I have to install anything on the remote box? Is it costly for the remote host? Remote host are production sites and I want to download backup that sits there. I want to be able to select which backup file to download although it would be in most cases the latest one. –  Radek Aug 17 '11 at 7:49
2  
@Radek You do not need to run anything special on the remote box, SSHFS only needs normal SFTP access. On the local box, you need to have FUSE installed. –  Gilles Aug 17 '11 at 9:18
1  
@Radek: A slight addition to Caleb's suggestion.. If you do end up parsing the output of ls then you can use $ssh user@host 'ls -1 /path/to/dir' to get one item per line. This usually makes it easier to eat the output when you pipe it to the next stage of parsing. –  unclejamil Aug 17 '11 at 13:01
1  
@unclejamil: That is is not necessary because ls automatically detects when it's output is being piped somewhere and returns -1 anyway. A better way would to use find -maxdepth -print0 to get the output with each file name separated by a null so that you can parse it without a change of breaking on funky file names. –  Caleb Aug 17 '11 at 13:40
    
@Caleb: I didn't know ls was smart like that. Thank you for the info. –  unclejamil Aug 17 '11 at 13:44

Yes, it is possible. SSH clients usually take an optional argument to specify a command to execute. For instance:

ssh user@remoteserver "ls particulardirectory"
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.