The documentation in CPAN does not say anything about a close() routine and with SSH1 it assumes that every connection is new. But W32Perl version uses SSH2 which is persistent. How do you properly close the instance?
After creating 18 or so connections the script dies. I suspect I have created too many sessions that haven't been closed but can't figure out how to do it. When I chop up the list it works okay but I want to correct my programming style.
#!c:\perl\bin\perl.exe
use strict;
use Net::SSH::W32Perl;
die "Error: Module Spreadsheet::WriteExcel needed. Please install and
+ retry.\n" unless ( require
Spreadsheet::WriteExcel );
my $user = 'username';
my $password = 'password';
my $postfix = 'host.com';
my $cmd = 'lsallq';
my $excelfile = 'lsallq.xls';
die "Error: File $excelfile already exists. Please rename or delete a
+nd rerun\n" if ( -e $excelfile ); #
WriteExcel can't change an existing
my @servers = (
'servername1',
'servername2',
'servername3',
'servername4',
'servername5',
'servername6',
'servername7',
'servername8',
'servername9',
'servername10',
'servername11',
'servername12',
'servername13',
'servername14',
'servername15',
'servername16',
'servername17',
'servername18',
'servername19',
'servername20',
'servername21',
'servername22',
'servername24',
'servername25',
);
my $workbook;
die "Error: Could not create $excelfile.\n" unless ( $workbook = Sprea
+dsheet::WriteExcel->new($excelfile)
);
my $time = localtime(time());
my $worksheet = $workbook->add_worksheet("lsallq");
my $colnum = 0;
foreach my $server (@servers) {
my $rownum = 0;
print "Connecting to $server\n";
my $ssh = Net::SSH::W32Perl->new($server.'.'.$postfix, port => 22
+);
$ssh->login($user, $password);
my($stdout, $stderr, $exit) = $ssh->cmd($cmd);
if ($stderr) {
print "ERROR: $stderr $exit\n";
}
$worksheet->write_string($rownum,$colnum, $server);
my @printers = split(/\n/,$stdout);
sort @printers;
print "Generating spreadsheet entries\n";
foreach my $printer (@printers) {
$rownum++;
$worksheet->write_string($rownum,$colnum, $printer);
}
$colnum++;
$ssh->cmd('exit'); # explicit exit? Trying to fix die at 18th
+server, how do you destroy instance correctly?
}
print "Done. I think\n";
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.