Hi everyone,
I have this weird behaviour. (maybe it is weird because I am not seeing what I am doing wrong).
I use the Net::Telnet module and I am trying to do some error handling.
The script has the following parts:
1. Connect to the switch
2. Login to the switch
3. Excecute some commands
I declared the $host variable in the beginning. In my understanding this means that the variable (when set) is known to the whole script. right?
I itterate through all the hosts in the @hosts array to do the mentioned parts. What I find strange is that when I call a sub from within the foreach loop I need to provide the $host everytime.
Ok now something goes wrong. I am trying to telnet using the wrong password. As you can see in the login sub I call the Errors sub. I did not provide the $host but the Errors sub knows what $host is.
Another component and something else goed wrong. I am trying to open a telnet connection but the component does not respond. again I call the Errors sub without the $host. This time from within the foreach loop but it gives me an error that it does not know $host.
I do have warnings and strict in the script and it does not give me any other errors.
I might be overlooking something so I hope someone can tell me what I am overlooking.
InDenial
I have this weird behaviour. (maybe it is weird because I am not seeing what I am doing wrong).
I use the Net::Telnet module and I am trying to do some error handling.
The script has the following parts:
1. Connect to the switch
2. Login to the switch
3. Excecute some commands
I declared the $host variable in the beginning. In my understanding this means that the variable (when set) is known to the whole script. right?
I itterate through all the hosts in the @hosts array to do the mentioned parts. What I find strange is that when I call a sub from within the foreach loop I need to provide the $host everytime.
Ok now something goes wrong. I am trying to telnet using the wrong password. As you can see in the login sub I call the Errors sub. I did not provide the $host but the Errors sub knows what $host is.
Another component and something else goed wrong. I am trying to open a telnet connection but the component does not respond. again I call the Errors sub without the $host. This time from within the foreach loop but it gives me an error that it does not know $host.
I do have warnings and strict in the script and it does not give me any other errors.
I might be overlooking something so I hope someone can tell me what I am overlooking.
Code:
my $host;
my @hosts = qw(127.0.0.1 10.0.0.1);
my $t = new Net::Telnet (Timeout => 10,
Prompt => '/\\\\user>/',
Input_log => 'inputlog',
Dump_log => 'dumplog',
cmd_remove_mode => 0,
Errmode => 'return');
$t->input_log(\*STDOUT);
foreach $host(@hosts){
print OUTPUT "----------------Start $host----------\n";
print OUTPUT "$host : Start Telnet\n";
$command = "connect";
if ($t->open("$host")) {
if (&login($host)){
&dostuff($host);
}
}
else {[b]&Errors('CONERROR01)[/b]};
print OUTPUT "----------End $host-------\n";
}
########### subs ##########################
sub Errors {
my $error = $_[0];
if ($command eq "connect" || $command eq "login"){
print OUTPUT "$host : $command : $Errorlist{$error}\n";
}
else {
my $lastline = $t->lastline;
print OUTPUT "$host : $lastline\n";
}
}
sub login {
$host = $_[0];
$command = "login";
my ($prematch, $match) = $t->waitfor(Match => '/login: $/',
Errmode => 'return');
if (!$match) {
&Errors('LIERROR01');
return 0;
}
print OUTPUT $host." : Login prompt\n";
$t->print("user");
($prematch, $match) = $t->waitfor(Match => '/password: $/',
Errmode => 'return');
if (!$match) {
&Errors('LIERROR02');
return 0;
}
print OUTPUT $host." : password prompt\n";
$t->print("password");
($prematch, $match) = $t->waitfor(Match => '/\\\\user>/',
Errmode => 'return');
if (!$match){
[b]&Errors('LIERROR03');[/b]
return 0;
}
print OUTPUT $host." : Switch prompt\n";
}
InDenial