Learn How We Can Help You Keep Teleworkers Protected During the COVID-19 Crisis

Network Change & Configuration Management

Reply
Highlighted
Accepted Solution

Querying Method Variables in Perl

Authority
Posts: 29
3868     2

I have very little scripting experience.


I’m attempting to output other fields which are specified as [M] method such as “network_name”. I’m unsure how to incorporate into the following script:

 

#!/usr/bin/perl
# this script finds all Cisco devices in NetMRI

use strict;
use warnings;
use NetMRI::API;
open (OUT, "> /apps/ipspace/seedfile.csv") || die "Can't open file seedfile: $!\n";

# Connect to the NetMRI.
my $client = new NetMRI::API({
api_version => '2.3',
});

my @devices = $client->broker->device->find({

# find all devices where Vendor = Cisco
op_DeviceVendor => '=', val_c_DeviceVendor => 'Cisco',


# and device assurance is greater than 72 percent
op_DeviceAssurance => '>', val_c_DeviceAssurance => '72',

# and Type
op_DeviceType => 'not like', val_c_DeviceType => "'IP Phone','Linux','Windows'",

# and Name begins with I
#op_DeviceName => 'like', #val_c_DeviceName => 'I%',

# retreive the id and name to save bandwidth
select => [qw( DeviceID DeviceName DeviceModel DeviceSysContact DeviceSysLocation DeviceIPDotted DeviceType)],
});

foreach my $device (@devices)
{
# display the device name and model
print OUT join(',', $device->DeviceName, $device->DeviceIPDotted, $device->DeviceSysContact, $device->DeviceSysLocation, $device->DeviceModel, $device->DeviceType), "\n";
}

Highlighted

Re: Querying Method Variables in Perl

Adviser
Posts: 353
3868     2
I believe it will work if you just call $device->network_name. The issue is that it will make another call to the server, so this is slow. But I believe it should work.
Highlighted

Re: Querying Method Variables in Perl

Authority
Posts: 29
3868     2

I was expecting the Network View string to be generated:

Network View[M] network_nameStringA Network View assigned to the device.

 

...


foreach my $device (@devices)
{
# display the device name and model
print OUT join(',', $device->DeviceName, $device->network_name, $device->DeviceIPDotted, $device->DeviceSysContact, $device->DeviceSysLocation, $device->DeviceModel, $device->get_custom_field("location"), $device->DeviceType), "\n";
}

 

This generates output for $device->network_name that looks like this:

HASH(0x36e88b0).

 

Highlighted

Re: Querying Method Variables in Perl

Adviser
Posts: 353
3868     2
Huh. I would have expected network_name to be printed as well. Two things to try:

1) Try printing it out with Data:Smiley Very Happyumper. It’s probably going to be a HASH with a network_name key and the value of the network name.
2) Try instead to add a parameter to the query:

methods => ’network_name’

This should cause the server to call the network_name method on the object internally (before responding) and store the result in the returned data. Then, in the code you would access it like:

$device->{network_name}

Notice that is a hash access not a method call.

John
Highlighted

Re: Querying Method Variables in Perl

Authority
Posts: 29
3869     2

Thanks John!  

 

I know very little but the following worked for me:

#!/usr/bin/perl
# this script finds all Cisco devices in NetMRI

use strict;
use warnings;
use NetMRI::API;
open (OUT, "> /apps/seedfile.csv") || die "Can't open file seedfile: $!\n";

# Connect to the NetMRI.
my $client = new NetMRI::API({
api_version => '3.0',
});

my @devices = $client->broker->device->find({

# find all devices where Vendor = Cisco
op_DeviceVendor => '=', val_c_DeviceVendor => 'Cisco',

# and device assurance is greater than 72 percent
op_DeviceAssurance => '>', val_c_DeviceAssurance => '72',

# and Type
op_DeviceType => 'not like', val_c_DeviceType => "'IP Phone','Linux','Windows'",


# retreive the id and name to save bandwidth
# select => [qw( DeviceID DeviceName DeviceModel DeviceSysContact DeviceSysLocation DeviceIPDotted DeviceType custom_Location)],

methods => ['asset_type', 'network_name', 'data_source'],

});

foreach my $device (@devices)
{
print OUT join(',', $device->{data_source}->DataSourceName, $device->DeviceName, $device->{network_name}, $device->{asset_type}, $device->DeviceIPDotted, $device->DeviceSysContact, $device->DeviceSysLocation, $device->DeviceModel, $device->get_custom_field("location"), $device->DeviceType), "\n";
}

Showing results for 
Search instead for 
Do you mean 

Recommended for You