Search code examples
pythonpython-2.7string-search

Python script - String Search - Arista


I'm trying to run a script on arista switch, which is a simple python script. I got some values by running a command on the switch and I need to find a value from that command. for eg:

#!/usr/bin/python
from jsonrpclib import Server

switch = Server( "http://XXX:[email protected]/command-api" )

response = switch.runCmds( 1, [ "show interfaces ethernet 49 status" ] )

a = response[0]

print a

output is

{'interfaceStatuses': {'Ethernet49': {'vlanInformation': {'interfaceMode': 'routed', 'interfaceForwardingModel': 'routed'}, 'bandwidth': 10000000000L, 'interfaceType': '10GBASE-SR', 'description': 'GH1TPQACORS1 Et1', 'autoNegotiateActive': False, 'duplex': 'duplexFull', 'autoNegotigateActive': False, ***'linkStatus': 'connected'***}}}

Out of this result, I just need 'linkStatus' : 'connected' value, how do I do it?


Solution

  • Basically you have dictionary of dictionary of dictionary. For this case, its simply. But if you have multiple of such cases, you have to iterate with the keys of first two dictionaries (dict1: interface and dict2: ethernet)

    a['interfaceStatuses']['Ethernet49']['linkStatus']