I have a device that does not support many connections. If it times out during a telnet session, I'm forced to re-cycle the unit. I am using Expect.pm and just snagged the example code in it's perldoc. Normally, the timeout would not be a problem but it is because this device is not a workstation. Here is the output I see
Escape character is '^]'. Ubuntu 8.04.1 scriptbed login: $VAR1 = bless( \*Symbol::GEN0, 'Expect' ); scriptbed Password: $VAR1 = [ bless( \*Symbol::GEN0, 'Expect' ) ]; Can't call method "do_soft_close" on unblessed reference at ./myexpect +.pl line 57.
Here is the full code
#!/usr/bin/perl -w 2 3 BEGIN { 4 unshift(@INC,'/n/home5/release/Perl/Modules/lib/perl5/site_perl/5. +8.0'); 5 } 6 7 use strict; 8 use Expect; 9 use Data::Dumper; 10 11 my $timeout = 1; 12 13 my $username = 'scriptbed'; 14 my $password = 'invalid'; # forcing the timeout 15 16 print localtime(time)."\n"; 17 18 my $exp = Expect->spawn('/usr/bin/telnet','scriptbed'); 19 20 my $spawn_ok; 21 $exp->expect($timeout, 22 [ 23 qr'login: $', 24 sub { 25 $spawn_ok = 1; 26 my $fh = shift; 27 print Dumper $fh; 28 $fh->send("$username\n"); 29 exp_continue; 30 } 31 ], 32 [ 33 'Password: $', 34 sub { 35 my $fh = shift; 36 print $fh "$password\n"; 37 exp_continue; 38 } 39 ], 40 [ 41 eof => 42 sub { 43 my $fh = shift; 44 $fh->do_soft_close(); 45 if ($spawn_ok) { 46 die "ERROR: premature EOF in login.\n"; 47 } else { 48 die "ERROR: could not spawn telnet.\n"; 49 } 50 } 51 ], 52 [ 53 timeout => 54 sub { 55 my $fh = shift; 56 print Dumper $fh; 57 $fh->do_soft_close(); 58 die "Timed out\n"; 59 } 60 ], 61 '-re', qr'[#>:] $', #' wait for shell prompt, then +exit expec t 62 );

In reply to Trying to close Expect.pm telnet timeout situation to avoid overwhelming a small target device by elmoz

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.