def get_iface_info(vservice_name): try: iface_details = json.loads(clid("show int mgmt 0")) data = iface_details['TABLE_interface']['ROW_interface'] except Exception as error: logger.error("Show command failed") sys.exit() mac_addr = data['eth_hw_addr'].replace('.', '') mac_addr = ':'.join(mac_addr[i:i + 2] for i in range(0, len(mac_addr), 2)) data ="mgmt0.multicast=false\nmgmt0.mtu=" + data['eth_mtu'] + \ "\nmgmt0.mac=" + mac_addr + "\nmgmt0.addresses=0\nmgmt0.0.ip=" + \ data['eth_ip_addr'] + "\nmgmt0.0.prefix=" + str(data['eth_ip_mask']) with open('/bootflash/interfaces', 'w') as fd: fd.write(data) cont_path = '/isan/vdc_1/virtual-instance/' + vservice_name + '/rootfs' if os.path.isdir(cont_path): os.chdir(cont_path) dir_name = 'embndb' try: if not os.path.exists(dir_name): os.system('mkdir ' + dir_name) #subprocess.call(['sudo', 'mkdir', dir_name]) except: logger.error("Permission denied to create embndb directory") else: logger.error("Please provide proper virtual-service name") sys.exit() os.chdir(cont_path + '/' + dir_name) os.system('sudo cp ' + cont_path + '/xnclite/launcher.sh interfaces') os.system('sudo cp /bootflash/interfaces interfaces') os.system('sudo rm /bootflash/interfaces') #subprocess.call(['sudo', 'cp', cont_path+'/xnclite/launcher.sh', 'interfaces']) #To get required permission #subprocess.call(['sudo', 'cp', '/bootflash/interfaces', 'interfaces']) #Override the file which has the permission #subprocess.call(['sudo', 'rm', '/bootflash/interfaces']) # cleanup tmp interface file logger.info( "Successfully created /embndb/interface file with management interface details" )
def get_iface_info(vservice_name,forceFlag): global reactivate_flag, mkdir_flag try: iface_details=json.loads(clid("show int mgmt 0")) data = iface_details['TABLE_interface']['ROW_interface'] except Exception as error: logger.error("Show command failed") sys.exit() mac_addr = data['eth_hw_addr'].replace('.', '') mac_addr = ':'.join(mac_addr[i:i+2] for i in range(0, len(mac_addr), 2)) data ="mgmt0.multicast=false\nmgmt0.mtu=" + data['eth_mtu'] + \ "\nmgmt0.mac=" + mac_addr + "\nmgmt0.addresses=0\nmgmt0.0.ip=" + \ data['eth_ip_addr'] + "\nmgmt0.0.prefix=" + str(data['eth_ip_mask']) try: with open('/bootflash/interfaces', 'w') as fd: fd.write(data) except IOError as e: logger.error("No space left on the device") logger.error("Pls clean up the /bootflash and retry") sys.exit() cont_path ='/isan/vdc_1/virtual-instance/' + vservice_name + '/rootfs' if os.path.isdir(cont_path): os.chdir(cont_path) dir_name = 'embndb' if os.path.exists(dir_name): if forceFlag == 1: pass else: logger.info("NDB application is already installed.") sys.exit(0) try: if not os.path.exists(dir_name): os.system('mkdir '+dir_name) mkdir_flag = 1 #subprocess.call(['sudo', 'mkdir', dir_name]) except: logger.error("Permission denied to create embndb directory") sys.exit() else: output = cli("show virtual-service detail name " + vservice_name) if output[1] == "\n": logger.error("Pls provide the right NDB service name") sys.exit(0) try: logger.info("Triggered to activate virtual service %s"%(vservice_name)) cli("conf t ; virtual-service "+ vservice_name +" ; activate") iter = 0 while(iter < 24): time.sleep(5) #pdb.set_trace() virtual_service_json = cli("show virtual-service detail name "+ vservice_name + " | json ") vservice_output = json.loads(virtual_service_json[1]) vservice_state = vservice_output['TABLE_detail']['ROW_detail']['state'] if "Activated" in vservice_state: logger.info("Virtual service %s was successfully activated"%(vservice_name)) break; else: iter += 1 except: logger.error("Error while activating virtual-service " + vservice_name) sys.exit() #os.system('mkdir '+cont_path) os.chdir(cont_path) dir_name = 'embndb' try: if not os.path.exists(dir_name): os.system('mkdir '+dir_name) mkdir_flag = 1 #subprocess.call(['sudo', 'mkdir', dir_name]) except: logger.error("Permission denied to create embndb directory") try: if mkdir_flag == 1 or forceFlag == 1: reactivate_flag = 1 os.chdir(cont_path+ '/' + dir_name) os.system('sudo cp '+cont_path+'/xnclite/launcher.sh interfaces') os.system('sudo cp /bootflash/interfaces interfaces') os.system('sudo rm /bootflash/interfaces') #subprocess.call(['sudo', 'cp', cont_path+'/xnclite/launcher.sh', 'interfaces']) #To get required permission #subprocess.call(['sudo', 'cp', '/bootflash/interfaces', 'interfaces']) #Override the file which has the permission #subprocess.call(['sudo', 'rm', '/bootflash/interfaces']) # cleanup tmp interface file emb_path ='/isan/vdc_1/virtual-instance/' + vservice_name + '/rootfs/embndb/interfaces' logger.info("Successfully created %s file with management interface details" % emb_path) except IOError: logger.error("No space left on the device") logger.error("Pls clean up the /bootflash and retry") sys.exit()
#!/usr/bin/env python from cisco import clid import json if __name__ == "__main__": arp_table = clid('show ip arp') arp_tabled = json.loads(arp_table) arp_entries = arp_tabled['TABLE_vrf']['ROW_vrf']['TABLE_adj']['ROW_adj'] if isinstance(arp_entries, dict): arp_entries_list = [arp_entries] combined_table = {} for entry in arp_entries: combined_table[entry.get('mac')] = {} combined_table[entry.get('mac')]['ipaddr'] = entry['ip-addr-out'] mac_table = clid('show mac address-table') mac_tabled = json.loads(mac_table) mac_entries_list = mac_tabled['TABLE_mac_address']['ROW_mac_address'] if isinstance(mac_entries_list, dict): mac_entries_list = [mac_entries_list] for entry in mac_entries_list: if not combined_table.get(entry.get('disp_mac_addr')): combined_table[entry.get('disp_mac_addr')] = {} combined_table[entry.get('disp_mac_addr')]['interface'] = entry.get( 'disp_port') combined_table[entry.get('disp_mac_addr')]['vlan'] = entry.get(
# Copyright 2016 Cisco Systems, Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. #!/usr/bin/env python from cisco import clid ver_text = clid("show version") for pair in ver_text.split(",") : if "sys_ver_str" in pair: print(pair[pair.index(':')+2:len(pair)])
def get_iface_info(vservice_name, forceFlag): global reactivate_flag, mkdir_flag try: iface_details = json.loads(clid("show int mgmt 0")) data = iface_details['TABLE_interface']['ROW_interface'] except Exception as error: logger.error("Show command failed") sys.exit() mac_addr = data['eth_hw_addr'].replace('.', '') mac_addr = ':'.join(mac_addr[i:i + 2] for i in range(0, len(mac_addr), 2)) data ="mgmt0.multicast=false\nmgmt0.mtu=" + data['eth_mtu'] + \ "\nmgmt0.mac=" + mac_addr + "\nmgmt0.addresses=0\nmgmt0.0.ip=" + \ data['eth_ip_addr'] + "\nmgmt0.0.prefix=" + str(data['eth_ip_mask']) try: with open('/bootflash/interfaces', 'w') as fd: fd.write(data) except IOError as e: logger.error("No space left on the device") logger.error("Pls clean up the /bootflash and retry") sys.exit() cont_path = '/isan/vdc_1/virtual-instance/' + vservice_name + '/rootfs' if os.path.isdir(cont_path): os.chdir(cont_path) dir_name = 'embndb' if os.path.exists(dir_name): if forceFlag == 1: pass else: logger.info("NDB application is already installed.") sys.exit(0) try: if not os.path.exists(dir_name): os.system('mkdir ' + dir_name) mkdir_flag = 1 #subprocess.call(['sudo', 'mkdir', dir_name]) except: logger.error("Permission denied to create embndb directory") sys.exit() else: output = cli("show virtual-service detail name " + vservice_name) if output[1] == "\n": logger.error("Pls provide the right NDB service name") sys.exit(0) try: logger.info("Triggered to activate virtual service %s" % (vservice_name)) cli("conf t ; virtual-service " + vservice_name + " ; activate") iter = 0 while (iter < 24): time.sleep(5) #pdb.set_trace() virtual_service_json = cli( "show virtual-service detail name " + vservice_name + " | json ") vservice_output = json.loads(virtual_service_json[1]) vservice_state = vservice_output['TABLE_detail']['ROW_detail'][ 'state'] if "Activated" in vservice_state: logger.info( "Virtual service %s was successfully activated" % (vservice_name)) break else: iter += 1 except: logger.error("Error while activating virtual-service " + vservice_name) sys.exit() #os.system('mkdir '+cont_path) os.chdir(cont_path) dir_name = 'embndb' try: if not os.path.exists(dir_name): os.system('mkdir ' + dir_name) mkdir_flag = 1 #subprocess.call(['sudo', 'mkdir', dir_name]) except: logger.error("Permission denied to create embndb directory") try: if mkdir_flag == 1 or forceFlag == 1: reactivate_flag = 1 os.chdir(cont_path + '/' + dir_name) os.system('sudo cp ' + cont_path + '/xnclite/launcher.sh interfaces') os.system('sudo cp /bootflash/interfaces interfaces') os.system('sudo rm /bootflash/interfaces') #subprocess.call(['sudo', 'cp', cont_path+'/xnclite/launcher.sh', 'interfaces']) #To get required permission #subprocess.call(['sudo', 'cp', '/bootflash/interfaces', 'interfaces']) #Override the file which has the permission #subprocess.call(['sudo', 'rm', '/bootflash/interfaces']) # cleanup tmp interface file emb_path = '/isan/vdc_1/virtual-instance/' + vservice_name + '/rootfs/embndb/interfaces' logger.info( "Successfully created %s file with management interface details" % emb_path) except IOError: logger.error("No space left on the device") logger.error("Pls clean up the /bootflash and retry") sys.exit()