Due to my laziness, I have written an extremely "messy" series of scripts in order to auto-initiate my openvpn. The configuration file I am using comes vpnbook.com/freevpn.

To get the password I use:

lynx --dump --nolist vpnbook.com/freevpn | grep -i password | sort -u | cut -b 18,19,20,21,22,23,24

The password is returned from the website.

Then, I use an expect script to automatically login (the user name is always vpnbook, but the password changes depending on the week):

#!/usr/bin/expect -f
spawn openvpn /vpn/vpnbook-ca1-tcp80.ovpn ### my vpn configuration file ###
expect "*?sername:*"
send -- "vpnbook\r"
expect "*?assword:*"
### This next line sends the password that changes by the week, which I...
###...unfortunately need to update manually (for lack of a better method):
send -- "weekly-password\r" 

The problems I am running into when attempting to automatically update the password:

1) I can't call lynx directly from the expect environment.

2) Since the password changes, I am not certain how to replace the the unique pass-phrase from the previous week with the updated version in :

send -- "unique-previous-password\r" 

3) I am not certain how use the string output from the lynx function as an input variable for editing the password from the previous week (located in my expect script).

Quite clearly, I am not "the brightest" programmer (nor am I the most efficient). However, at the end of the day, my only goal is to fully initialize my vpn by typing a single command (as I mentioned before, I am lazy).

Any help would be appreciated, thanks!


In your expect/tcl script, you can use:

send "$env(PASSWORD)\r"

And call your expect script with with:

PASSWORD=$(elinks -dump...) /path/to/your/expect/script

Note that you can use cut -b18-24 for short.

  • Thank you for your time -- This was a fantastic (not to mention simple) solution to my problem. – AQuestion Jun 14 '17 at 22:05

Stéphane's solution is very elegant, but in case you don't want to download and parse the password every time, you can retain your two step process with something like this:

Modify your password retrieval command to this:

sed -e '$ d' -i .bak f && echo -n "send -- \"" >> f && lynx --dump --nolist vpnbook.com/freevpn | grep -i password | sort -u | cut -b 18-24 >> f && echo "\\r\"" >> f

For brevity, I've called your expect script file f. In reality it needs to be the complete /path/to/your/expect/script unless you're already in the same directory as the script.

Then call your expect script as usual.

Broken down, the extended command works like this:

  • sed -e '$ d' -i .bak f Find the last line ($) in f and delete it (d). Careful of the -e and -i switches - they are different/not supported on some platforms. -e explicitly specifies the command when the -i switch is in use, and -i means to modify the file in-place, saving a backup with the .bak extension.

  • && execute the next command if the previous one succeeds.

  • echo -n "send -- \"" >> f Append to the file (>> f) without a newline (-n) the first part of the send expect function.

  • lynx... Your original command, except the result is appended to the file (>> f).

  • echo "\\r\"" >> f Append the last part of the send expect function.

  • I would enthusiastically "up-vote" your answer [ if only I had more than 15 reputation]. The thought processes / techniques presented your solution are extremely useful (not to mention, highly applicable to a variety of "slightly different" problems), and I am grateful for the opportunity to add them to my "repertoire." Thank you very much for your time and expertise. – AQuestion Jun 14 '17 at 22:34

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.