linux easy-rsa 批量生成密钥 (可执行)

最近搭了一个vpn 服务,需要给70多人生成密钥,这手工一个一个做肯定不是办法,就在网上找了下相关资料,用expect 写了个脚本

在服务器 yum install expect  安装expect 命令。

规则是 账号 密码   ,密码要随机生成。

密码生成用exce 随机公式 搞定。具体如下:

=CHAR(RAND()*93+33)&RANDBETWEEN(1000,9999)&CHAR(RAND()*93+33)&CHAR(RAND()*93+33)

1位任意字母(含数字)+4位数字+1位任意字母  ,例如:y61319$

生成后拷贝到一个文本文件中,这里命令userinfo。

具体文件列表如下:

singexpect.exp  #自动处理输入框脚本

singreq.sh        #签名脚本

testbash.sh     #执行脚本

userinfo       #账号密码

 

singreq.sh  签名脚本内容 如下

#!/bin/bash
name=$1
echo "正在给用户生成证书,用户名:${name}"
mkdir -p /etc/openvpn/${name}
cd /etc/openvpn/${name}
/usr/share/easy-rsa/3/easyrsa init-pki
/usr/share/easy-rsa/3/easyrsa gen-req ${name}
#输入用户密码
cd /usr/share/easy-rsa/3
./easyrsa import-req /etc/openvpn/${name}/pki/reqs/${name}.req ${name}
./easyrsa sign-req client ${name}
#输入 签名密码
mkdir /opt/openvpn/ca/${name}
cp /usr/share/easy-rsa/3/pki/ca.crt /opt/openvpn/ca/${name}/
cp /usr/share/easy-rsa/3/pki/issued/${name}.crt /opt/openvpn/ca/${name}/
cp /etc/openvpn/${name}/pki/private/${name}.key /opt/openvpn/ca/${name}/

singexpect.exp  处理脚本如下:

#!/usr/bin/expect
set username [lindex $argv 0]
set password [lindex $argv 1]
spawn bash /root/singreq.sh $username

set timeout 30

expect {
    "PEM pass phrase"  {send "$password\r";exp_continue}
    "Verifying - Enter PEM pass"  {send "$password\r"; exp_continue}
    "Common Name" {send "$username\r";exp_continue}
    "Confirm request details" {send "yes\r";exp_continue}
    "Enter pass phrase for /usr/share/easy-rsa/3/pki/private/ca.key" {send "xxxxx\r"}
}
expect eof

最后一行 send "xxxxx"  这里需要输入自己定义的密码,密码是前期生成openvpn 服务端 ca的密码。

testbash.sh 文件内容如下:

#!/bin/bash
for name in `awk '{print $1}' userinfo`
do
    pass=`awk -v I="$name" '{if(I==$1)print $2}' userinfo`
    expect singexpect.exp  $name $pass 
    echo $pass  $serverpass
done

userinfo   文本文件这个内容涉及到保密信息,这里随机几个,作为样例

zhangsan zhangsan@g1760g5
lisi	lisi@A7758eu
wangwu	wangsu@B3428[+

 

好了 大功告成。  证书都拷贝到了 /opt/openvpn/ca/ 目录

 

如果生成错了,怎么办。  那就注销证书, 注销证书也可以写个自动脚本,但涉及到删除,这里建议手工一个一个处理。

也列一下注销的脚本和命令:

cd /usr/share/easy-rsa/3
./easyrsa revoke zhangsan
# 输入yes
#输入 生成服务的密码

rm -fr /etc/openvpn/zhangsan
rm -fr /opt/openvpn/ca/zhangsan

 

posted @ 2022-08-11 11:12  jk_tim  阅读(577)  评论(0)    收藏  举报