Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Perl Expect module

by da97mld (Initiate)
on Nov 19, 2003 at 07:36 UTC ( #308243=perlquestion: print w/ replies, xml ) Need Help??
da97mld has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I'm trying to write a script using the Perl Expect module and I have problems getting it to work :( Is there someone that can give me a simple example for using the expect module and creating a new dtterm where I want som basic commands to execute for example 'ls -altr' ? I.e I want to control two terminals from a perl script using the expect module. Regards, Mathias

Comment on Perl Expect module
Re: Perl Expect module
by Anonymous Monk on Nov 19, 2003 at 08:26 UTC
    Hi, I'm trying to write a script using the Perl Expect module and I have problems getting it to work :(
    Great, now where is your code?
    Is there someone that can give me a simple example for using the expect module and creating a new dtterm where I want som basic commands to execute for example 'ls -altr' ?
    The documentation already provides examples. If they're not enough, you should think about hiring somebody to write this program for you.
      I think that was a pretty arrogant and ignorant remark from the anonymous monk that replied to your last query. Perhaps if he doesn't have anything to offer he shouldn't offer anything at all. Heres an example of perl using expect (hope this helps):
      #!/usr/bin/perl use strict; use Expect; my $session = new Expect; $session->spawn("/usr/bin/bash"); print $session "cd \/home\/homedir\r"; $session->expect(90, -re, "bash"); print $session "ls -altr\r"; my @array = $session->expect(60, -re, "bash"); $session->clear_accum; my @array1 = split('\n', $array[3]); for (my $i = 0; $i <= $#array1; $i++) { print "\nElement $i is\n$array1[$i]\n"; }

      Edit: BazB, added code tags.

Re: Perl Expect module
by Anonymous Monk on Nov 19, 2003 at 17:15 UTC
    I think that was a pretty arrogant and ignorant remark from the anonymous monk that replied to your last query. Perhaps if he doesn't have anything to offer he shouldn't offer anything at all. Heres an example of perl using expect (hope this helps):
    #!/usr/bin/perl use strict; use Expect; my $session = new Expect; $session->spawn("/usr/bin/bash"); print $session "cd \/home\/homedir\r"; $session->expect(90, -re, "bash"); print $session "ls -altr\r"; my @array = $session->expect(60, -re, "bash"); $session->clear_accum; my @array1 = split('\n', $array[3]); for (my $i = 0; $i <= $#array1; $i++) { print "\nElement $i is\n$array1[$i]\n"; }
Re: Perl Expect module
by markd (Acolyte) on Nov 20, 2003 at 12:18 UTC
    Sorry about the last bit of code if you tried it and it didn't work (I just tried it now). The buffer needs to be cleared out . The code is now commented to explain what each part does. Is this what you wanted or am I completely off the wall?:
    #!/usr/bin/perl use strict; use Expect; my $session = new Expect; $session->spawn("/usr/bin/bash"); # clear buffer $session->expect(20, -re, "."); $session->clear_accum(); # cd to required dir print $session "cd \/home\/homedir\r"; $session->expect(90, -re, "bash"); # wait sleep 20; # get the results of the command and assign them to an array print $session "ls \-altr\r"; my @array = $session->expect(60, -re, "bash"); $session->clear_accum; my @array1 = split('\n', $array[3]); # print out each element of the array so that they can be further proc +essed for (my $i = 0; $i <= $#array1; $i++) { print "\nElement $i is\n$array1[$i]\n"; }

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://308243]
Approved by davido
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (11)
As of 2014-10-24 12:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (131 votes), past polls