将 openssl 输入通过管道读取到 bash 脚本

将 openssl 输入通过管道读取到 bash 脚本

我需要将 bash 脚本的输入传递给我在脚本内运行的命令。我不知道该怎么做。我一整天都在寻找类似的东西bash

#!/bin/env bash

# This script is used for generating ssl cert's for websites
# ==========================================================
# Version 1.0
# ==========================================================

# Working dir
# ===========
cd "$(dirname "$0")"

# Debugging
# =========
set -x

# Imput options
# =============
read -p "Domain name: " domain_name;
read -p "Enter password: " pass;


# Verify if there is imput "conditional expressions"
# man test; help [[
# ==================================================
if [[ $domain_name ]]; then
  openssl genrsa -aes256 -out root/ca/intermediate/private/${domain_name}.key.pem 2048
  chmod 400 root/ca/intermediate/private/${domain_name}.key.pem
  openssl req -config root/ca/intermediate/openssl.cnf -key root/ca/intermediate/private/${domain_name}.key.pem -new -sha256 -out root/ca/intermediate/csr/${domain_name}.csr.pem
  openssl ca -config root/ca/intermediate/openssl.cnf -extensions server_cert -days 475 -notext -md sha256 -in root/ca/intermediate/csr/${domain_name}.csr.pem -out root/ca/intermediate/certs/${domain_name}.cert.pem
  chmod 444 root/ca/intermediate/certs/${domain_name}.cert.pem
else
  echo "Insert a domain name."
fi
if openssl x509 -noout -text -in intermediate/certs/${domain_name}.cert.pem; then
  openssl verify -CAfile root/ca/intermediate/certs/ca-chain.cert.pem intermediate/certs/${domain_name}.cert.pem
fi

实际上,我需要脚本不会失败并自动创建ssl自签名证书。

我需要传递的输入是:password, domain name

答案1

你可以在 bash 中使用类似的方法来做到这一点:

#!/bin/bash
domain="domain.com"
pass="somethingCompleX"
your_script.sh <<EOF
$domain
$password
EOF

与“your_script.sh”,您的问题中给出的脚本。

此语法允许在脚本的输入中传递一些字符串。第一个<<EOF给出“文件结束”标记,表示您要传递给脚本的字符串的结尾。之后的所有字符都将传递给您的脚本,因此

your_script.sh <<EOF
foo
bar
EOF

等于

$ your_script.sh
Domain name: foo
password: bar

你也可以参考这个在 stackoverflow 上回答

相关内容