集群管理脚本


虚拟机集群管理脚本

文章目录

一、远程调用脚本(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
相关推荐
Apipi*3 小时前
30天速通Linux 第六章信号及信号处理
linux·运维·信号处理
天启HTTP4 小时前
爬虫防封核心原理:IP轮换机制与风控规避逻辑
linux·服务器·网络·爬虫·tcp/ip
广州浮点FLOATLIC7 小时前
许可证服务器迁移后软件打不开:研发 IT 怎样定位连接问题
linux·服务器·数据库
BLUcoding7 小时前
接口服务公网超时排查记录:SecureRandom 阻塞问题分析与解决
java·linux·springboot·aes·securerandom
AIgorithmGEEK7 小时前
[Linux]从手写报头到内核套路:序列化、反序列化与自定义协议全链路
linux·运维·服务器·网络·序列化·反序列化
Nil2087 小时前
leetcode 139单词拆分
linux·运维·服务器
Starry-sky(jing)7 小时前
BUG: unable to handle kernel paging request 完整排查:dmesg 四要素与三路定罪
linux·运维·服务器·内核·排障
mounter6257 小时前
从硬件互连到操作系统变革:CXL 技术演进与 Linux 内核工程挑战
linux·运维·服务器
xh didida9 小时前
Linux -- 基础IO
linux·服务器·开发语言·c++
zly350010 小时前
centos7 mount设备挂载 /dev/sda1/硬盘挂载
linux·运维·服务器