Define input number with expect

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
1
down vote

favorite












I am trying to automate the deployment of a Docker container. Three prompts need to be filled in manually in order to proceed with the deployment. However, they are not to be filled in after each other. The order in which these fields appear is very odd. First off it asks for a password and a confirmation (two fields following each other). Then, the OpenVPN script does its thing and then needs an FQDN to generate a certificate I believe.



So: I'm wondering if I can tell expect to fill in value X for field X and once the input field Y appears, fill in value Y. In other words, can I tell expect to automatically recognize input prompts and fill them in accordingly?







share|improve this question






















  • I recommend Ansible.
    – Rui F Ribeiro
    Feb 7 at 11:09










  • @RuiFRibeiro Ansible introduces more challenges than the problem it's supposed to solve for this specific project. We do most of our deployment with Ansible though.
    – William Edwards
    Feb 7 at 11:12










  • I have used 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 at 11:13










  • @RuiFRibeiro I am aware.
    – William Edwards
    Feb 7 at 11:13






  • 1




    The answer to your current question is: yes, probably. Can you show examples of those prompts and values? Then someone here might be able to provide a useful answer.
    – Jeff Schaller
    Feb 7 at 11:35














up vote
1
down vote

favorite












I am trying to automate the deployment of a Docker container. Three prompts need to be filled in manually in order to proceed with the deployment. However, they are not to be filled in after each other. The order in which these fields appear is very odd. First off it asks for a password and a confirmation (two fields following each other). Then, the OpenVPN script does its thing and then needs an FQDN to generate a certificate I believe.



So: I'm wondering if I can tell expect to fill in value X for field X and once the input field Y appears, fill in value Y. In other words, can I tell expect to automatically recognize input prompts and fill them in accordingly?







share|improve this question






















  • I recommend Ansible.
    – Rui F Ribeiro
    Feb 7 at 11:09










  • @RuiFRibeiro Ansible introduces more challenges than the problem it's supposed to solve for this specific project. We do most of our deployment with Ansible though.
    – William Edwards
    Feb 7 at 11:12










  • I have used 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 at 11:13










  • @RuiFRibeiro I am aware.
    – William Edwards
    Feb 7 at 11:13






  • 1




    The answer to your current question is: yes, probably. Can you show examples of those prompts and values? Then someone here might be able to provide a useful answer.
    – Jeff Schaller
    Feb 7 at 11:35












up vote
1
down vote

favorite









up vote
1
down vote

favorite











I am trying to automate the deployment of a Docker container. Three prompts need to be filled in manually in order to proceed with the deployment. However, they are not to be filled in after each other. The order in which these fields appear is very odd. First off it asks for a password and a confirmation (two fields following each other). Then, the OpenVPN script does its thing and then needs an FQDN to generate a certificate I believe.



So: I'm wondering if I can tell expect to fill in value X for field X and once the input field Y appears, fill in value Y. In other words, can I tell expect to automatically recognize input prompts and fill them in accordingly?







share|improve this question














I am trying to automate the deployment of a Docker container. Three prompts need to be filled in manually in order to proceed with the deployment. However, they are not to be filled in after each other. The order in which these fields appear is very odd. First off it asks for a password and a confirmation (two fields following each other). Then, the OpenVPN script does its thing and then needs an FQDN to generate a certificate I believe.



So: I'm wondering if I can tell expect to fill in value X for field X and once the input field Y appears, fill in value Y. In other words, can I tell expect to automatically recognize input prompts and fill them in accordingly?









share|improve this question













share|improve this question




share|improve this question








edited Feb 7 at 12:49









Vlastimil

6,3711146119




6,3711146119










asked Feb 7 at 11:06









William Edwards

3211420




3211420











  • I recommend Ansible.
    – Rui F Ribeiro
    Feb 7 at 11:09










  • @RuiFRibeiro Ansible introduces more challenges than the problem it's supposed to solve for this specific project. We do most of our deployment with Ansible though.
    – William Edwards
    Feb 7 at 11:12










  • I have used 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 at 11:13










  • @RuiFRibeiro I am aware.
    – William Edwards
    Feb 7 at 11:13






  • 1




    The answer to your current question is: yes, probably. Can you show examples of those prompts and values? Then someone here might be able to provide a useful answer.
    – Jeff Schaller
    Feb 7 at 11:35
















  • I recommend Ansible.
    – Rui F Ribeiro
    Feb 7 at 11:09










  • @RuiFRibeiro Ansible introduces more challenges than the problem it's supposed to solve for this specific project. We do most of our deployment with Ansible though.
    – William Edwards
    Feb 7 at 11:12










  • I have used 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 at 11:13










  • @RuiFRibeiro I am aware.
    – William Edwards
    Feb 7 at 11:13






  • 1




    The answer to your current question is: yes, probably. Can you show examples of those prompts and values? Then someone here might be able to provide a useful answer.
    – Jeff Schaller
    Feb 7 at 11:35















I recommend Ansible.
– Rui F Ribeiro
Feb 7 at 11:09




I recommend Ansible.
– Rui F Ribeiro
Feb 7 at 11:09












@RuiFRibeiro Ansible introduces more challenges than the problem it's supposed to solve for this specific project. We do most of our deployment with Ansible though.
– William Edwards
Feb 7 at 11:12




@RuiFRibeiro Ansible introduces more challenges than the problem it's supposed to solve for this specific project. We do most of our deployment with Ansible though.
– William Edwards
Feb 7 at 11:12












I have used 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 at 11:13




I have used 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 at 11:13












@RuiFRibeiro I am aware.
– William Edwards
Feb 7 at 11:13




@RuiFRibeiro I am aware.
– William Edwards
Feb 7 at 11:13




1




1




The answer to your current question is: yes, probably. Can you show examples of those prompts and values? Then someone here might be able to provide a useful answer.
– Jeff Schaller
Feb 7 at 11:35




The answer to your current question is: yes, probably. Can you show examples of those prompts and values? Then someone here might be able to provide a useful answer.
– Jeff Schaller
Feb 7 at 11:35










1 Answer
1






active

oldest

votes

















up vote
2
down vote



accepted










Certainly, this is what expect is for. The exp_continue command is the key here:



expect 
"password: " send "$passwordr"; exp_continue
"confirmation" send "$confirmr"; exp_continue
"FQDN" send "$fqdnr"; 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 "secretr"
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





share|improve this answer






















  • The code (second block) doesn't work. Variables aren't getting passed properly I suppose? no such variable (read trace on "env(hostname)") invoked from within
    – William Edwards
    Feb 7 at 19:27











  • did you export the shell variable hostname?
    – glenn jackman
    Feb 7 at 20:05










  • Yes. The certificate is not generated successfully: Options error: --dh fails with '/etc/openvpn/pki/dh.pem': No such file or directory (errno=2)
    – William Edwards
    Feb 7 at 20:12










  • I rewrote the code slightly and it seems to work now. Hold on.
    – William Edwards
    Feb 7 at 20:17










  • Getting unable to load CA private key now but I figured that can have many reasons, so I will debug further.
    – William Edwards
    Feb 7 at 20:26











Your Answer







StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);








 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f422515%2fdefine-input-number-with-expect%23new-answer', 'question_page');

);

Post as a guest






























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
2
down vote



accepted










Certainly, this is what expect is for. The exp_continue command is the key here:



expect 
"password: " send "$passwordr"; exp_continue
"confirmation" send "$confirmr"; exp_continue
"FQDN" send "$fqdnr"; 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 "secretr"
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





share|improve this answer






















  • The code (second block) doesn't work. Variables aren't getting passed properly I suppose? no such variable (read trace on "env(hostname)") invoked from within
    – William Edwards
    Feb 7 at 19:27











  • did you export the shell variable hostname?
    – glenn jackman
    Feb 7 at 20:05










  • Yes. The certificate is not generated successfully: Options error: --dh fails with '/etc/openvpn/pki/dh.pem': No such file or directory (errno=2)
    – William Edwards
    Feb 7 at 20:12










  • I rewrote the code slightly and it seems to work now. Hold on.
    – William Edwards
    Feb 7 at 20:17










  • Getting unable to load CA private key now but I figured that can have many reasons, so I will debug further.
    – William Edwards
    Feb 7 at 20:26















up vote
2
down vote



accepted










Certainly, this is what expect is for. The exp_continue command is the key here:



expect 
"password: " send "$passwordr"; exp_continue
"confirmation" send "$confirmr"; exp_continue
"FQDN" send "$fqdnr"; 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 "secretr"
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





share|improve this answer






















  • The code (second block) doesn't work. Variables aren't getting passed properly I suppose? no such variable (read trace on "env(hostname)") invoked from within
    – William Edwards
    Feb 7 at 19:27











  • did you export the shell variable hostname?
    – glenn jackman
    Feb 7 at 20:05










  • Yes. The certificate is not generated successfully: Options error: --dh fails with '/etc/openvpn/pki/dh.pem': No such file or directory (errno=2)
    – William Edwards
    Feb 7 at 20:12










  • I rewrote the code slightly and it seems to work now. Hold on.
    – William Edwards
    Feb 7 at 20:17










  • Getting unable to load CA private key now but I figured that can have many reasons, so I will debug further.
    – William Edwards
    Feb 7 at 20:26













up vote
2
down vote



accepted







up vote
2
down vote



accepted






Certainly, this is what expect is for. The exp_continue command is the key here:



expect 
"password: " send "$passwordr"; exp_continue
"confirmation" send "$confirmr"; exp_continue
"FQDN" send "$fqdnr"; 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 "secretr"
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





share|improve this answer














Certainly, this is what expect is for. The exp_continue command is the key here:



expect 
"password: " send "$passwordr"; exp_continue
"confirmation" send "$confirmr"; exp_continue
"FQDN" send "$fqdnr"; 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 "secretr"
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






share|improve this answer














share|improve this answer



share|improve this answer








edited Feb 7 at 16:14

























answered Feb 7 at 13:36









glenn jackman

46.4k265102




46.4k265102











  • The code (second block) doesn't work. Variables aren't getting passed properly I suppose? no such variable (read trace on "env(hostname)") invoked from within
    – William Edwards
    Feb 7 at 19:27











  • did you export the shell variable hostname?
    – glenn jackman
    Feb 7 at 20:05










  • Yes. The certificate is not generated successfully: Options error: --dh fails with '/etc/openvpn/pki/dh.pem': No such file or directory (errno=2)
    – William Edwards
    Feb 7 at 20:12










  • I rewrote the code slightly and it seems to work now. Hold on.
    – William Edwards
    Feb 7 at 20:17










  • Getting unable to load CA private key now but I figured that can have many reasons, so I will debug further.
    – William Edwards
    Feb 7 at 20:26

















  • The code (second block) doesn't work. Variables aren't getting passed properly I suppose? no such variable (read trace on "env(hostname)") invoked from within
    – William Edwards
    Feb 7 at 19:27











  • did you export the shell variable hostname?
    – glenn jackman
    Feb 7 at 20:05










  • Yes. The certificate is not generated successfully: Options error: --dh fails with '/etc/openvpn/pki/dh.pem': No such file or directory (errno=2)
    – William Edwards
    Feb 7 at 20:12










  • I rewrote the code slightly and it seems to work now. Hold on.
    – William Edwards
    Feb 7 at 20:17










  • Getting unable to load CA private key now but I figured that can have many reasons, so I will debug further.
    – William Edwards
    Feb 7 at 20:26
















The code (second block) doesn't work. Variables aren't getting passed properly I suppose? no such variable (read trace on "env(hostname)") invoked from within
– William Edwards
Feb 7 at 19:27





The code (second block) doesn't work. Variables aren't getting passed properly I suppose? no such variable (read trace on "env(hostname)") invoked from within
– William Edwards
Feb 7 at 19:27













did you export the shell variable hostname?
– glenn jackman
Feb 7 at 20:05




did you export the shell variable hostname?
– glenn jackman
Feb 7 at 20:05












Yes. The certificate is not generated successfully: Options error: --dh fails with '/etc/openvpn/pki/dh.pem': No such file or directory (errno=2)
– William Edwards
Feb 7 at 20:12




Yes. The certificate is not generated successfully: Options error: --dh fails with '/etc/openvpn/pki/dh.pem': No such file or directory (errno=2)
– William Edwards
Feb 7 at 20:12












I rewrote the code slightly and it seems to work now. Hold on.
– William Edwards
Feb 7 at 20:17




I rewrote the code slightly and it seems to work now. Hold on.
– William Edwards
Feb 7 at 20:17












Getting unable to load CA private key now but I figured that can have many reasons, so I will debug further.
– William Edwards
Feb 7 at 20:26





Getting unable to load CA private key now but I figured that can have many reasons, so I will debug further.
– William Edwards
Feb 7 at 20:26













 

draft saved


draft discarded


























 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f422515%2fdefine-input-number-with-expect%23new-answer', 'question_page');

);

Post as a guest













































































Popular posts from this blog

How to check contact read email or not when send email to Individual?

Displaying single band from multi-band raster using QGIS

How many registers does an x86_64 CPU actually have?