0

There are similar questions on stackoverflow, but they either don't have an answer or use some language (C#, Python, ...). I'm trying to execute a command on a remote machine using ssh and get the console output back to the local machine. Below is the command I'm having issues:

sshpass -p $password 'find /home/pi/Transmission_Downloads/ -type f \( -iname "*.mp4" -o -iname "*.mkv" -o -iname "*.avi" \) -newermt "2016-02-01"' user@myserver.com

When I try to execute it inside my script I get "sshpass: Failed to run command: No such file or directory" error.

What I'm trying to achieve: fetch from the server a list of new files downloaded (movies and TV shows) for later on pulling them from the server using rsync.

Is there a way I can achieve this using only password, or do I HAVE TO use public/private keys to access the server?

My local machine is using Ubuntu 14.04 (desktop) and my server is running Raspbian.

Vini.g.fer
  • 11,639
  • 16
  • 61
  • 90

1 Answers1

1

Setting up a password less login to your remote machine might be a solution for easily accomplishing the task.

First log in on Sys_A as user a and generate a pair of authentication keys. Do not enter a passphrase:

a@Sys_A:~> ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/a/.ssh/id_rsa): 
Created directory '/home/a/.ssh'.
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /home/a/.ssh/id_rsa.
Your public key has been saved in /home/a/.ssh/id_rsa.pub.
The key fingerprint is:
3e:4f:05:79:3a:9f:96:7c:3b:ad:e9:58:37:bc:37:e4 a@A

Now use ssh to create a directory ~/.ssh as user b on Sys_B. (The directory may already exist, which is fine):

a@Sys_A:~> ssh b@B mkdir -p .ssh
b@Sys_B's password: 

Finally append a's new public key to b@Sys_B:.ssh/authorized_keys and enter b's password one last time:

a@Sys_A:~> cat .ssh/id_rsa.pub | ssh b@B 'cat >> .ssh/authorized_keys'
b@Sys_B's password:

From now on you can log into Sys_B as b from Sys_A as a without password:

a@Sys_A:~> ssh b@Sys_B

Then you can integrate your command in a bash script, and use ssh without any user interaction.

Br. Sayan
  • 486
  • 1
  • 7
  • 22
  • The problem is I would have to create public/private keys for each computer I would like to pull the files to. Is there any way to achieve this using password (sshpass for example)? – Vini.g.fer Feb 13 '16 at 20:38
  • Another way of doing the thing might be `expect`, please see : [Use expect in bash script to provide password to SSH command](http://stackoverflow.com/questions/4780893/use-expect-in-bash-script-to-provide-password-to-ssh-command). How many computers are you dealing with? For 5-10 computers, setting up SSH keys will be pretty quick and after that the rest is very easy. And above all SSH keys are more secure than sending raw passwords over the network. – Br. Sayan Feb 14 '16 at 07:52
  • Is it just me, or using SSH keys adds a small delay (1~3 seconds) during auth? – Vini.g.fer Feb 15 '16 at 13:15
  • 1
    Yes, for the first time it was the case with me also. Don't know if it happens with others. But after that the script should run smoothly. Better if you add it to `crontab`. It will run at scheduled time without any user intervention. – Br. Sayan Feb 16 '16 at 03:30