集群管理脚本


虚拟机集群管理脚本

文章目录

一、远程调用脚本(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
相关推荐
Linux-lucky3 小时前
21-Linux学习之旅之HTTPS和安全加固
linux·运维·学习·ubuntu·https
清水白石0083 小时前
Python 死锁排查全攻略:从线程卡死到锁依赖定位与工程化修复
linux·网络·python
ao-weilai5 小时前
Linux基础:Ext系列文件系统
linux·运维·服务器
qq_349447955 小时前
Linux系统,安装git,从使用git下载仓库(GitHub, Gitee, GitLab 等),并且使用ssh密钥,可以直接执行git pull
linux·git·ssh
mCell8 小时前
GitHub Actions 玩法大赏:一台远程主机的七种活法
linux·github·agent
盐焗鹌鹑蛋8 小时前
【Linux】动静态库的制作与使用
linux
艾伦_耶格宇9 小时前
【ELK】-4 logstash的搭建及操作
linux·运维·ubuntu·elk
mifengxing10 小时前
文件逻辑结构、物理结构与磁盘空闲存储空间管理
大数据·linux·运维·操作系统·计算机408
zhangrelay10 小时前
ROS 2 Lyrical 第9章 计算机视觉与摄像头应用
linux·笔记·学习·ubuntu·机器人·ros2