Friday, 14 August 2009

vpnc timeout problem solved with Perl

Or ... EXPECT PAIN!!!

I've seen lots of people complaining about a time out problem with vpnc. This happens to me even if I'm ping-ing like crazy or not, so its nothing to do with activity on the connection (I think).

The problem with automatically re-connecting was the need to interactively type a password at prompt. Note that this was not my "Xauth password". For some reason my organisation requires an extra password (there are many ways a VPN can be configured).

If you just need to type one username and password, its probable that you can set up your config file to contain:

Xauth username meUserName
Xauth password meSekretPassword


With the above you could just automatically restart vpnc every time it drops out (i.e. using bash). I have this extra password to deal with though, so its a bit harder. Here is the solution achieved using Perl, Expect [1], and a lot of help from the friendly mst in irc://irc.freenode.net/#Perl

  1. use Expect;
  2. while(1){
  3. $e=Expect->new("vpnc", ("--no-detach", "/home/me/my.VPN.conf"));
  4. $e->debug(1);
  5. $e->log_file("log");
  6. if($e->expect(15, "-re", "Password for VPN")){
  7. $e->send("sekret pwd\n");
  8. $e->interact
  9. }
  10. else{
  11. die "fail!!!\n"
  12. }
  13. print "woop\n";
  14. }


[1] http://search.cpan.org/~rgiersig/Expect-1.21/Expect.pod


Take care,
Dan.