#!/usr/bin/perl use strict; # make sure we can open input and output files open( H, "host_list_file" ) or die "host_list_file: $!"; open( O, ">output.csv" ) or die "output.csv: $!"; # read host list my @hosts = ; close H; chomp @hosts; # remove "\r\n" from ends of lines my @iplist; for my $host ( @hosts ) { my $ip = ... # do whatever you do to get $ip for $host (and chomp if needed) push @iplist, $ip; } my %ipdata; my @infolist = qw/ifDescr ifOperStatus ifAdminStatus ifLastChange/; my $cmdarg = "public interfaces.ifTable.ifEntry."; # UPDATED to include final "." for my $ip ( @iplist ) { for my $info ( @infolist ) { my @pdata = `snmpwalk $ip $cmdarg$info`; chomp @pdata; # remove line-terminations s/.* = // for (@pdata); # remove redundant text $ipdata{$ip}{$info} = [ @pdata ]; # create array ref in the hash structure $ipdata{$ip}{lastport} = $#pdata; # keep track of how many ports } } # now print the csv data for my $ip ( @iplist ) { for my $portid ( 0 .. $ipdata{$ip}{lastport} ) { my $outstr = ''; for my $info ( @infolist ) { $outstr .= '"' . $ipdata{$ip}{$info}[$portid] . '",'; } $outstr =~ s/,$/\n/; # replace trailing comma print O $outstr; } }