集群管理脚本


虚拟机集群管理脚本

文章目录

一、远程调用脚本(remote_call.sh)

如果有传命令参数,则执行该命令;如果没有传命令参数,则不执行。

shell 复制代码
#!/bin/bash

cmd=$1
if [ ! $cmd ];then
        cmd="jps"
fi

# 提取集群免密通信的虚拟机主机名
hosts=`sed -n '3,$p' /etc/hosts | awk '{print $2}'`

# 遍历所有主机
for host in $hosts;do
        echo "-------- $host --------"
        # 此处使用"$cmd"的原因是避免将命令中的空格识别为多条命令
        ssh root@$host "$cmd"
done
二、远程复制目录脚本(remote_copy.sh)

首先,要验证待复制的目录在本机是否存在;然后需要从/etc/hosts文件中获取除去当前主机名的其他主机名,并且对每个主机进行循环操作,先判断父目录是否存在,再进行递归复制。

shell 复制代码
#!/bin/bash

path=$1 # 本机目录

# 验证路径是否存在
if [ ! -e $path ];then
        echo "目录 $path 不存在,无法拷贝"
        exit 0
fi

# 提取集群中排除当前主机名之外的所有主机名
# 只需验证父目录是否存在即可
me=`hostname`
parent=`dirname $path`
hosts=`sed -n '3,$p' /etc/hosts | awk '{print $2}' | grep -v "$me"`

for host in $hosts;do
        scp -r $path root@$host:$parent
        echo "-------- 拷贝 $path 到 $host 成功 --------"
done
相关推荐
多弗朗皮卡丘12 小时前
网络基础:数据链路层与其他协议或技术
linux·网络·tcp/ip
时间的拾荒人18 小时前
Redis Cluster 集群详解:分片算法、搭建、故障处理与扩容实战
数据库·redis·算法·集群
dingdingfish18 小时前
TLPI 第52章 读书笔记:POSIX Message Queues
linux·mq·ipc·posix·message·tlpi
wacsii19 小时前
【Linux进程(五)】进程控制全解析:从创建、终止、等待到程序替换
linux·服务器
七夜zippoe20 小时前
Nexent Linux 部署:用 deploy.sh
linux·服务器·docker·部署·ai智能体·nexent
去码头整点薯条9820 小时前
NFS服务器
linux·运维·服务器
蜗牛一步步往上爬21 小时前
第一章 初识Linux shell
linux·运维·服务器
拂拉氏21 小时前
【知识讲解】 Linux线程中锁的认识
linux·互斥锁
无足鸟ICT21 小时前
【RHCA+】系统调优配置文件
linux·服务器