相关推荐recommended
UbuntuWSL下生产密钥脚本
作者:mmseoamin日期:2024-02-24

说明:

     有时候需要为开发人员配发密钥,为方便写了个小脚本,在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