Execute commands on a remote servers by 2 scripts

MohammedSimba

Currently I am using SSHPass in a script to connect to a remote server:

sshpass -p PASSWORD ssh -o StrictHostKeyChecking=no [email protected]

and to execute any command remotely, I use:

sshpass -p PASSWORD ssh -o StrictHostKeyChecking=no [email protected] "COMMAND"

I am planning to separate the connection step in one script (conn.sh) and the commands in another (cmd.sh).

I tried this way:

cmd.sh:

#!/bin/bash
ls -ltr

conn.sh:

#!/bin/bash    
sshpass -p PASSWORD ssh -o StrictHostKeyChecking=no [email protected] "$1"

And execution after giving both scripts "chmod a+x":

./conn.sh cmd.sh

But it gave me error:

cmd.sh: command not found

I even tried to replace $1 with the path to cmd.sh, but it gave me this error:

sh: /home/myhome/cmd.sh: No such file or directory
terdon

The reason it fails is that when you run something like

ssh user@remote command

The command is not only run on the remote machine, it is also searched for on the remote machine. Therefore, when you run ./conn.sh cmd.sh, you are connecting to the remote and then you are trying to run a script called cmd.sh on the remote. Since there is no such script (it's stored on your local machine), the execution fails and you see that error.

You can do what you describe by changing your conn.sh to:

#!/bin/bash
sshpass -p PASSWORD ssh -o StrictHostKeyChecking=no [email protected] \
 bash -s < "$1"

And then run

./conn.sh /local/path/to/cmd.sh

That will open a bash shell on the remote machine. The -s tells bash to read commands from standard input and the < "$1" provides the 1st argument given to conn.sh as the input to that bash shell.

Are you sure you need to do this though? What you describe is almost certainly an XY problem. The simple solution is to create the cmd.sh on the remote server. Then you can do

./conn.sh /remote/path/to/cmd.sh

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Bash for loop to execute commands on remote servers

From Dev

Running remote scripts on Linux Servers from Windows

From Dev

How to execute multiple commands remotely on few servers?

From Dev

How to execute local script as root in remote servers?

From Java

How to execute mongo commands through shell scripts?

From Dev

Execute 2 or more remote scripts sharing the same curl pattern, without redundancy

From Dev

golang: Execute shell commands on remote server

From Dev

How to easily execute R commands on remote server?

From Dev

Execute remote commands as root using Python and paramiko

From Dev

Ansible doesn't execute commands on remote server

From Dev

How to easily execute R commands on remote server?

From Dev

Running remote cqlsh to execute commands on Cassandra Cluster

From Dev

Execute remote commands using variables in a for loop

From Dev

Execute multiple remote commands on SSH and close connection

From Dev

Login to remote Linux box and execute commands

From Dev

Ansible doesn't execute commands on remote server

From Dev

execute 2 commands in File shortcut

From Dev

execute 2 commands within container

From Dev

How to run VMware commands from remote scripts on windows

From Dev

Team Foundation Server: Execute Powershell scripts/commands to build instead of msbuild

From Dev

Team Foundation Server: Execute Powershell scripts/commands to build instead of msbuild

From Dev

Can't execute init scripts on remote host via ssh

From Dev

Errant error output behavior with perl execute bash scripts on a remote machine

From Dev

Can't execute init scripts on remote host via ssh

From Dev

Execute system commands using wmi python on remote computer

From Dev

How to execute remote pysftp commands under a specific shell

From Dev

Execute several commands through ssh with local and remote values

From Dev

How to execute "php artisan migrate" and other Laravel commands in remote server?

From Dev

Execute sftp commands on remote server using batch file and PuTTY

Related Related

  1. 1

    Bash for loop to execute commands on remote servers

  2. 2

    Running remote scripts on Linux Servers from Windows

  3. 3

    How to execute multiple commands remotely on few servers?

  4. 4

    How to execute local script as root in remote servers?

  5. 5

    How to execute mongo commands through shell scripts?

  6. 6

    Execute 2 or more remote scripts sharing the same curl pattern, without redundancy

  7. 7

    golang: Execute shell commands on remote server

  8. 8

    How to easily execute R commands on remote server?

  9. 9

    Execute remote commands as root using Python and paramiko

  10. 10

    Ansible doesn't execute commands on remote server

  11. 11

    How to easily execute R commands on remote server?

  12. 12

    Running remote cqlsh to execute commands on Cassandra Cluster

  13. 13

    Execute remote commands using variables in a for loop

  14. 14

    Execute multiple remote commands on SSH and close connection

  15. 15

    Login to remote Linux box and execute commands

  16. 16

    Ansible doesn't execute commands on remote server

  17. 17

    execute 2 commands in File shortcut

  18. 18

    execute 2 commands within container

  19. 19

    How to run VMware commands from remote scripts on windows

  20. 20

    Team Foundation Server: Execute Powershell scripts/commands to build instead of msbuild

  21. 21

    Team Foundation Server: Execute Powershell scripts/commands to build instead of msbuild

  22. 22

    Can't execute init scripts on remote host via ssh

  23. 23

    Errant error output behavior with perl execute bash scripts on a remote machine

  24. 24

    Can't execute init scripts on remote host via ssh

  25. 25

    Execute system commands using wmi python on remote computer

  26. 26

    How to execute remote pysftp commands under a specific shell

  27. 27

    Execute several commands through ssh with local and remote values

  28. 28

    How to execute "php artisan migrate" and other Laravel commands in remote server?

  29. 29

    Execute sftp commands on remote server using batch file and PuTTY

HotTag

Archive