Linux之——自定义脚本实现在集群中所有的主机上执行相同的命令

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/l1028386804/article/details/101613862

注意:需要提前配置好集群中每台服务器的主机名和IP地址的对应关系,能够互相使用主机名进行通信,并配置了SSH免密码登录。

xcall脚本文件内容如下:

#!/bin/bash
pcount=$#
if (( pcount<1 )) ; then
	echo no args;
	exit;
fi
#先在本机上执行命令
echo ------------binghe$host-----------------
$@
#循环在集群中的远程节点上执行命令
for (( host=100 ; host<=105; host=host+1)) ; do
	echo ------------binghe$host-----------------
	ssh binghe$host $@
done;

接下来,为xcall脚本赋予可执行权限,如下所示。

chmod a+x ./xcall

使用格式如下:

./xcall 在服务器上执行的完整命令

使用示例:
1.在集群中的每台服务器的/home目录下创建hello.txt文件,内容为hello world

./xcall echo "hello world" >> /home/hello.txt

2.查看集群中每台服务器上hello.txt文件的内容

./xcall cat /home/hello.txt

3.删除集群中每台服务器上的hello.txt文件

./xcall rm -rf /home/hello.txt

猜你喜欢

转载自blog.csdn.net/l1028386804/article/details/101613862