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

Network Change & Configuration Management

Reply
Highlighted
Accepted Solution

List Custom Fields via Perl

Authority
Posts: 29
7741     0

Below is an the example script from the Network Automation API Guide to list routing devices:  

 

my $broker = $client->broker->device;

my @devices = $broker->index({ DeviceType => [“Router”, “Switch-Router”]});

foreach my $device (@devices)
{
print $device->DeviceName, ' ', $device->DeviceIPDotted, ' ', $device->DeviceType,
"\n";
}

 

But I want to include a device custom field along with the device name, IP and type in the file that is generated.  

Is that possible? 

 

Highlighted

Re: List Custom Fields via Perl

Adviser
Posts: 431
7742     0

Say you have a custom field name "Sif Location"

Use the following

$device->custom_sif_location

 

If you have a field name "Sif"

$device->custom_sif

 

Let me know if you need anymore help on this, you can also update the 

 

Follow me on LinkedIn: https://www.linkedin.com/in/sifbaksh
Twitter: https://twitter.com/sifbaksh

www.sifbaksh.com
Highlighted

Re: List Custom Fields via Perl

Authority
Posts: 29
7742     0

Thank you for your response. I'm trying to retrieve the device custom field "Location".  

 

I received this:

 

Can't locate object method "custom_location" via package "NetMRI::API::Remote::V3_0_0:Smiley Very Happyevice" at ./xseed.pl line 35

 

 

When I execute this:

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

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

# Connect to the NetMRI.
my $client = new NetMRI::API({
api_version => '3.0.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)],

});

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

 

It looks like the last part of your response was truncated.

 

Highlighted

Re: List Custom Fields via Perl

Adviser
Posts: 353
7742     0
Try accessing it as a hash entry rather than a method call.
Highlighted

Re: List Custom Fields via Perl

[ Edited ]
Expert
Posts: 42
7742     0

Hi JPemberton,

 

please try

 

$device->get_custom_field($customfieldname);

 

You can also specify your custom field name directly like,

 

$device->get_custom_field("Location");

 

 

For more Information see the Perl API doc at:

 

http://netmriip/netmri/share/api/perldoc/NetMRI__API__Remote__Device.html#remote-get_custom_field-field_name-

 

$remote->get_custom_field( $field_name )

Get the custom field with the name $field_name.

 

 

Best,

 

Markus

 

Highlighted

Re: List Custom Fields via Perl

Authority
Posts: 29
7742     0

I confirmed this worked for the Custom Field "Location" in my script:

 

$device->get_custom_field("location")

 

Note the lowercase "l".  Also, this didn't seem to work if you had a special character, such as a space or hyphen,  in the custom field name (ex: "serial-number".)  It was easy enough to rename them.

 

Thanks Markus!

 

 

Highlighted

Re: List Custom Fields via Perl

Adviser
Posts: 353
7742     0
I believe special characters will all get mapped to an underscore.

John
Highlighted

Re: List Custom Fields via Perl

Authority
Posts: 29
7742     0

Confirmed:

 

Custom field names: "Serial-Number", "Serial Number" and "Serial_Number" all worked with an underscore:

 

$device->get_custom_field("serial_number")

 

Thank you John.

Showing results for 
Search instead for 
Do you mean 

Recommended for You