Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

foreach and variables problem

sconossconos Posts: 1Member
Kind of new to Perl, having a problem with a foreach loop reading a log file - each line in the logfile has a format of datastring=datanumber and I'm splitting on the "=" - the curious thing that I'm having a problem with no matter how many lines in the log file - I only get variable return from each datastring in the log file and just the last datanumber in the log file?

/----------------------------------

#!c:Perlinperl.exe
use Net::SNMP;
use CGI qw(:standard);

$LOGFILE = "stormon.log";
open(LOGFILE) or die("Could not open log file.");
foreach $line () {

($hostip, $driveid,) = split('=',$line);

($session,$error) = Net::SNMP->session(Hostname => $hostip,
Community => "public");
die "session error: $error" unless ($session);

# This gets hostname from System OID
$result = $session->get_request("1.3.6.1.2.1.1.5.0");
$hostname = ($result->{"1.3.6.1.2.1.1.5.0"});

# This gets Drive Letter & Label from Host OID
$result = $session->get_request("1.3.6.1.2.1.25.2.3.1.3." . $driveid . "");
$drivename = ($result->{"1.3.6.1.2.1.25.2.3.1.3." . $driveid . ""});

die "request error: ".$session->error unless (defined $result);

print ( $hostname," , ", $drivename,"
" ) ;

$session->close;

}
close(LOGFILE);

/-------------------------------------------------------------

For a return I get

hostname1 ,
hostname2 , drivelabel

Played with the variables and this should work, but I can't see were its breaking.

TIA Mike
Sign In or Register to comment.