简体   繁体   English

如何使用很多命令在bash脚本中执行ssh?

[英]How to do ssh in a bash script with lot of commands?

I tried to run the following command in a bash script but only till ./install.sh rereplica is called . 我试图在bash脚本中运行以下命令,但是直到调用./install.sh rereplica为止。 Rest of the commands are not called at all. 其余命令完全不被调用。

  ssh $node2user@$node2 "cd /tmp; tar -xf $mmCS.tar; cd $mmCS; ./install.sh csreplica; ./install.sh keepalived $vip low;./install.sh haproxy $node1:8080 $node2:8080 $vip:8080; ./install.sh confmongo $dbPath"

If I have to do execute a complex script using SSH, I usually write a script locally, copy it on the target machine with SSH and then execute it there: 如果必须使用SSH执行复杂的脚本,通常会在本地编写脚本,使用SSH将其复制到目标计算机上,然后在其中执行:

scp foo.sh $node2user@$node2:
ssh $node2user@$node2 "bash ./foo.sh"

That way, I can debug the script simply by invoking it with bash -x and I can use the full power of BASH. 这样,我可以简单地通过使用bash -x调用脚本来调试脚本,并且可以使用BASH的全部功能。

Alternatively, you can use 或者,您可以使用

ssh $node2user@$node2 "set +x; cd /tmp; ..."

您可以在标准输入上为ssh提供脚本:

ssh $node2user@$node2 < my_script.sh

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

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