0

I'm new in Ubuntu terminal. I have a command to connect to a VPN server. in first step I must type sudo vpn_command and so I must type root password, then in next step I must Enter "yes" and vpn username and vpn password respectively.

I want to do all these actions in one command. can you help me?

2
  • 3
    If this involves storing your password in plaintext to feed it to commands, you probably shouldn't. It would be helpful if you told us which vpn_command you are talking about. Running commands as root without password prompt is possible. Maybe your vpn_command supports parameters like -y to assume yes to all answers, and maybe it supports specifiying the username on the command line, so you don't have to provide it interactively. And finally, if your vpn provider supports key-based authentication, you wouldn't have to enter the password. – danzel Apr 17 '19 at 15:41
  • Please edit your question and provide more detailed information so we can get rid of the ifs and maybes. – danzel Apr 17 '19 at 15:42
1

This is possible. Although as mentionned in comments probably not the best idea. Please note that you have to use your own password for sudo, not root's password (that's precisely the point of running sudo). It's probably best to configure sudo to not ask a password. Also your vpn probably has an option that takes a credential filename.

The program you need is called expect. So first sudo apt-get install expect. Expect, as its name suggests, expects strings on terminal and sends back other strings. It is the perfect tool when a program prompts for answer. It's a quite complex program but here, it can be used in a very basic way :

  1. run sudo command and monitor output on terminal
  2. when you see this string, send this answer
  3. when you see this other string, send this other answer
  4. etc...

see askubuntu question 307067 or stackoverflow question 38458053 for example. Your search engine will find many examples once you know you have to ask for "expect password examples".

sudo will not always ask for your password. if you rerun a sudo command in the same terminal within 5 minutes or so, it won't ask for a password the second time. You can use expect timeout in such situations.

You could write a script called myexpectscript with such content :

#!/usr/bin/expect -d
set timeout 5

set pw {YOUR ubuntu GNU/linux P4ßw0rd}
set vpnuser {yourvpnusername}
set vpnpass {vpn-PAs$w0rd}
spawn sudo -p sudoisaskingforyourpassword vpncommand -foo -b ar etc...

expect "sudoisaskingforyourpassword" { send "$pw\n" }
expect {Do you really want to do that} { send "Yes\n" }
expect {vpn user prompt} { send "$vpnuser\n" }
expect {vpn password prompt} { send "$vpnpass\n" }
interact

make it executable with chmod 700 myexpectscript, then run ./myexpectscript. Change -d to -f to get rid of the debugging outpout. You have to adjust the timeout, the command and the various prompts and passwords.

All this is, of course, completely untested.

Last but not least : expect uses the TCL language. TCL is a very elegant and friendly language, it's just a bit picky about who its friends are. If you get errors, be prepared to read docs for a long time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.