Perl Error Handling Problem

I can get this working, but if something is down I get an error and the script does not move on. I can not get the "else" function working. What might I be doing wrong?

use SNMP::Simple
my %ios = ();
$list="list.list";
open(DAT, $list) || die("Can't Open List");
@raw_data=<DAT>;
close(DAT);
foreach $dest (@raw_data)
{
chomp($dest);
my $s = SNMP::Simple->new(
DestHost => $dest,
Community => 'string',
Version => 1,
);
if 
($data{name} = $s->get('sysName'))
($data{sys} = $s->get('sysDescr'))
($data{rom} = $s->get('romId')) {
push @device_data, \%ios;
#print "$data{name} - $data{sys} - $data{rom}\n";
$file = "iosfull.txt";
open(OUT, ">>$file");
print OUT "$data{name} - $data{sys} - $data{rom}\n";
close(OUT);
} else {
print $s did not answer\n";
next;
}

This is the error I am getting.

syntax error at ios.pl line 16, near ")
       ("

There are a couple things very wrong here (marked in red):

use strict; # These 2 lines should be in every Perl program
use warnings;

use SNMP::Simple; #Missing semicolon

my %ios = ();
$list = "list.list";
open( DAT, $list ) || die("Can't Open List");
@raw_data = <DAT>;
close(DAT);
foreach $dest (@raw_data) {
    chomp($dest);
    my $s = SNMP::Simple->new(
        DestHost  => $dest,
        Community => 'string',
        Version   => 1,
    );
    if ( $data{name} = $s->get('sysName') ) #What exactly are you trying to do here??
       ( $data{sys} = $s->get('sysDescr') )
       ( $data{rom} = $s->get('romId') ){
              push @device_data, %ios;

                #print "$data{name} - $data{sys} - $data{rom}\n";
                $file = "iosfull.txt";
                open( OUT, ">>$file" );
                print OUT "$data{name} - $data{sys} - $data{rom}\n";
                close(OUT);
        } else {
              print "$s did not answern "; # Missing opening quotes
              next;
        }
} # Missing closing bracket

Hint: when you've finished writing a program, check it with perl -c first. Also, Perl::Tidy (and the command line utility perltidy) help to improve the readability of your programs a lot, and will help with the visual identification of syntax errors.

1 Like
    if ( $data{name} = $s->get('sysName') ) #What exactly are you trying to do here??
       ( $data{sys} = $s->get('sysDescr') )
       ( $data{rom} = $s->get('romId') ){

I am trying to loop through a list of devices and get 3 snmp variables. If a device in my list is down I want to print "$s did not answer" and move on.

I still don't catch your logic. Do you want to check if all 3 get() calls returned a value? If so, rewrite it to

    if ( defined ( $data{name} = $s->get('sysName') ) &&
         defined ( $data{sys} = $s->get('sysDescr') ) &&
         defined ( $data{rom} = $s->get('romId') ) )

Otherwise, please describe the logic you're thinking of in these 3 specific lines.

Ok,

I have a list of devices. Device A, Device B, etc.

I am trying to get the snmp variables from each device. Name, IOS and ROM. All 3 variables will responsed if the device is online.

If a device is not responding, then skip and move on to the next device on the list.

Name -> IOS -> ROM
Name -> IOS -> ROM
No responsing
Name -> IOS -> ROM
etc.