简体   繁体   English

SSH后运行脚本命令

[英]Running script commands after SSH

I need to write a script that will SSH into a remote host, then run certain commands on that remote host, then exit. 我需要编写一个将SSH连接到远程主机的脚本,然后在该远程主机上运行某些命令,然后退出。 If I just do 如果我这样做

ssh $host

#some command

the script will SSH in, wait until I manually exit, then run the commands. 该脚本将SSH进入,等到我手动退出,然后运行命令。

How do I run those commands on the remote host? 如何在远程主机上运行这些命令?

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM