in reply to perl expect

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

Replies are listed 'Best First'.
Re^2: perl expect
by Anonymous Monk on Dec 07, 2016 at 19:51 UTC
    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. ...
        Thanks! I very much appreciate it.