2

I am trying to automate my vpn using a shell scipt

What I want is to get a webpage (curl) and parse it for password. If I write a normal script looks something like:

var=$(curl -i http://www.vpnbook.com/freevpn | grep "Password: <strong>*")
echo ${var:26:8}

This outputs a password.

Now, I want to parse this via expect, store it, and then run the openvpn command:

spawn sudo openvpn --config vpnbook-euro2-tcp443.ovpn
expect "Enter Auth Username:"
send "vpnbook"
expect "Enter Password: "
send $pass

I am unable to set this pass variable properly.

Any Ideas?

2

You can use the environment to store the value, and use expect's env array to retrieve it:

var=$(curl -i http://www.vpnbook.com/freevpn | grep "Password: <strong>*")
export passwd=${var:26:8}
expect -c '
    spawn sudo openvpn --config vpnbook-euro2-tcp443.ovpn
    expect "Enter Auth Username:"
    send "vpnbook"
    expect "Enter Password: "
    send $env(passwd)
    interact
'
1
  • Worked perfectly! – xyz Oct 29 '13 at 8:21
0

I was trying to do this same thing for my vpnbook automatic connection. I've created my version using the other answer plus downloading all the needed files and keeping them up to date. It's now a one button script that connects to vpn book.

#!/bin/bash

rm vpnbook*
rm VPNBook*
curl -O http://www.vpnbook.com/free-openvpn-account/VPNBook.com-OpenVPN-US1.zip
unzip VPNBook.com-OpenVPN-US1.zip
var=$(curl -i http://www.vpnbook.com/freevpn | grep "Password: <strong>*")
export passwd=${var:26:8}
expect -c '
    spawn sudo openvpn --config vpnbook-us1-tcp80.ovpn
    expect "Enter Auth Username:"
    send "vpnbook\r"
    expect "Enter Password: "
    send "$env(passwd)\r"
    interact
'

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.