Perl telnet does not wait for the end of the previous command. In the file infile.log I see the continuation of the command my @ config = $ telnet-> cmd ("sh run");
, but the script is already beginning to run the command print ("Format configuration", $ _, "\ n");
. As a result, I get an empty array @config.
foreach (@linksys_sps){
print ("Connecting to ",$_,"\n");
my $telnet = new Net::Telnet ( Timeout=>10,Errmode=>'return',Input_Log => "infile.log");
$telnet->open($_);
if ($telnet->errmsg){
print "Can't connect to " . $_ . " Error: " . $telnet->errmsg . "\n";
} else {
$telnet->max_buffer_length(5 * 1024 * 1024);
$telnet->waitfor('/User Name:$/i');
$telnet->print('admin');
$telnet->waitfor('/Password:$/i');
$telnet->print('password');
print ("Set Terminal Variable ",$_,"\n");
$telnet->cmd("terminal datadump");
print ("Create file ",$_,"\n");
my $file = sprintf($folder."/".$_);
print ("Create file ",$file,"\n");
system "touch $file";
system "chown nobody $file";
print ("Read configuration ",$_,"\n");
my @config = $telnet->cmd("sh run");
print ("Write configuration ",$_," to file ",$file,"\n");
open my $fh, "> $file" or die "Can't open $file : $!";
foreach (@config) {
print $fh "$_"; # Print each entry in our array to the file
}
close $fh;
print ("Set Terminal Variable ",$_,"\n");
$telnet->cmd("no terminal datadump");
$telnet->cmd("exit");
}
}
How to fix it?
From the docs:
The methods login() and cmd() use the prompt setting in the object to
determine when a login or remote command is complete. Those methods
will fail with a time-out if you don't set the prompt correctly.
In otherwords, you need to add the prompt parameter when you create the $telnet
object:
my $switch_name = 's-east';
my $telnet = new Net::Telnet (
Timeout=>10,
Errmode=>'return',
Input_Log => "infile.log",
Prompt => '/\Q$switch_name\E#\s?$/'
);
(It looks for the shell prompt to know that a command is completed).
Due to the instability of the cmd()
, I switched to using waitfor(String => $string)
.
New script below:
foreach (@linksys_sps){
my $file = sprintf($folder."/".$_);
print ("Create file ",$file,"\n");
system "touch $file";
my $string = sprintf($_."#");
print ("Prompt String is ",$string,"\n");
print ("Connecting to ",$_,"\n");
my $telnet = new Net::Telnet (
Timeout=>20,
Errmode=>'return',
Input_Log => "infile.log"
);
$telnet->open($_);
if ($telnet->errmsg){
print "Can't connect to " . $_ . " Error: " . $telnet->errmsg . "\n";
} else {
$telnet->waitfor('/User Name:$/i');
$telnet->print('admin');
$telnet->waitfor('/Password:$/i');
$telnet->print('password');
$telnet->waitfor(String => $string );
print ("Set Backup Terminal Variable ",$_,"\n");
$telnet->print("terminal datadump");
$telnet->waitfor(String => $string );
print ("Read configuration ",$_,"\n");
$telnet->print('sh run');
my @config = $telnet->waitfor(String => $string );
print ("Write configuration ",$_," to file ",$file,"\n");
open my $fh, '>', $file or die "Cannot open file: $!";
foreach my $config (@config) {
print $fh "$config\n";
}
close $fh;
print ("Set Default Terminal Variable ",$_,"\n");
$telnet->print('no terminal datadump');
$telnet->waitfor(String => $string );
$telnet->print('exit');
}
}