Bash向EasyRSA gen-req传递已输入的yes

3
问题: 我正在尝试编写一个EasyRSA密钥生成请求的脚本,但是,如果密钥文件已经存在,任何试图使用yesecho yes命令覆盖都会导致Aborting without confirmation。我相信EasyRSA要求我实际上输入'yes',否则会中止。

我想知道是否有另一种方法可以传递需要键入响应的提示信息中的'yes'?

我尝试过:yes | <command>yes yes | <command>echo yes | <command>

只想说这对我有效: echo yes | ./easyrsa revoke certificate.name - Tom Anderson
2个回答

1

你可以做两件事情。第一件可能不起作用,但第二件会。

第一:你可以通过以下方式执行easyrsa:

pi@raspberry$ sudo easyrsa <some command> -y

-y将强制执行yes。您也可以使用-yes或-f参数。如果这不起作用,请尝试第二种方法。
pi@raspberry$ sudo ./script.sh

脚本将包含以下内容:
cat << EOF | sudo easyrsa <some command>
yes
EOF

这一定会起作用。很高兴能帮忙 :)

请告诉我结果,这样我就可以进一步修改我的答案并帮助您。 - Usman Khan
前两个没有起作用。尝试第二个。我想,如果键已经存在,我不需要重新制作它,我可以给自己一个参数等待我的响应提示。 - Chimera.Zen
我只写了两个方法?顺便问一下,你执行了 sudo ./script 并且在制作脚本之前执行了这个命令吗? - Usman Khan
我遇到了“意外文件结尾”错误。该脚本没有使用sudo。if [ -f "pki/private/$name.key" ]; then cat << EOF | sudo ./easyrsa gen-req $name nopass yes EOF fi - Chimera.Zen
1
你需要在结束EOF时正确缩进。缩进可能会导致问题。我认为,在fi之前的最后一个EOF需要有零缩进。尝试更改其缩进并再次尝试。这将解决你的问题。 - Usman Khan
当我尝试使用easyrsa v3.1.6执行命令"easyrsa build-client-full xxx nopass -y"时,它会打印出"忽略未知的命令选项:'-y'"。 - undefined

1
这不是全部内容,但它是创建新密钥或覆盖现有密钥的部分操作。
#client common name - a string
CN=$1

# 'yes' or 'no' to overwrite or not resp.
OVERWRITE=$2 

#paths
EASYRSA_DIR="/path/to/easy-rsa/easyrsa3"
EASYRSA="./easyrsa"

if [ -f "$EASYRSA_DIR/pki/private/$CN.key" ]; then         
    cat <<-EOF  | sudo $EASYRSA gen-req $CN nopass         
    $OVERWRITE
    $CN
    EOF
else
    cat <<-EOF  | sudo $EASYRSA gen-req $CN nopass         
    $CN   
    EOF
fi

然后使用更多命令运行脚本以满足easy-rsa的要求!预期输出:

Note: using Easy-RSA configuration from: /etc/openvpn/easy-rsa/easyrsa3/vars
Using SSL: openssl OpenSSL 1.1.1h  22 Sep 2020


WARNING!!!

An existing private key was found at /etc/openvpn/easy-rsa/easyrsa3/pki/private/testuser.key
Continuing with key generation will replace this key.

Type the word 'yes' to continue, or any other input to abort.
  Confirm key overwrite: Generating a RSA private key
............................+++++
.....................+++++
writing new private key to '/etc/openvpn/easy-rsa/easyrsa3/pki/easy-rsa-12805.EocLO7/tmp.CmRehr'
-----
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Common Name (eg: your user, host, or server name) [testuser]:
Keypair and certificate request completed. Your files are:
req: /etc/openvpn/easy-rsa/easyrsa3/pki/reqs/testuser.req
key: /etc/openvpn/easy-rsa/easyrsa3/pki/private/testuser.key

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接