ssh之公钥分发脚本

公钥分发脚本:

只需要修改IP、用户、密码、ssh端口 可定义

脚本地址:https://github.com/woniu336/open_shell/blob/main/pub_send.py
国内地址:https://gitee.com/dayu777/open_shell/raw/main/pub_send.py

注意:脚本会删除原来的密钥对

使用场景

你有几十台服务器,现在用跳板机(堡垒机)来统一管控,把公钥分发到每台机器上

下载脚本

wget https://gitee.com/dayu777/open_shell/raw/main/pub_send.py

编辑pub_send.py 添加多台主机

最后运行

python3 pub_send.py

脚本内容:

#!/usr/bin/env python3
import os

# 删除用户家目录下的 SSH 密钥文件
os.system('rm -rf ~/.ssh/id_rsa*')

# 生成新的 SSH 密钥对
os.system('ssh-keygen -f ~/.ssh/id_rsa -P "" >/dev/null 2>&1')

# 设置 SSH 密钥路径
key_path = os.path.expanduser('~/.ssh/id_rsa.pub')

# 定义IP地址列表、用户名、密码和端口
hosts_and_passwords = {
    "192.168.1.28": {"user": "root", "password": "123456", "port": 22},
    "192.168.1.29": {"user": "root", "password": "123456", "port": 22},
    "192.168.1.30": {"user": "root", "password": "123456", "port": 22}
}

# 循环遍历 IP 地址列表,将 SSH 公钥复制到远程主机
for host, credentials in hosts_and_passwords.items():
    user = credentials["user"]
    password = credentials["password"]
    port = credentials["port"]
    os.system(f'sshpass -p "{password}" ssh-copy-id -i {key_path} -o StrictHostKeyChecking=no -p {port} {user}@{host}')
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值