Reputation: 39
I'm trying to open a device list file to run a command but getting an "unopened filehandle" error. How I can fix that? The error is on the while (<List>) {
line.
#!\usr\bin\Perl\bin\perl
use warnings;
use strict;
use NET::SSH2;
use MIME::Base64;
my $host = "C:/temp/devices.txt"; # input file
my $user = "XXX"; # your account
my $pass = "XXXXX"; # your password 64 bit mime
my $ssh2 = Net::SSH2->new();
my $result = "C:/temp/result1.txt"; # output file
$ssh2->debug(1); # debug on/off
open(List, '<', "$host" ) or die "$!";
my @lines = <List>;
for my $line (@lines) {
$ssh2->connect($line) or die "Unable to connect host $@ \n";
my $dp=decode_base64("$pass");
$ssh2->auth_password("$user","$dp");
my $chan = $ssh2->channel();
$chan->exec('sh ver');
my $buflen =30000;
my $buf1 = '0' x $buflen;
$chan->read($buf1, $buflen);
close (LIST);
#### ## print in file
open (OUTPUT, '>', "C:/temp/result.txt") or die "$!";
open (OUTPUT, '>', "$result") or die "$!";
print OUTPUT "Result For:", "$host\n", $buf1,"\n";
close (OUTPUT);
}
Upvotes: 0
Views: 197
Reputation: 6824
Your List
is lowercase in the while loop, and upper case LIST
in the rest. Perl is case sensitive.
To the rest of the code, it's not obvious what you're trying to do. Assuming you need to do something line-by-line from the LIST, then replace your while loop with:
for my $line (@lines) {
then in your code, whereever you need the "current line", use $line
.
I can't comment on the usage of the ssh module here; haven't used it myself.
Upvotes: 4