Reply

NetMRI Python - String functions to get an device Interface output

Techie
Posts: 9
6975     0

I am trying to split output of "sh int desc" for Cisco devices using split() function but getting below error.

 

===========================================

*** ERROR: Error 'Traceback (most recent call last):
File "", line 153, in
AttributeError: 'list' object has no attribute 'split'
' ***

============================================

 

Below is my python Script :

 

# BEGIN-SCRIPT-BLOCK
#
# Script-Filter:
# true
#
# END-SCRIPT-BLOCK

from infoblox_netmri.easy import NetMRIEasy

# This values will be provided by NetMRI before execution
defaults = {
"api_url": api_url,
"http_username": http_username,
"http_password": http_password,
"job_id": job_id,
"device_id": device_id,
"batch_id": batch_id
}

# Create NetMRI context manager. It will close session after execution
with NetMRIEasy(**defaults) as easy:
with Remote(**defaults) as remote:
interface=easy.send_command('show int desc')
x=interface.splitlines()

y=x.split()
print(y[3])

 

 

My aim is to convert output into single dimensional array.

 

Tried using python API to directly take output value of an interface but no luck ., not sure how to call python API for getting interface description

 

ifDescr(like /<Value >/)

 

any suggestions on String functions or Python API would be greatly appreciated .

 

Re: NetMRI Python - String functions to get an device Interface output

Superuser
Posts: 115
6975     0

What's your end goal for the script?

 

 

Also, can you print out the following variables so we can see what is stored in them:

interface and x

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

https://sifbaksh.com

Re: NetMRI Python - String functions to get an device Interface output

Techie
Posts: 9
6975     0

Thank You for getting in touch Sif.

 

My goal is to get output of interface description of a deivce and store that in a variable along with corresponding device .

 

I tried to do this via Python API using RemoteModel Class but no luck 

 

Below is script for same:

 

# BEGIN-SCRIPT-BLOCK
#
# Script-Filter:
# true
#
# END-SCRIPT-BLOCK

#from infoblox_netmri.easy import NetMRIEasy
from RemoteModel import InterfaceBroker



# This values will be provided by NetMRI before execution
defaults = {
"api_url": api_url,
"http_username": http_username,
"http_password": http_password,
"job_id": job_id,
"device_id": device_id,
"batch_id": batch_id 
}

# Create NetMRI context manager. It will close session after execution
with InterfaceBroker(**defaults) as remote:
int = remote.ifDescr
print(int)

 

 

 

Answer to your second part i.e output of inerface and x.

 

I am able to get output sucessfully for interface i.e sh int desc, but not for x and getting below error :

 

Error.log:

 *** ERROR: Error 'Traceback (most recent call last):  File "", line 153, in AttributeError: 'list' object has no attribute 'split'' ***  

Re: NetMRI Python - String functions to get an device Interface output

Techie
Posts: 9
6976     0

Hello Sif ,

 

I prepared below script to update Interface description . My basic Goal was to add a Prefix to all interfaces which was having Server in its Description .

 

This is done in Cisco Router and switches

 

with NetMRIEasy(**defaults) as easy:
int=easy.send_command('show int desc ')
b=int.strip().splitlines() # Striping output of sh interface description command
for x in b:
sp=x.split() # Spliting each line into a single array in order to get interface ID 
mmm=easy.send_command('show int '+sp[0]+' | i Desc') # Description ouput is stored in a variable
if ((mmm.find('Server')!=-1) | (mmm.find('server')!=-1))  # If Descricption contains Server or server 
newdesc=mmm.lstrip('Description: ') # Output of variable mmm was trimmed in order to just get value which is                                                                         description 
easy.send_command('conf t')
easy.send_command('int '+sp[0])
easy.send_command('description MMM'+newdesc) # Adding a prefix value 
easy.send_command('end')
else:
print('No value')

 

I know this can be done using Python API also as it is having few classes which can directly give interface description value but I am not sure how to call that.

Class name is "RemoteModel"

I have already opened a case to get this sorted out meanwhile above script will do my task .

 

 

Re: NetMRI Python - String functions to get an device Interface output

New Member
Posts: 3
6976     0

Hi Techecook,

 

Here is the problem with your script: 

 

x=interface.splitlines() --> on this line interface is a string variable which will hold the output of your command, on string variables we can use split function to split string as array based on delimeter, default delimeter is space, on this line we are spliting the string based on new lines

y=x.split() --> on this line x is an array, we can not use split function on list or array, if you are trying to split the first row of the array then "y=x[0].split()" will work.

 

Hope this helps to fix your issue.

Re: NetMRI Python - String functions to get an device Interface output

New Member
Posts: 1
6976     0

The AttributeError is an exception thrown when an object does not have the attribute you tried to access. The 'list' object has no attribute 'split' error is that you're trying to call python split function on the whole list of lines, and you can't split a list of strings, only a string. So, you need to split each line, not the whole thing.

 

Showing results for 
Search instead for 
Did you mean: 

Recommended for You