Skip to content

Instantly share code, notes, and snippets.

@henryhuypham
Last active March 15, 2022 12:55
Show Gist options
  • Save henryhuypham/8119979 to your computer and use it in GitHub Desktop.
Save henryhuypham/8119979 to your computer and use it in GitHub Desktop.
Bash script using Expect - providing input for bash script beforehand.
#!/bin/bash
client="client_000"
country=""
state=""
locality=""
org=""
org_unit=""
common_name=""
name=""
email=""
pass=""
company=""
sign="y"
commit="y"
usage=$(cat<<EOF
OPTIONS: \n
--client [client_000] \n
--country [] \n
--state [] \n
--locality [] \n
--org [] \n
--org_unit [] \n
--common_name [] \n
--name [] \n
--email [] \n
--pass [] \n
--company [] \n
--sign [y] \n
--commit [y] \n
EOF
)
if ! options=$(getopt -o h -l client:,country:,state:,locality:,org:,org_unit:,common_name:,name:,email:,pass:,company:,sign:,commit: -- "$@")
then
# something wrong
exit 1
fi
while [ $# -gt 0 ]
do
case $1 in
--client)
client="$2" ; shift
;;
--country)
country="$2" ; shift
;;
--state)
state="$2" ; shift
;;
--locality)
locality="$2" ; shift
;;
--org)
org="$2" ; shift
;;
--org_unit)
org_unit="$2" ; shift
;;
--common_name)
common_name="$2" ; shift
;;
--name)
name="$2" ; shift
;;
--email)
email="$2" ; shift
;;
--pass)
pass="$2" ; shift
;;
--company)
company="$2" ; shift
;;
--sign)
sign="$2" ; shift
;;
--commit)
commit="$2" ; shift
;;
-h)
echo -e $usage ; exit 0 ;;
(--) shift; break;;
(-*) echo "$0: error - unrecognized option $1" 1>&2; exit 1;;
(*) break;;
esac
shift
done
/usr/bin/expect <<EOD
spawn /etc/openvpn/easy-rsa/build-key $client
expect "Country Name"
send "$country\n"
expect "State or Province Name"
send "$state\n"
expect "Locality Name"
send "$locality\n"
expect "Organization Name"
send "$org\n"
expect "Organizational Unit Name"
send "$org_unit\n"
expect "Common Name"
send "$common_name\n"
expect "Name"
send "$name\n"
expect "Email Address"
send "$email\n"
expect "A challenge password"
send "$pass\n"
expect "An optional company name"
send "$company\n"
expect "Sign the certificate?"
send "$sign\n"
expect "certificate requests certified"
send "$commit\n"
interact
sleep 3
EOD
@clarkwang
Copy link

take a look at sexpect. you can write expect scripts with bash only.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment