从PHP(变量)运行bash交互式脚本(带输入)

时间:2020-02-14 15:49:54

标签: php bash shell

我的服务器上有一个bash脚本(.sh),并且**向用户询问一些问题,然后完成他的工作。

脚本:

function newClient () {
echo ""
echo "Tell me a name for the client."
echo "Use one word only, no special characters."
until [[ "$CLIENT" =~ ^[a-zA-Z0-9_]+$ ]]; do
    read -rp "Client name: " -e CLIENT
done
echo ""
echo "Do you want to protect the configuration file with a password?"
echo "(e.g. encrypt the private key with a password)"
echo "   1) Add a passwordless client"
echo "   2) Use a password for the client"
until [[ "$PASS" =~ ^[1-2]$ ]]; do
    read -rp "Select an option [1-2]: " -e -i 1 PASS
done
cd /etc/openvpn/easy-rsa/ || return
case $PASS in
    1)
        ./easyrsa build-client-full "$CLIENT" nopass
    ;;
    2)
    echo "⚠️ You will be asked for the client password below ⚠️"
        ./easyrsa build-client-full "$CLIENT"
    ;;
esac
# Home directory of the user, where the client configuration (.ovpn) will be written
if [ -e "/home/$CLIENT" ]; then  # if $1 is a user name
    homeDir="/home/$CLIENT"
elif [ "${SUDO_USER}" ]; then # if not, use SUDO_USER
    homeDir="/home/${SUDO_USER}"
else # if not SUDO_USER, use /root
    homeDir="/root"
fi
# Determine if we use tls-auth or tls-crypt
if grep -qs "^tls-crypt" /etc/openvpn/server.conf; then
    TLS_SIG="1"
elif grep -qs "^tls-auth" /etc/openvpn/server.conf; then
    TLS_SIG="2"
fi
# Generates the custom client.ovpn
cp /etc/openvpn/client-template.txt "$homeDir/$CLIENT.ovpn"
{
    echo "<ca>"
    cat "/etc/openvpn/easy-rsa/pki/ca.crt"
    echo "</ca>"
    echo "<cert>"
    awk '/BEGIN/,/END/' "/etc/openvpn/easy-rsa/pki/issued/$CLIENT.crt"
    echo "</cert>"
    echo "<key>"
    cat "/etc/openvpn/easy-rsa/pki/private/$CLIENT.key"
    echo "</key>"
    case $TLS_SIG in
        1)
            echo "<tls-crypt>"
            cat /etc/openvpn/tls-crypt.key
            echo "</tls-crypt>"
        ;;
        2)
            echo "key-direction 1"
            echo "<tls-auth>"
            cat /etc/openvpn/tls-auth.key
            echo "</tls-auth>"
        ;;
    esac
} >> "$homeDir/$CLIENT.ovpn"
echo ""
echo "Client $CLIENT added, the configuration file is available at $homeDir/$CLIENT.ovpn."
echo "Download the .ovpn file and import it in your OpenVPN client."
exit 0
}

我想从PHP或另一个bash脚本运行此脚本,但是我想将PHP形式的变量传递给该脚本问题。

最好使用php或其他bash脚本,然后在该bash脚本上使用php?

0 个答案:

没有答案
相关问题