Shengqi Wang
Shengqi Wang

Reputation: 161

How to run shell script in another server in a script?

No where online can i find a way to run a shell script on a remote server from another script. This is for automation, so the script on the host machine will automatically trigger another script on a different server. The server that my script will ssh to will either have a password prompt or have RSA key pair set up

Thanks!

Upvotes: 5

Views: 21219

Answers (3)

Omari Victor Omosa
Omari Victor Omosa

Reputation: 2869

Another way, using expect package.

Disclaimer: This you can use for testing environments since it has an open password. but depends on your usecase

If your server does not have expect, you may add the package then. run the command. You can also put this command inside an .sh script.

expect -c 'spawn ssh [email protected] "/path/to/my.sh"; expect "assword:"; send "Y0urp@ssw0rd\r"; interact'

Upvotes: 0

user12711146
user12711146

Reputation: 11

Let's say you want to execute a script on node2 but you have your script on node1 file name of script is sp over location /home/user/sp. Simply

ssh node2 < /path-of-the-script-including-the-filename

Upvotes: 1

Quentin
Quentin

Reputation: 943142

Just pass the command as an argument to ssh.

ssh someserver /path/to/some/script.bsh

Upvotes: 6

Related Questions