Execute command inside a shell script on a remote machine and get output on local machine

Vini.g.fer

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"' [email protected]

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.

Br. Sayan

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to execute a shell script stored on remote machine from a local machine?

From Dev

Execute shell script in remote machine using ssh command with config file

From Dev

Shell Script to run on Local and Remote machine

From Dev

How to execute linux commands in a remote machine with shell script

From Dev

Execute a command on Remote Machine in Python

From Dev

How can I execute local script on remote machine and include arguments?

From Dev

Execute Script available on remote machine

From Dev

Run shell script on remote machine with SSH using local environment variable

From Dev

how to get the response of a command or shell script from a remote machine in Java code

From Dev

Triggering a shell script on remote machine

From Dev

Execute command on docker container from remote machine

From Dev

execute git command in a remote machine using paramiko

From Dev

Execute local python script including local functions on a remote machine without scp

From Dev

How execute an AutoIt script on remote machine

From Dev

How to execute bash script on a remote machine asynchronously

From Dev

Pipe output of c program on remote machine to local machine

From Dev

Remove directory in a remote machine using shell script

From Dev

Remove directory in a remote machine using shell script

From Dev

executing method on remote machine with sshpass shell script

From Dev

bash - running remote script from local machine

From Dev

Can fabric run a local script on a remote machine?

From Dev

bash - running remote script from local machine

From Dev

Run local python script on remote machine

From Dev

Execute Shell script from workspace on remote machine after build successful (Jenkins)

From Dev

Source environment variables and execute bash before running local script on remote machine

From Dev

How to run local shell script on remote GCP machine via gcloud compute?

From Dev

How to redirect command output from remote machine to local file via ssh?

From Dev

How to redirect command output from remote machine to local file via ssh?

From Dev

How do i execute any command inline ssh as if I'm typing it manually but direct output to local machine?

Related Related

  1. 1

    How to execute a shell script stored on remote machine from a local machine?

  2. 2

    Execute shell script in remote machine using ssh command with config file

  3. 3

    Shell Script to run on Local and Remote machine

  4. 4

    How to execute linux commands in a remote machine with shell script

  5. 5

    Execute a command on Remote Machine in Python

  6. 6

    How can I execute local script on remote machine and include arguments?

  7. 7

    Execute Script available on remote machine

  8. 8

    Run shell script on remote machine with SSH using local environment variable

  9. 9

    how to get the response of a command or shell script from a remote machine in Java code

  10. 10

    Triggering a shell script on remote machine

  11. 11

    Execute command on docker container from remote machine

  12. 12

    execute git command in a remote machine using paramiko

  13. 13

    Execute local python script including local functions on a remote machine without scp

  14. 14

    How execute an AutoIt script on remote machine

  15. 15

    How to execute bash script on a remote machine asynchronously

  16. 16

    Pipe output of c program on remote machine to local machine

  17. 17

    Remove directory in a remote machine using shell script

  18. 18

    Remove directory in a remote machine using shell script

  19. 19

    executing method on remote machine with sshpass shell script

  20. 20

    bash - running remote script from local machine

  21. 21

    Can fabric run a local script on a remote machine?

  22. 22

    bash - running remote script from local machine

  23. 23

    Run local python script on remote machine

  24. 24

    Execute Shell script from workspace on remote machine after build successful (Jenkins)

  25. 25

    Source environment variables and execute bash before running local script on remote machine

  26. 26

    How to run local shell script on remote GCP machine via gcloud compute?

  27. 27

    How to redirect command output from remote machine to local file via ssh?

  28. 28

    How to redirect command output from remote machine to local file via ssh?

  29. 29

    How do i execute any command inline ssh as if I'm typing it manually but direct output to local machine?

HotTag

Archive