Reputation: 2042
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?
Upvotes: 9
Views: 13150
Reputation: 13
This can be used too, if you have multiple servers.
for ((i=1; i <= 10; i++))
do
echo $i
ssh host$i << EOF
cd /somedir
command1;
command2;
EOF
done
Upvotes: 1
Reputation: 64563
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
Upvotes: 21