Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

perl expect

by janasec (Sexton)
on Nov 26, 2015 at 13:52 UTC ( #1148665=perlquestion: print w/replies, xml ) Need Help??

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

Hi I am having difficulty in understanding expect module

#!/usr/bin/perl use Expect; $Expect::Exp_Internal = 1; my $host = ""; my $username = "root"; my $pass = "Pass1234"; my $expect = Expect->new; my $expect = Expect->spawn("ssh $host -l $user") or die; $expext->send("$pass"); $cmd = `ls`; $expect->send("$cmd"); print "$expect\n";

in the above code iam not seeing anything and I need list the files,perform some remote processing on the system and read the out put

basically i want to run commands on the linux machine like 'ls','ifconfig','ping' and return their output back from the remote system

Replies are listed 'Best First'.
Re: perl expect
by Corion (Pope) on Nov 26, 2015 at 13:58 UTC
Re: perl expect
by salva (Canon) on Nov 26, 2015 at 15:20 UTC
Re: perl expect
by janasec (Sexton) on Nov 26, 2015 at 18:00 UTC

    found some sample code i think for now this will do

    my $exp = Expect->spawn("telnet localhost") or die "Cannot spawn telnet: $!\n";; my $spawn_ok; $exp->expect($timeout, [ qr'login: $', sub { $spawn_ok = 1; my $fh = shift; $fh->send("$username\n"); exp_continue; } ], [ 'Password: $', sub { my $fh = shift; print $fh "$password\n"; exp_continue; } ], [ eof => sub { if ($spawn_ok) { die "ERROR: premature EOF in login.\n"; } else { die "ERROR: could not spawn telnet.\n"; } } ], [ timeout => sub { die "No login.\n"; } ], '-re', qr'[#>:] $', #' wait for shell prompt, then exit + expect );

    this is from the expect module from cpan

    Thanks all who tried

      Just a raw beginner's question but in the last line of this code - what does the 'gr' in gr'[#>:] $' mean? I realize he's looking for the prompt at the end of a line but I can't find a reference to the 'gr' syntax anywhere.

        See qr/STRING/msixpodualn in Regexp Quote Like Operators:

        This operator quotes (and possibly compiles) its STRING as a regular expression. ...
Re: perl expect
by u65 (Chaplain) on Nov 26, 2015 at 23:24 UTC

    Hi, janasec, this node explains the way I do similar things (in response to this node). A lot depends on how varied or unpredictable your work flow is.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (2)
As of 2021-06-25 01:46 GMT
Find Nodes?
    Voting Booth?
    What does the "s" stand for in "perls"? (Whence perls)

    Results (133 votes). Check out past polls.