[英]bash script to ssh and run commands
我想SSH到一个节点并在那里运行命令,然后退出。 对于所有点头重复此步骤。 脚本非常简单
#!/bin/bash
NODES="compute-0-0 compute-0-1 compute-0-2 compute-0-3"
for i in $NODES
do
ssh $i
ls -l /share/apps
rpm -ivh /share/apps/file.rpm
exit
done
但是问题是,在ssh之后, ls -l
命令丢失了。 因此,命令提示符等待输入!
有什么办法解决吗?
更新:
我将循环体修改为
ssh $i <<END
ls -l /share/apps
exit
END
但是我明白了
./lst.sh: line 9: warning: here-document at line 5 delimited by end-of-file (wanted `END')
./lst.sh: line 10: syntax error: unexpected end of file
我将更改脚本,然后将ssh
命令与要执行的命令一起运行。
例如:
#!/bin/bash
NODES="compute-0-0 compute-0-1 compute-0-2 compute-0-3"
for i in $NODES
do
ssh $i "ls -l /share/apps && rpm -ivh /share/apps/file.rpm && exit"
done
&&
运算符表示仅在上一个命令成功后才执行每个命令。
如果要独立运行命令,可以将&&
运算符更改为;
代替。
尝试这个
#!/bin/bash
NODES="compute-0-0 compute-0-1 compute-0-2 compute-0-3"
for i in $NODES
do
ssh $i "ls -l /share/apps;rpm -ivh /share/apps/file.rpm;exit;"
done
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.