
1arryb
User
May 18, 2009, 5:43 PM
Post #4 of 4
(3076 views)
|
Hi kp2a, I played around with this program for awhile. It looks like the Timeout value is applied to every call to the telnet server. I simplified your program a bit and was able to tune the program for better speed. Minimum timeout value is 1, however. If you set it to zero, the program "succeeds" but the remote command is never executed. What I learned about Net::Telnet: 1. The timeout is adjustable, minimum value is 1. 2. waitfor() is cool. 3. cmd() is NOT cool: I was never able to get either a return code from the remote command OR the output via either of the scalar or list variants. 4. Unless you set Errmode to 'return', waitfor() fails with a bogus 'timeout waiting for regex' error. All in all, I'd say this module isn't ready for prime time suitable for high volume remote command execution.
#!/usr/bin/perl use strict; use warnings; use Net::Telnet; use Time::HiRes qw( usleep ualarm gettimeofday tv_interval ); my ($sec,$usec) = gettimeofday ; my $start = $sec + ($usec/1000000.0); sub usec { ($sec,$usec) = gettimeofday ; $sec + ($usec/1000000.0) - $start; } printf "%10.6f start\n", usec; $| = 1; my ($ip, $user, $pass) = @ARGV; die "usage: $0 <host> <user> <pass>" unless $ip and $user and $pass; printf "%10.6f call Telnet for $ip expect connected\n",usec; my $timeout = 1; my $t = new Net::Telnet( Host => $ip, Timeout => $timeout, Prompt => '/ > /', Input_log => "input.log", Errmode => 'return' ); if($t) { printf "%10.6f telnet connected - expect Login:\n",usec; } else { printf "%10.6f error: telnet connect failed\n",usec; exit; } $t->waitfor('/login:/i'); printf "%10.6f now send $user - expect Password:\n",usec; $t->print($user); $t->waitfor('/password:/i'); printf "%10.6f now send password - expect prompt ' > '\n",usec; $t->print($pass); $t->waitfor('/ > /'); printf "%10.6f send command - expect echo, reply and new prompt\n",usec; my @output = (); eval { my $ret = $t->cmd(String => "echo hello"); }; if ($@) { printf "%10.6f command failed, ret=%s\n",usec, $@; $t->close; exit -1; } printf "%10.6f command suceeded:\n",usec; my $echo = 0; while( my $line = $t->getline(Timeout => $timeout, Errmode => 'return') ) { last unless $line; $line =~ s/\r//g; chomp $line; printf "%10.6f line <%s>\n",usec,$line; if( $line =~ /print>/ ) { $echo++; printf "%10.6f command echo\n",usec; next; } if( $line =~ / > / && $echo ) { printf "%10.6f prompt for next command, if any\n",usec; last; } } printf "%10.6f close\n",usec; $t->close; Here's the output:
0.000008 start 0.000052 call Telnet for myserver expect connected 0.001049 telnet connected - expect Login: 0.006488 now send admin - expect Password: 0.046403 now send password - expect prompt ' > ' 2.086405 send command - expect echo, reply and new prompt 4.087419 command suceeded: 4.087634 line < > 4.087709 line <Last login: Mon May 18 17:32:18 from mytesthost> 4.087785 line <[admin@myserver ~]$ echo hello> 4.087868 line <hello> 6.087356 close Cheers, Larry
(This post was edited by 1arryb on May 19, 2009, 6:55 AM)
|