如何同时操作三台服务器

通过pycharm 的SSH terminal 操作三台主机,想输出一条命令在3同机器上同步执行

方法一,写个脚本
1,首先使用 ssh-keygen 生成密钥文件。
2,ssh-copy-id -i xxx.pub HOST 把公钥文件拷贝到指定的HOST
3,当前机器创建一个hostlist文件,写入机器地址(xxx.xxx.xxx.xxx)
4,在当前机器写个脚本(注意脚本中的路径要改一下)

#!/bin/bash
run_cmd () 
{ 
    local hostlist=`cat /opt/k8s/work/hostlist | awk '{ print $1 }'`;
    for host in $hostlist;
    do
        echo "=============="$host"=============="
        ssh -o StrictHostKeyChecking=no -o ConnectTimeout=2  "$host" $@  ;
    done
}
run_cmd $@

5 ,增加文件的执行权限
6,执行命令./run_cmd date

————————————————
原文链 https://blog.csdn.net/weixin_42165490/article/details/112652257

方法二,还没尝试
使用 ansible

猜你喜欢

转载自blog.csdn.net/weixin_44831720/article/details/125314314