2

我需要将我的文本文件(我的输出)解析为 csv 格式,其中仅包含来自设备的名称和 Ips。像:

Name, ip
LAB-W, 10.66.1.12
LAB-D, 10.66.1.13

我没有在 perl 中解析的经验,也许有人可以帮助我解决这个问题。

use Net::Cisco::ISE;
use Data::Dumper;
use Text::CSV;

my $ise = Net::Cisco::ISE->new(hostname=>'hostname', username=>'user', password=>'user');

for my $name (keys %{$networkdevices}){
    my $device = $ise->networkdevices("id" => $networkdevices->{$name}->id);
    print Dumper $device->name;
    print Dumper $device->NetworkDeviceIPList;
}

我有这个输出:

$VAR1 = 'LAB-W';
$VAR1 = {
     'NetworkDeviceIP' => {
         'mask' => '32',
         'ipaddress' => '10.66.1.12'
      }
};
$VAR1 = 'LAB-D';
$VAR1 = {
     'NetworkDeviceIP' => {
          'mask' => '24',
          'ipaddress' => '10.66.1.13'
      }
};

我试过这个脚本(来自这篇文章),但我没有看到输出,也没有错误:

use DBI;
use strict;
use Data::Dumper;
use File::Slurp;

open (FILE, "<./my_output") or die "could not open file: $!";
undef $/;

my $contents = <FILE>;
close(FILE);

my $hash_of_arrays = eval $contents;

for my $key (keys %$hash_of_arrays) {
    my $hash = $hash_of_arrays->{$key};
    for my $key2 (keys %$hash) {
        my $array = $hash->{$key2};
        for my $i (0..$#$array) {
            print "$i: $$array[$i]\n";
        }
    }
}
4

1 回答 1

2

由于数据只是在散列中,您可以直接在循环中访问它们:

for my $name (keys %{$networkdevices}){
    my $device = $ise->networkdevices("id" => $networkdevices->{$name}->id);
    print join(',',
       $device->name
       $device->NetworkDeviceIPList->{NetworkDeviceIP}->{ipaddress}
    ) . "\n";
}
于 2018-01-26T03:48:10.823 回答