[svc]expect的爱恨情仇
背景
openvpn生成证书想把它做成一键化,这样添加新用户时候就方便
遇到的问题
我的代码
gg_vpn_keys.exp
#!/usr/bin/expect set user [lindex $argv 0] set secret [lindex $argv 1] spawn /usr/share/easy-rsa/2.0/build-key-pass $user expect { "phrase" {send "$secret\r";exp_continue} "phrase" {send "$secret\r";exp_continue} "CN" {send "\r";exp_continue} "Guangdong" {send "\r";exp_continue} "Shenzhen" {send "\r";exp_continue} "PP100" {send "\r";exp_continue} "Account" {send "\r";exp_continue} "$user" {send "\r";exp_continue} "EasyRSA" {send "\r";exp_continue} "mon@pp100.com" {send "\r";exp_continue} "password" {send "\r";exp_continue} "company name" {send "\r";exp_continue} "certificate" {send "y\r";exp_continue} "commit" {send "y\r";exp_continue} eof } #expect eof exit -onexit { send_user "general user cert already!!!\n" }
gg.sh
#!/bin/sh init_env(){ #change to root and stop openvpn and init opvpn cert sudo su - root /etc/init.d/openvpn stop cd /usr/share/easy-rsa/2.0 && source ./vars } #read opvpn username and general password read -t 20 -p 'pls input the username:' user || exit 1 secret=`openssl rand -hex 8` init_cert_bak_dir(){ #create ovpn cert_bak_dir if not exist [ -e /server/openvpn_accout_bak ] || mkdir /server/openvpn_accout_bak [ -e /server/openvpn_accout_bak/$user ] && \ exit 1 || mkdir /server/openvpn_accout_bak/$user } create_passfile(){ #write cert password to user_ovpn_dir echo $secret > /server/openvpn_accout_bak/${user}/${user}_pass_$(date +%F).txt #general cert [ -e /server/shells/gg_vpn_keys.exp ] && \ expect /server/shells/gg_vpn_keys.exp $user $secret sleep 5 #copy cert to user_ovpn_dir cp /usr/share/easy-rsa/2.0/keys/${user}* /server/openvpn_accout_bak/$user #generay gg code useradd $user && chown -R ${user}.$user /server/openvpn_accout_bak/$user $gg=`sudo su - $user -c /usr/local/bin/gg-auth.py` echo $gg >> /server/openvpn_accout_bak/${user}/${user}_pass_$(date +%F).txt #general gg qrcode cd /server/openvpn_accout_bak/${user} && qrencode -o ${user}_google_qrcode.png -t png -s 20 $gg #general client ovpn file cd /server/openvpn_accout_bak/ && cp tt100.ovpn $user/${user}.ovpn sed -i 's#tt100#${user}#g' $user/${user}.ovpn #general user.tar.gz file cd /server/openvpn_accout_bak/ && tar zcf ${user}_openvpn.tar.gz ./$user echo "good lucky" } main(){ init_env init_cert_bak_dir create_passfile } main
搞了老半天..没搞定 后面想起来在搞吧.