![](/img/trans.png)
[英]how to write a bash shell script to ssh to remote machine and change user and export a env variable and do other commands
[英]How to do ssh in a bash script with lot of commands?
我试图在bash脚本中运行以下命令,但是直到调用./install.sh rereplica为止。 其余命令完全不被调用。
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"
如果必须使用SSH执行复杂的脚本,通常会在本地编写脚本,使用SSH将其复制到目标计算机上,然后在其中执行:
scp foo.sh $node2user@$node2:
ssh $node2user@$node2 "bash ./foo.sh"
这样,我可以简单地通过使用bash -x
调用脚本来调试脚本,并且可以使用BASH的全部功能。
或者,您可以使用
ssh $node2user@$node2 "set +x; cd /tmp; ..."
您可以在标准输入上为ssh
提供脚本:
ssh $node2user@$node2 < my_script.sh
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.