#!/usr/bin/python

'''
Use eAPI to enumerate interfaces and display (In|Out)Octets
'''

from jsonrpclib import Server

def main(args):
    ''' main '''
    parser = OptionParser()
    parser.add_option('-u', '--username', default='eapi', help='username')
    parser.add_option('-p', '--password', default='', help='password')
    parser.add_option('-P', '--port', default='8543', help='port')
    options, args = parser.parse_args()

    for switch in args:
        url = "https://{0}:{1}@{2}:{3}/command-api".format(
            options.username, options.password, switch, options.port
        )
        request = Server(url)
        response = request.runCmds(1, ["show interfaces"] )
        print "Switch {0} interfaces:".format(switch)
        print "{:20} {:<20} {:<20}".format("Interface", "inOctets", "outOctets")
        for inf, data in response[0]['interfaces'].items():
            print "{:20} {:<20} {:<20}".format(
                inf, data['interfaceCounters']['inOctets'], data['interfaceCounters']['outOctets']
            )
        print


if __name__ == '__main__':
    try:
        sys.exit(main(sys.argv[1:]))
    except KeyboardInterrupt, e:
        print >> sys.stderr, "Exiting on user request.\n"
        sys.exit(1)

@@ -110,16 +110,73 @@
 t Server
+%0Afrom optparse import OptionParser%0Aimport sys%0Aimport json
 %0A%0Adef ma
@@ -443,16 +443,90 @@
 'port')%0A
+    parser.add_option('-D', '--debug', action='store_true', help='debug')%0A
     opti
@@ -811,16 +811,107 @@
 ces%22%5D )%0A
+        if options.debug:%0A            print json.dumps(response, indent=4, sort_keys=True)%0A
         
@@ -1097,16 +1097,64 @@
 tems():%0A
+            if 'interfaceCounters' in data:%0A    
         
@@ -1185,32 +1185,36 @@
 %7B:%3C20%7D%22.format(%0A
+    
                 
@@ -1296,16 +1296,20 @@
 ctets'%5D%0A
+    
         

add debugging and error checking