Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running script commands after SSH

Tags:

shell

ssh

I need to write a script that will SSH into a remote host, then run certain commands on that remote host, then exit. If I just do

ssh $host

#some command

the script will SSH in, wait until I manually exit, then run the commands.

How do I run those commands on the remote host?

like image 654
mathematician Avatar asked Aug 29 '12 19:08

mathematician


People also ask

Can you SSH in a script?

SSH is also used to access local Bash scripts from a local or remote server. If you're looking for information on how to run multiple Linux commands in a Bash script, this article contains an extensive guide on how you can achieve this.


1 Answers

ssh $host 'command1; command2; command3'

or if you have just one command:

ssh $host command1

or if you have many commands (a script file):

cat file | ssh $host sh
like image 80
Igor Chubin Avatar answered Oct 01 '22 18:10

Igor Chubin