2

I need to parse my text file (my output) to csv format, which contains just Names and Ips from devices. Somthing like:

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

I do not have experience with parsing in perl, maybe somebody can help me with this question.

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;
}

And I have this output:

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

I tried this script (from this post), but I do not see output, also no mistakes :

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";
        }
    }
}
2
  • 1
    Could you please specify if you really need to parse the output, or if you can actually edit the script that generates the output? Commented Jan 25, 2018 at 13:42
  • So far as I know I can get this data just in hash Commented Jan 25, 2018 at 15:40

1 Answer 1

2

Since the data are just in a hash, you can access them directly inside your loop:

for my $name (keys %{$networkdevices}){
    my $device = $ise->networkdevices("id" => $networkdevices->{$name}->id);
    print join(',',
       $device->name
       $device->NetworkDeviceIPList->{NetworkDeviceIP}->{ipaddress}
    ) . "\n";
}
Sign up to request clarification or add additional context in comments.

1 Comment

$device->NetworkDeviceIPList->{NetworkDeviceIP}->{ipaddress} oh....I did not know, that it could be so easy...I tried with $device->NetworkDeviceIP->{ipaddress} and it did nor work, because there ist no NetworkDeviceIP methode in this bib. but you are right , I can access it directly inside the loop! Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.