Ubuntu/WSL下生产密钥脚本

说明:

     有时候需要为开发人员配发密钥,为方便写了个小脚本,在linux下运行,要求 python10, putty-tools。

     使用时,在staffList定义用户列表,运行后程序自动产生对应目录及密钥。

安装:

apt install putty-tools

脚本:

#!/usr/bin/env python

import subprocess
import sys
import os


@staticmethod
def __external_cmd(cmd, code="utf8"):
    print(cmd)
    process = subprocess.Popen(cmd, shell=True, stdin=subprocess.PIPE,
                               stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
    while process.poll() is None:
        line = process.stdout.readline()
        line = line.strip()
        if line:
            print(line.decode(code, 'ignore'))


staffList = {'developer1','developer2',
             }
keyname = 'id_rsa'
alg = 'rsa'

for name in staffList:
    if not os.path.exists(f"./{name}"):
        os.makedirs(f"./{name}")
    print(name + " 密钥产生中:")
    cmd1 = f"ssh-keygen -t ed25519 -f {name}/id_rsa"
    # cmd1 = f"ssh-keygen -t rsa -f {name}/id_rsa"
    __external_cmd(cmd1)
    cmd2 = f"puttygen {name}/id_rsa -o {name}/id_rsa.ppk -O private"
    __external_cmd(cmd2)

运行结果

root@ZS-HOME-MH001:/auths/StaffKeys# python generate_keys.py
developer2 密钥产生中:
ssh-keygen -t ed25519 -f developer2/id_rsa
Generating public/private ed25519 key pair.
ssh_askpass: exec(/usr/bin/ssh-askpass): No such file or directory
ssh_askpass: exec(/usr/bin/ssh-askpass): No such file or directory
Your identification has been saved in developer2/id_rsa
Your public key has been saved in developer2/id_rsa.pub
The key fingerprint is:
SHA256:fvcJwyhUaUYRw7fH/GjPmeeTWoqiyZGA322y0mJI4fw root@ZS-HOME-MH001
The key's randomart image is:
+--[ED25519 256]--+
|         .=o     |
|         ..o.    |
|          =. +   |
|   ..    +  . +  |
|  o...  S    . o |
|   +. ooo  o  o .|
|  . o..=ooo =. ++|
|   . E..*+ ..+o*+|
|    . o=. .. o+.+|
+----[SHA256]-----+
puttygen developer2/id_rsa -o developer2/id_rsa.ppk -O private
developer1 密钥产生中:
ssh-keygen -t ed25519 -f developer1/id_rsa
Generating public/private ed25519 key pair.
ssh_askpass: exec(/usr/bin/ssh-askpass): No such file or directory
ssh_askpass: exec(/usr/bin/ssh-askpass): No such file or directory
Your identification has been saved in developer1/id_rsa
Your public key has been saved in developer1/id_rsa.pub
The key fingerprint is:
SHA256:Lrbn7A9Qat6yKalfGMPMdejKgj2llJnHhSAP2P4ofAI root@ZS-HOME-MH001
The key's randomart image is:
+--[ED25519 256]--+
|+..              |
|.+.. . .         |
| .. . + o        |
|E .O + +         |
|o =oX = S        |
|.*.*.B +         |
|..B +.* +        |
|   ooo B..       |
|  .o..+++..      |
+----[SHA256]-----+
puttygen developer1/id_rsa -o developer1/id_rsa.ppk -O private

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

bennybi

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值