russH3 has asked for the wisdom of the Perl Monks concerning the following question:

Thanks for reading: I am writing a script to log in to work via our VPN. I use a system() call to execute the login. I am new to perl (and programming), so this may be a silly way to do it, but it works and the script runs well.

The problem I am having is that I am prompted after the connection is complete and asked if I "wish to continue" and of course I do, but I don't know how to code it and the program just hangs there.

I was fortunate enough to get some direction towards Expect from Intrepid (thanks) but I am still fighting a losing battle. Thanks for anyone who can help.

Replies are listed 'Best First'.
Re: yes/no prompt
by madbombX (Hermit) on Sep 04, 2006 at 04:38 UTC
    You need to provide us with more information. Where are you getting stuck? Is it a problem with Expect? If so, what does the error look like? What does the code look like where its stopping you? Please take a look at How do I post a question effectively?. It would help us to better be able to help you.

    Eric

Re: yes/no prompt
by russH3 (Initiate) on Sep 04, 2006 at 04:55 UTC
    Thanks Eric. I have been trying to get Expect to work, but again, my knowledge is weak at best. I have not worked Expect into the script yet. I left the question general because I was looking for a general answer.

    What I would like to know is if Expect is the route I should take or if there is another way. I am using system to start up the VPN client:
    $login="vpnclient.exe connect \"VPN NAME\" user \"username\" pwd \"pas +sword\" "; system ("exit"); system ($login);
    After the connection is secure, I get a "Do you wish to continue prompt? y/n." This is probably not the best way to do it. It is simple and amateur, but jumping into Expect is pretty far above my head right now. If Expect is the only way, then I will dig in and read up, but I was looking for options on how else I can run the vpn client or get past the y/n prompt.
      Expect is the way to go in my opinion (based on the little information that you provided. I would dig in and get it working with that. Unless someone has already written a PM that works with the VPN client that you are using.

      Eric

      Expect's interface is a little weird, but once you get it working it should be pretty easy. Something like the following should work:

      use Expect; system('exit'); my $exp = Expect->new; $exp->spawn( 'vpnclient.exe connect "VPN NAME" user "username" pwd "password"' ); $exp->expect( 10, [ qr/Do you wish to continue/i => sub { $exp->send('y'); $exp->exp_continue; } ] );
Re: yes/no prompt
by zentara (Cardinal) on Sep 04, 2006 at 12:53 UTC
    As an alternative to Expect, you could try IPC::Open3 (or IPC::Run or any of them), to run your script in an "expect like manner".

    Check out the faq entry

    8.25: How can I capture STDERR from an external command?

    You can also use the Searchbox and search for "IPC".

    A super-simplified barebones example is below. IPC::OPen3 may not work in your particular case, and there can be many reasons why. In a real situation, you need to process the STDOUT and STDERR outputs with alot of regex's, then determine what reply to send to STDIN.

    #!/usr/bin/perl use warnings; use strict; use IPC::Open3; #interface to "bc" calculator #my $pid = open3(\*WRITE, \*READ, \*ERROR,"bc"); my $pid = open3(\*WRITE, \*READ,0,"bc"); #if \*ERROR is false, STDERR is sent to STDOUT while(1){ print "Enter expression for bc, i.e. 2 + 2\n"; chomp(my $query = <STDIN>); #send query to bc print WRITE "$query\n"; #give bc time to output select(undef,undef,undef,.5); #get the answer from bc chomp(my $answer = <READ>); print "$query = $answer\n"; } waitpid($pid, 1); # It is important to waitpid on your child process, # otherwise zombies could be created.

    I'm not really a human, but I play one on earth. Cogito ergo sum a bum
Re: yes/no prompt
by russH3 (Initiate) on Sep 05, 2006 at 02:42 UTC
    Thanks to everyone who helped. Having a couple of options gives me some room. I will give it a shot and let you know how it turned out.