1

I have a small doubts because I don't know how can I prepare shell script which should execute other command with questions...

It means e.g. that I have to connect with VPN client and need to answer a several question. Accept trusting (yes/no), then choose VPN option (VPN/VPN-1), introduce login and password. I would like to have one script with all parameters (of course exclude password).

Any ideas? Thanks

Greg
  • 188
  • 13
  • 1
    Just have all your answers in a file, say `answers.txt`, in correct order and whichever command needs it, feed it like `command < answers.txt`. If you mean to supply password, then see https://stackoverflow.com/questions/4857702/how-to-provide-password-to-a-command-that-prompts-for-one-in-bash – Mihir Luthra Oct 04 '19 at 07:59
  • Ok thanks, but what can I do if I would like to skip the last question and enter the password from console? Is there any possible to do it? – Greg Oct 04 '19 at 08:18
  • 1
    use `read -s password` in you script – Pacifist Oct 04 '19 at 08:21
  • I need to introduce password manually because its token + password – Greg Oct 04 '19 at 08:21
  • Dealing with passwords is deeply discussed in the link i provided above. Although as mentioned there, its a serious security hole. Also if you want to skip some questions, just have an empty line in your `answers.txt`. – Mihir Luthra Oct 04 '19 at 08:23
  • Thanks, its working properly – Greg Nov 27 '19 at 11:25

1 Answers1

1

If the answers file works, you can avoid placing the password into the file using replacement token. For example, write 'PASSWORD in the file, and then use 'sed' (or other tool) to replace it at run time.

Possible to use 'read -s password' or other method to get the password at run-time.

read -s REAL_PASSWORD
sed -e 's/__PASSWORD__/$REAL_PASSWORD/' | command-to-setup

If the number of items in the answer file is small, and do not change, you can inline them into your script

read -s REAL_PASSWORD
command-to-setup <<__ANSWERS__
yes
VPN-1
login
$REAL_PASSWORD
__ANSWERS__
dash-o
  • 13,723
  • 1
  • 10
  • 37
  • Yes, its working thanks! :) I used the second one example. Besides how can I execute other command e.g. command-to-setup2 in parallel because sleep and '&' didn't working in my script. Of course it's not needed but I'm asking out of curiosity – Greg Oct 04 '19 at 09:50