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

The minimized script below is my first try at using Net::Telnet's waitfor() to automate password updates on CatOS LAN switches that do interactive prompting.   It passes #!/bin/perl -wc but when run without the "-c" the following error results: Can't call method "waitfor" without a package or object reference at line 30 which is the first line containing a "waitfor()".   In case it matters - Perl 5.00503 on Debian 2.2r2.

I started with an example from Network Programming with Perl and a chunk from (code)) Cisco Pass Mass - IOS (deprecated by node 123464).   Super Search turned up a thread where waitfor() is used, but it didn't help me understand how to apply the right syntax in my scriptlet.  

Mind you, I don't even pretend to grok OOP.   Can a OOP-literate monk help me to see the error of my ways?

P.S. I know the code is double-ugly, but my intent is just to figure out the "waitfor()" syntax.   After that, I'll clean it up big-time.
    cheers,
    Don
    striving for Perl Adept
    (it's pronounced "why-bick")

#!/usr/bin/perl -w # setpassCat.pl # February 26-27, 2001 use strict; use Net::Telnet::Cisco; use vars qw($before $match); my $target = ''; my $oldpass = ''; my $olden = ''; my $newpass = ''; umask oct 177; my $cs = Net::Telnet::Cisco->new( host=>"$target", errmode=>'return', timeout=>'30', input_log=>'ciscoconf.log',) or die " Error connecting to $target\n"; $cs->login('',$oldpass) or die " Error logging in to $target. Bad pa +ssword?\n"; print " ", $cs -> last_prompt, "\n"; &PAUSE(); $cs->enable($olden) or die " Error getting $target privilaged mode.\n +"; print " ", $cs -> last_prompt, "\n"; &PAUSE(); my $telnet = $cs -> print('set pass') or die "Error giving \"set pass\ +" command.\n"; ($before,$match) = $telnet -> waitfor('Enter old password:') or die "E +rror getting \"Enter old password\" prompt\n"; print " ", $cs -> last_prompt, "\n"; &PAUSE(); my $output = $cs -> print("$oldpass") or die "Error giving existing ac +cess password.\n"; ($before,$match) = $telnet -> waitfor('Enter new password:') or die "E +rror getting \"Enter new password\" prompt\n"; print " ", $cs -> last_prompt, "\n"; &PAUSE(); $output = $cs -> print("$newpass") or die "Error giving new access pas +sword.\n"; ($before,$match) = $telnet -> waitfor('Retype new password:') or die " +Error getting \"Retype new password\" prompt\n"; print " ", $cs -> last_prompt, "\n"; &PAUSE(); $output = $cs -> print("$newpass") or die "Error confirming new access + password.\n"; ($before,$match) = $telnet -> waitfor('Password changed') or die "Erro +r getting \"Password changed\" confirmation.\n"; print " ", $cs -> last_prompt, "\n"; $cs->close; ###################################################################### +### sub PAUSE { print " <enter> to continue...\n"; my $continue = <STDIN>; } ###################################################################### +###

Replies are listed 'Best First'.
Re: Net::Telnet and waitfor()
by chipmunk (Parson) on Feb 28, 2001 at 03:34 UTC
    my $telnet = $cs -> print('set pass') or die "Error giving \"set pass\" command.\n"; ($before,$match) = $telnet -> waitfor('Enter old password:') or die "Error getting \"Enter old password\" prompt\n";
    You're expecting $cs->print('set pass') to return some kind of object. Apparently, it doesn't. Perhaps you want to call waitfor() on the object in $cs instead?
      Thanks, o wise rodent.   8^)   Changing the waitfor() to refer to $cs (as you and archon both suggested) got rid of the object error.

      I've added "$dump_log=>setpassCat.dumplog" to "my $cs =".   With a bit of luck, perusing the dump_log and input_log should help me work out remaining problems.
          cheers,
          Don
          striving for Perl Adept
          (it's pronounced "why-bick")

      Update: It's still quite kludgy, but the following scriptlet *does* work.   8^D   The next order of business is to replace the or die's with if(){} for all the $cs -> sections, so Net::Telnet can deal directly with timeouts and such.   I expect to then find that the waitfor()</t>'s need further tweaking.

      #!/usr/bin/perl -w # setpassCat.pl # February 26-27, 2001 use strict; use Net::Telnet::Cisco; my $target = ''; my $oldpass = ''; my $olden = ''; my $newpass = ''; umask oct 177; my $cs = Net::Telnet::Cisco->new ( host =>"$target", errmode =>'return', timeout =>'30', input_log =>'setpassCat.inlog', ) or die " Error connecting +to $target: $!"; $cs->login('',$oldpass) or die " Error logging in +to $target: $!"; print $cs -> last_prompt, "\n"; $cs->enable($olden) or die " Error getting pri +vilaged mode at $target: $!"; print $cs -> last_prompt, "\n"; my @cmd_output = $cs->cmd('set pass'); print (@cmd_output) or die "Error giving \"set +pass\" command: $!"; print $cs -> last_cmd, "\n"; $cs ->waitfor('/Enter old password: /') or die "Error getting \"Ent +er old password\" prompt: $!"; print $cs -> last_prompt, "\n"; @cmd_output = $cs -> cmd($oldpass); print (@cmd_output) or die "Error giving \"old +password\": $!"; $cs ->waitfor('/Enter new password: /') or die "Error getting \"Ent +er new password\" prompt: $!"; print $cs -> last_prompt, "\n"; @cmd_output = $cs -> cmd($newpass); print (@cmd_output) or die "Error giving \"new +password\": $!"; $cs ->waitfor('/Retype new password: /') or die "Error getting \"Ret +ype new password\" prompt: $!"; print $cs -> last_prompt, "\n"; @cmd_output = $cs -> cmd($newpass); print (@cmd_output) or die "Error giving \"Rety +pe new password\": $!"; $cs ->waitfor('/Password changed./') or die "Error getting \"Pas +sword changed\" statement: $!"; print $cs -> last_prompt, "\n"; @cmd_output = $cs -> cmd('disa'); print (@cmd_output) or die "Error leaving privi +laged mode: $!"; print $cs -> last_prompt, "\n"; $cs -> close;
Re: Net::Telnet and waitfor()
by archon (Monk) on Feb 28, 2001 at 03:35 UTC
    I think you want $cs->waitfor(), not $telnet->waitfor(). You might also look into using Expect.pm.