Posted on March 21, 2015 1:35 pm
 |  Asked by Andrew Harris
 |  1933 views
RESOLVED
1
1
Print Friendly, PDF & Email

Hi,

I am trying to do something which I thought would be fairly simple using the EOS API, alas it is causing me some issues. I want to run the command “show interfaces” via the JSON API and then extract and print various bits of information. I have started off simple by (trying) to extract the MTU.

This is my Python code:

import json
import jsonrpclib
from jsonrpclib import Server

switches = ["10.2.7.170"]
username = "admin"
password = "admin"

# Going through all the switch IP addresses listed above
for switch in switches:
urlString = "https://{0}:{1}@{2}/command-api".format(username, password, switch)
switchReq = Server( urlString )

# Display the current interface list
response = switchReq.runCmds( 1, ["show interfaces"] )

dataString = json.loads(jsonrpclib.history.response)

dataDump = json.dumps(dataString,sort_keys=True,indent=2)

parent = dataString["result"][0]["interfaces"]
for item in parent:
print item["mtu"]

However when I execute I get the following error:


Traceback (most recent call last):

 

  File “getInte.py”, line 25, in <module>

 

    print item[“mtu”]

 

TypeError: string indices must be integers

Any suggestions on how I can correctly extract the MTU information? Here is the raw JSON output from EOS

{
   "jsonrpc": "2.0",
   "result": [
      {
         "interfaces": {
            "Management1": {
               "lastStatusChangeTimestamp": 1426614417.95,
               "name": "Management1",
               "duplex": "duplexFull",
               "autoNegotiate": "success",
               "burnedInAddress": "00:0c:29:7b:2e:e2",
               "mtu": 1500,
               "hardware": "ethernet",
               "interfaceStatus": "connected",
               "bandwidth": 1000000000,
               "forwardingModel": "routed",
               "lineProtocolStatus": "up",
               "interfaceCounters": {
                  "outBroadcastPkts": 0,
                  "linkStatusChanges": 3,
                  "totalOutErrors": 0,
                  "inMulticastPkts": 0,
                  "counterRefreshTime": 1426937173.23,
                  "inBroadcastPkts": 0,
                  "outputErrorsDetail": {
                     "deferredTransmissions": 0,
                     "txPause": 0,
                     "collisions": 0,
                     "lateCollisions": 0
                  },
                  "outOctets": 4520291,
                  "outDiscards": 0,
                  "inOctets": 86336009,
                  "inUcastPkts": 0,
                  "inputErrorsDetail": {
                     "runtFrames": 0,
                     "rxPause": 0,
                     "fcsErrors": 0,
                     "alignmentErrors": 0,
                     "giantFrames": 0,
                     "symbolErrors": 0
                  },
                  "outUcastPkts": 14658,
                  "outMulticastPkts": 0,
                  "totalInErrors": 0,
                  "inDiscards": 0
               },
               "interfaceStatistics": {
                  "inBitsRate": 2665.69759675,
                  "outBitsRate": 1502.55994891,
                  "inPktsRate": 0,
                  "updateInterval": 300,
                  "outPktsRate": 0.353148063658
               },
               "interfaceAddress": [
                  {
                     "secondaryIpsOrderedList": [],
                     "broadcastAddress": "255.255.255.255",
                     "secondaryIps": {},
                     "primaryIp": {
                        "maskLen": 24,
                        "address": "10.2.7.170"
                     },
                     "virtualIp": {
                        "maskLen": 0,
                        "address": "0.0.0.0"
                     }
                  }
               ],
               "physicalAddress": "00:0c:29:7b:2e:e2",
               "description": ""
            },
            "Ethernet2": {
               "lastStatusChangeTimestamp": 1426614418.98,
               "name": "Ethernet2",
               "duplex": "duplexFull",
               "autoNegotiate": "unknown",
               "burnedInAddress": "00:0c:29:7b:2e:f6",
               "mtu": 9214,
               "hardware": "ethernet",
               "interfaceStatus": "connected",
               "bandwidth": 10000000000,
               "forwardingModel": "bridged",
               "lineProtocolStatus": "up",
               "interfaceCounters": {
                  "outBroadcastPkts": 0,
                  "linkStatusChanges": 1,
                  "totalOutErrors": 0,
                  "inMulticastPkts": 0,
                  "counterRefreshTime": 1426937173.22,
                  "inBroadcastPkts": 0,
                  "outputErrorsDetail": {
                     "deferredTransmissions": 0,
                     "txPause": 0,
                     "collisions": 0,
                     "lateCollisions": 0
                  },
                  "outOctets": 21382515,
                  "outDiscards": 0,
                  "inOctets": 0,
                  "inUcastPkts": 0,
                  "lastClear": 1426614364.71,
                  "inputErrorsDetail": {
                     "runtFrames": 0,
                     "rxPause": 0,
                     "fcsErrors": 0,
                     "alignmentErrors": 0,
                     "giantFrames": 0,
                     "symbolErrors": 0
                  },
                  "outUcastPkts": 0,
                  "outMulticastPkts": 169905,
                  "totalInErrors": 0,
                  "inDiscards": 0
               },
               "interfaceStatistics": {
                  "inBitsRate": 0,
                  "outBitsRate": 0,
                  "inPktsRate": 0,
                  "updateInterval": 300,
                  "outPktsRate": 0
               },
               "interfaceAddress": [],
               "physicalAddress": "00:0c:29:7b:2e:f6",
               "description": ""
            },
            "Ethernet3": {
               "lastStatusChangeTimestamp": 1426614418.98,
               "name": "Ethernet3",
               "duplex": "duplexFull",
               "autoNegotiate": "unknown",
               "burnedInAddress": "00:0c:29:7b:2e:00",
               "mtu": 9214,
               "hardware": "ethernet",
               "interfaceStatus": "connected",
               "bandwidth": 10000000000,
               "forwardingModel": "bridged",
               "lineProtocolStatus": "up",
               "interfaceCounters": {
                  "outBroadcastPkts": 0,
                  "linkStatusChanges": 1,
                  "totalOutErrors": 0,
                  "inMulticastPkts": 0,
                  "counterRefreshTime": 1426937173.22,
                  "inBroadcastPkts": 0,
                  "outputErrorsDetail": {
                     "deferredTransmissions": 0,
                     "txPause": 0,
                     "collisions": 0,
                     "lateCollisions": 0
                  },
                  "outOctets": 21382515,
                  "outDiscards": 0,
                  "inOctets": 0,
                  "inUcastPkts": 0,
                  "inputErrorsDetail": {
                     "runtFrames": 0,
                     "rxPause": 0,
                     "fcsErrors": 0,
                     "alignmentErrors": 0,
                     "giantFrames": 0,
                     "symbolErrors": 0
                  },
                  "outUcastPkts": 0,
                  "outMulticastPkts": 169905,
                  "totalInErrors": 0,
                  "inDiscards": 0
               },
               "interfaceStatistics": {
                  "inBitsRate": 0,
                  "outBitsRate": 0,
                  "inPktsRate": 0,
                  "updateInterval": 300,
                  "outPktsRate": 0
               },
               "interfaceAddress": [],
               "physicalAddress": "00:0c:29:7b:2e:00",
               "description": ""
            },
            "Ethernet1": {
               "lastStatusChangeTimestamp": 1426614418.98,
               "name": "Ethernet1",
               "duplex": "duplexFull",
               "autoNegotiate": "unknown",
               "mtu": 1500,
               "hardware": "ethernet",
               "interfaceStatus": "connected",
               "bandwidth": 10000000000,
               "forwardingModel": "routed",
               "lineProtocolStatus": "up",
               "interfaceCounters": {
                  "outBroadcastPkts": 0,
                  "linkStatusChanges": 1,
                  "totalOutErrors": 0,
                  "inMulticastPkts": 43334,
                  "counterRefreshTime": 1426937173.21,
                  "inBroadcastPkts": 23,
                  "outputErrorsDetail": {
                     "deferredTransmissions": 0,
                     "txPause": 0,
                     "collisions": 0,
                     "lateCollisions": 0
                  },
                  "outOctets": 5110583,
                  "outDiscards": 0,
                  "inOctets": 5475112,
                  "inUcastPkts": 10773,
                  "inputErrorsDetail": {
                     "runtFrames": 0,
                     "rxPause": 0,
                     "fcsErrors": 0,
                     "alignmentErrors": 0,
                     "giantFrames": 0,
                     "symbolErrors": 0
                  },
                  "outUcastPkts": 5427,
                  "outMulticastPkts": 43442,
                  "totalInErrors": 0,
                  "inDiscards": 0
               },
               "interfaceStatistics": {
                  "inBitsRate": 0,
                  "outBitsRate": 0,
                  "inPktsRate": 0,
                  "updateInterval": 300,
                  "outPktsRate": 0
               },
               "interfaceAddress": [
                  {
                     "secondaryIpsOrderedList": [],
                     "broadcastAddress": "255.255.255.255",
                     "secondaryIps": {},
                     "primaryIp": {
                        "maskLen": 30,
                        "address": "10.0.0.1"
                     },
                     "virtualIp": {
                        "maskLen": 0,
                        "address": "0.0.0.0"
                     }
                  }
               ],
               "physicalAddress": "00:0c:29:4b:b3:b9",
               "description": ""
            }
         }
      }
   ],
   "id": "CapiExplorer-123"
}
0
Posted by Mark Berly
Answered on March 21, 2015 2:29 pm

Andrew – You are trying to iterate over a string and not a dictionary, try this out:

from jsonrpclib import Server

#----------------------------------------------------------------
# Configuration section
#----------------------------------------------------------------
SWITCH_IP = 'switch name or IP'
USERNAME = 'user'
PASSWORD = 'pass'
#----------------------------------------------------------------


def main():
   switch = Server( 'https://%s:%s@%s/command-api' %
                    ( USERNAME, PASSWORD, SWITCH_IP ) )
   response = switch.runCmds( 1, [ 'show interfaces' ] )
   interfaces = response[ 0 ][ 'interfaces' ]
   for intf, value in interfaces.iteritems():
      mtu = [value][0]['mtu']
      print "Interface %s has its MTU set to %i" % ( intf, mtu )

if __name__ == '__main__':
   main()
0
Posted by Andrew Harris
Answered on March 22, 2015 9:56 am

That works perfectly, many thanks for your help!

If you want to quickly see all of the values for the interfaces on the switch, you can import pprint and pprint the value, try this:

from jsonrpclib import Server
from pprint import pprint

#----------------------------------------------------------------
# Configuration section
#----------------------------------------------------------------
SWITCH_IP = 'SWITCH IP or NAME'
USERNAME = 'USERNAME
PASSWORD = 'PASSWORD'
#----------------------------------------------------------------

def main():
   switch = Server( 'https://%s:%s@%s/command-api' %
                    ( USERNAME, PASSWORD, SWITCH_IP ) )
   response = switch.runCmds( 1, [ 'show interfaces' ] )
   interfaces = response[ 0 ][ 'interfaces' ]
   for intf, value in interfaces.iteritems():
      print "--- Interface %s ---" % intf
      pprint(value)
      print 

if __name__ == '__main__':
   main()
(Mark Berly at March 22, 2015 2:23 pm)

Post your Answer

You must be logged in to post an answer.