I am using Net::SSH2 to ssh into a NAS box, and run a command. I can get the command results while I am inside the loop. When I am outside the loop, I don't see the variable if it contains the output. However, I can see the variable if a new value is defined.
In the code below, I assign $result to $_. If I am inside the loop, I can see it. If I am outside the loop I cannot. However, I can define $result="test" before I leave the loop, and I can then see it outside the loop. It appears that I cannot pass the $_ (output of the NAS Command)
How do I pass the $_ outside the loop?
Here is my code:
#!C:\strawberry\perl\bin
use warnings;
use strict;
use Net::SSH2;
use MIME::Lite;
use Config::Tiny;
my $ssh2 = Net::SSH2->new();
my $ipaddr="w.x.y.z";
my $username="username";
my $password="password";
$ssh2->connect("$ipaddr") || die "PROBELM - $!";
$ssh2->auth_password("$username","$password") || die "Username
+/Password not right";
my $chan = $ssh2->channel();
$chan->blocking(0);
$chan->exec('export NAS_DB=/nas;/nas/bin/nas_fs -list');
print "Chan= $chan\n";
my $result;
while (<$chan>)
{
$result=$_; #Desired output. Only works inside the loop.
$result='test'; #Used for testing, this works inside and outside of lo
+op
#print $result; #Works
}
print $result; #Works if $result is set, but not with a $_;
print "END";