Currently in Migration - Jason Edelman's Old Blog
  • Home
  • About
  • Contact
Code is being updated - it's changed quite a bit actually.  Link to code on github will be posted soon.  Check back again soon!


Main module is followed by the Arista module.

You can see I was playing with how to best add new modules.  Cisco's onePK module is embedded as class in cpal.py while Arista's is a separate module that is imported.  They accomplish the same thing.  In order to maintain modularity and simplicity, the Cisco class will likely become it's own module.
 #!/usr/bin/env python  
 #cpal.py   
 #by Jason Edelman   
 #Last Updated 2/18/14  

 from onep.element.NetworkElement import NetworkApplication, NetworkElement  
 from onep.interfaces.InterfaceStatus import InterfaceStatus  
 from onep.element.SessionConfig import SessionConfig  
 from onep.interfaces import NetworkInterface  
 from onep.core.exception import OnepDuplicateElementException  
 from onep.vty.VtyService import VtyService  
 import json  
 import random  
 import sys  
 from arista import device1 as arista  
 class device():  
      from onep.element.NetworkElement import NetworkApplication  
      def __init__(self,manufacturer,address):  
           self.manufacturer = manufacturer  
           self.address = address  
           if self.manufacturer.lower() == 'cisco':  
                self.cisco_device = self.cisco(self.address)  
                self.native = self.cisco_device.native  
                if self.native != 'DNE':  
                     self.facts = self.cisco_device.getFacts()  
           else:  
                if manufacturer.lower() == 'arista':  
                     self.arista_device = arista(self.address)  
                     self.native = self.arista_device.native  
                     #if self.native != 'DNE':  
                          #print self.native  
                     self.facts = self.arista_device.getFacts()  
      class cisco():  
           def __init__(self,ip_address):  
                self.username = 'ciscosdn'  
                self.password = 'cisco'  
                self.myapp = NetworkApplication.get_instance()  
                if not self.myapp.name == "onePK-Python-Shell-CLI":  
                     self.myapp.name = "onePK-Python-Shell-CLI"  
                #self.myapp.name = "onePK-Python-Shell-CLI-" + str(random.randint(0,10000))  
                self.address = ip_address  
                self.native = self.jconnect()   
           def jconnect(self):  
                session_config = SessionConfig(SessionConfig.SessionTransportMode.TLS)  
                session_config.ca_certs = "/home/cisco/ca.pem"  
                ne = self.myapp.get_network_element(self.address)  
                if not ne.is_connected():  
                     try:  
                          #print 'Trying to connect...'  
                          session_handle = ne.connect(self.username, self.password, session_config)  
                          #print 'Connection made to ' + self.address + ' successfully.'  
                     except OnepDuplicateElementException, e:  
                       print e  
                       existing = e.get_original_network_element()  
                       print existing  
                       print 'Unable to connect to device.'  
                     except Exception, e:  
                       print e  
                       print 'Could not connect to ' + self.address + '. Please validate reachability.'  
                       return 'DNE'  
                else:  
                     print 'Connection to ' + self.address + ' already exists, but you can use this new object if you want.'  
                return ne  
           def getserialNumber(self):  
                return self.native.properties.SerialNo  
           def getCPU(self):  
                return self.native.system_cpu_utilization  
           def getfreeMemory(self):  
                return self.native.get_free_system_memory()  
           def gettotalMemory(self):  
                return self.native.total_system_memory  
           def getHostname(self):  
                return self.native.properties.sys_name  
           def getUptime(self):  
                return self.native.properties.sys_uptime  
           def getPlatform(self):  
                return self.native.properties.product_id  
           def getReasonforReboot(self):  
                reason = 'Unknown'  
                vty_session = VtyService(self.native)  
                vty_session.open()  
                parsed_show_version = vty_session.write('show version').split('\n')  
                for line in parsed_show_version:  
                     if line.startswith('Last reload'):  
                          parsed_line = line.split(':')  
                          reason = parsed_line[1].strip()  
                          return reason  
           def getFacts(self):  
                cpu_utilization = self.getCPU()  
                free_memory = self.getfreeMemory()  
                total_memory = self.gettotalMemory()  
                hostname = self.getHostname()  
                uptime = self.getUptime()  
                platform = self.getPlatform()  
                serial_number = self.getserialNumber()  
                reboot_reason = self.getReasonforReboot()  
                connect_ip = self.address  
                facts = {'connect_ip': connect_ip,'serial_number': serial_number, 'cpu_utilization': cpu_utilization, 'free_system_memory': free_memory,   
                'total_sytem_memory': total_memory,'hostname': hostname, 'system_uptime': uptime, 'platform': platform, 'last_reboot_reason': reboot_reason, 'vendor':'cisco'}  
                return facts  
Arista Module
 #!/usr/bin/env python  
 #arista.py   
 #Jason Edelman   
 #Last Updated 2/18/14  
 from jsonrpclib import Server  
 class device1():  
      def __init__(self,address):  
           self.username = 'arista'  
           self.password = 'arista'  
           self.address = address       
           self.native = self.jconnect()  
           self.facts = self.getFacts()  
      def jconnect(self):  
           connect_string = "http://" + self.username + ":" + self.password + "@" + self.address + "/command-api"  
           #print connect_string  
           switch = 'DNE'  
           try:  
                #print 'Trying to connect...'  
                #print 'Using API: ', connect_string  
                switch = Server(connect_string)  
                #print 'Connection made to ' + self.address + ' successfully.'  
           except Exception, e:  
                print e  
                print 'Unable to connect to device.'  
           return switch  
      def getPlatform(self,output):  
           return output[0]["modelName"]  
      def getserialNumber(self,output):  
           if output[0]["serialNumber"] == '':  
                return '12345'  
           else:  
                return output[0]["serialNumber"]  
      def getUptime(self,response):  
           c = response[0]['output']  
           return c[:9]  
      def getHostname(self,output):  
           parse = output[0]['output'].split('\n')  
           return parse [3].strip()[16:-1]  
      def getfreeMemory(self,output):  
           return output[0]['memFree']  
      def gettotalMemory(self,output):  
           return output[0]['memTotal']  
      def getFacts(self):  
           sh_ver = self.native.runCmds( 1, ["show version"] )  
           sh_uptime = self.native.runCmds( 1, ["show uptime"],"text" )  
           sh_lldp_localinfo = self.native.runCmds( 1, ["show lldp local-info"],"text")  
           #cpu_utilization = self.getCPU(ne)  
           free_memory = self.getfreeMemory(sh_ver)  
           total_memory = self.gettotalMemory(sh_ver)  
           uptime = self.getUptime(sh_uptime)  
           platform = self.getPlatform(sh_ver)  
           serial_number = self.getserialNumber(sh_ver)  
           connect_ip = self.address  
           hostname = self.getHostname(sh_lldp_localinfo)  
           facts = {'hostname': hostname, 'connect_ip': connect_ip,'platform':platform, 'serial_number':serial_number, 'system_uptime':uptime,'free_system_memory': free_memory,   
                'total_sytem_memory': total_memory,'vendor':'arista'}  
           return facts