Certainly, this is what expect is for. The exp_continue
command is the key here:
expect {
"password: " {send "$password\r"; exp_continue}
"confirmation" {send "$confirm\r"; exp_continue}
"FQDN" {send "$fqdn\r"; exp_continue}
"some other pattern"
}
Of course you'll have to change the quoted patterns so they match your actual case.
With this structure, the patterns can match in any order. The patterns don't even have to appear at all.
When "some other pattern" is seen, since there's no action block associated with it, it triggers the end of the encompassing expect command and the rest of the script can carry on.
For expect to be able to control a process, you must launch that process from within expect. Try this:
#!/bin/bash
port=$1
export hostname=$2
export OVPN_DATA="ovpn-data-$port"
docker volume create --name $OVPN_DATA
/usr/bin/expect <<'END_EXPECT'
set timeout -1
spawn docker run -v $env(OVPN_DATA):/etc/openvpn --rm kylemanna/openvpn ovpn_genconfig -u udp://$env(hostname)
expect {
"Enter PEM pass phrase:" {
# this should match whether it's the initial or confirmation prompt
send "secret\r"
exp_continue
}
"Common Name" {
send "$env(hostname)\r"
exp_continue
}
eof
}
END_EXPECT
docker run -v $OVPN_DATA:/etc/openvpn --rm -it kylemanna/openvpn ovpn_initpki
docker run -dit --restart unless-stopped --name=$port -v $OVPN_DATA:/etc/openvpn -d -p $port:1194/udp --cap-add=NET_ADMIN kylemanna/openvpn
expect
in the past. It is a bit limited for this kind of actions, and involves a lot of babysitting over time. – Rui F Ribeiro Feb 7 '18 at 11:13