bash – 使用easy rsa,如何自动化客户端/服务器创建过程
我使用easy rsa(
https://github.com/OpenVPN/easy-rsa)来构建我的小ca及其客户端/服务器.由于它仅用于学习目的,我正在考虑进行一些自动化.通常我可以创建客户端输入所需的文件:
./easyrsa build-client-full client 但当我需要~500个不同的客户端文件时呢?这不是我想要的. 我试着写一个小的bash脚本来帮助自己: #!/bin/bash clients=3 pass=randompass capass=castrongpassword for i in `seq 1 $clients` do ./easyrsa build-client-full test$i $pass $pass $capass done 但它只是“不起作用”.当我在终端中构建我的客户端文件时,我必须提供2个客户端密码和ca密码.主要问题是我不知道在脚本中如何传递它 – 这可能吗?我的脚本让我忽略未知的命令选项:作为输出. 解决方法
通过easyrsa源代码阅读,似乎build-client-full命令只接受一个参数:name.因此,您获得的错误仅仅是因为它不知道如何处理您在命令行上添加的密码.我想程序会以交互方式询问您这些密码,因此您必须通过stdin将它们提供给程序.
黑客easyrsa 注意:OpenSSL(easyrsa的后端)不会监听stdin 为了能够使用下面的替代方法,将其添加到gen_req中 gen_req() { .... local opts= opts="-passout stdin" # ADD THIS LINE # Alternatively: opts="-passout file:passfile" .... } 此外,似乎OpenSSL在第一个之后关闭了stdin流 sign_req() { local crt_type="$1" opts= opts="-passin file:capassfile" # ADD THIS LINE (also: create capassfile) ... } (注意:以下解决方案主要是为了将来参考,如果不涉及多次调用OpenSSL ……) “一般解决方案”#1:管道 通过stdin传递密码的一种方法是启动子shell,然后将其传递给easyrsa,这将模拟您手动完成的实际按键.您的示例将如下所示: #!/bin/bash clients=3 pass=randompass #capass=castrongpassword # (create the file 'capassfile' instead) for i in `seq 1 $clients` do (echo $pass; echo $pass) | ./easyrsa build-client-full test$i done 您可以将(echo $pass; echo …)更改为,而不是创建子shell printf '%sn' $pass $pass | ./easyrsa build-client-full test$i 要么 echo -e "$passn$pass" | ./easyrsa build-client-full test$i 取决于你发现的最具可读性. “通用解决方案”#2:stdin重定向 上述替代方案的缺点是密码将出现在进程列表(例如ps)中,因此从安全角度来看,如果您在共享框中则是个坏主意.更好的方法是使用密码创建一个文件,如下所示: randompass randompass 然后,您只需编写以下内容即可在上面的循环中调用easyrsa: ... for i in `seq 1 $clients` do ./easyrsa build-client-full test$i <passfile done 其中passfile是包含密码的文件.当然,这假设您拥有所有文件的相同密码. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |