StayCalm
StayCalm

Reputation: 145

CSV file from a hash Perl

I'm trying to get csv file from my output.

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

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

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

Output:

     $VAR1 = bless( {
             'NetworkDeviceGroupList' => {
                                           'NetworkDeviceGroup' => [
                                                                   'Device Type#All Device Types',
                                                                   'IPSEC#Is IPSEC Device#No',
                                                                   'Location#All Locations'
                                                                 ]
                                         },
             'tacacsSettings' => {
                                   'previousSharedSecretExpiry' => '0',
                                   'connectModeOptions' => 'OFF',
                                   'sharedSecret' => '******'
                                 },
             'NetworkDeviceIPList' => {
                                        'NetworkDeviceIP' => {
                                                             'mask' => '24',
                                                             'ipaddress' => '10.66.0.0'
                                                           }
                                      },
             'name' => 'LAB-Distri',
             'coaPort' => '1700',
             'authenticationSettings' => {
                                           'radiusSharedSecret' => '******',
                                           'keyInputFormat' => 'ASCII',
                                           'enableKeyWrap' => 'false',
                                           'networkProtocol' => 'RADIUS'
                                         },
             'profileName' => 'Cisco',
             'id' => '3d21e200-a534-11e3-82cc-00505694f123'
           }, 'Net::Cisco::ISE::NetworkDevice' );

This script should give me my selected elements (ipaddress and name) in console but it doesn't work

   foreach $name (keys %{$networkdevices}){
   print  
   my $ip = $networkdevices{$name}{'ipaddress'};
   my $name = $networkdevices{$name}{'name'};
   }
   

At the end I Need a csv file with names and ip addresses from devices.


Now I use just two methods: Name and NetworkDeviceIPList (just to clean my output info). Unfortunately in Net::Cisco::ISE there is no method just for IPs to get just ips. I can call NetworkDeviceIPList and get:

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

But I do not know how to get now from this output csv data with Arrays of names and ips.

Upvotes: 0

Views: 122

Answers (1)

zdim
zdim

Reputation: 66964

The Dumper output begins with "bless" so you have objects on hand and thus need to find methods to query them, not directly poke into their hashref.

From Net::Cisco::ISE docs *

networkdevices

The returned hash contains instances of Net::Cisco::ISE::NetworkDevice ...
...
When you know the hostname or ID, use the devices call with arguments as listed below.

which identifies what class the returned object belong to, and goes on to give an example

my $device = $ise->networkdevices("id","123"); 

Then look through Net::Cisco::ISE::NetworkDevice for methods to use on $device.

So instead of

my $ip = $networkdevices{$name}{'ipaddress'}; # etc

use $ise->networkdevices as above and make further method calls on the returned object.

Note  

An object is a reference, and normally a hash reference, and one can indeed use it as such. However, that is Not Good, it is borne with trouble, and should really never be done.

Every single poke into object internals may eventually crumble the program, and by the nature of that business probably in subtle ways that are difficult to diagnose (discover).

Remember that an important point about a class is to offer an interface to its functionality and shield the internals from any obligation; it encapsulates the implementation so that it is in principle invisible to the user. The internals may change at any point, without any notice, and perhaps in subtle ways.

One should always find methods that are provided for that class and only use those.


* The CPAN page for this seems to have far less detail

Even if you were to retrieve values directly from $networkdevices, it is a hashref so it needs to be dereferenced first, $networkdevices->{...}{...} where after the first level we may indeed drop the arrow, by a syntax convenience. (But please don't do this with objects.)

Upvotes: 2

Related Questions