def main(): con = sqlite3.connect('test.db') _cursor = con.cursor() _cursor.execute( "SELECT name FROM sqlite_master WHERE type='table' AND name='vlans'") row = _cursor.fetchone() if row is None: apic = login() con = sqlite3.connect('test.db') _cursor = con.cursor() _cursor.execute("CREATE TABLE vlans (pk INTEGER PRIMARY KEY," + "hostname TEXT, vlanNumber INTEGER, vlanType TEXT, " + "ipAddress TEXT, networkAddress TEXT, prefix TEXT)") con.commit() all_devices_response = apic.networkdevice.getAllNetworkDevice() for device in all_devices_response.response: if device.platformId is not None: vlan_svi_response = apic.networkdeviceidvlan.getDeviceVLANData( id=device.id) if len(vlan_svi_response.response) > 0: insert_vlan_response(vlan_svi_response.response, device.hostname, _cursor) con.commit() query_time(_cursor, con.cursor())
def main(): apic = login() device_ids = get_apic_tag_association(apic, "desktop-switch") for device_id in device_ids: apic_response = apic.networkdevice.getNetworkDeviceById(id=device_id) device = apic_response.response if device.platformId is None: break
def main(): d = date.today() print("{0} ##########################".format(d.isoformat())) apic = uniq_login.login() allDevicesResponse = apic.networkdevice.getAllNetworkDevice() for device in allDevicesResponse.response: print("Checking: {0}, ID: {1}".format(device.hostname, device.id)) if device.hostname is not None: if len(device.hostname.strip()) != 0: try: if apic.networkdevice.getRunningConfigById( networkDeviceId=device.id) is not None: deviceConfigResponse = apic.networkdevice.getRunningConfigById( networkDeviceId=device.id).response config_filename = "configs/" + device.hostname.split( '.')[0] + "-config" tmp_filename = "configs/tmp_file" if not fileExists(config_filename): print(config_filename, " DOESN'T EXIST!! write it") outputFile(config_filename, deviceConfigResponse) else: # print(config_filename, " ALREADY EXISTS!! is it differnet??") outputFile(tmp_filename, deviceConfigResponse) # if hashIsSame(tmp_filename, config_filename): if diffTheFiles2(tmp_filename, config_filename): print( "\t {0} ALREADY EXISTS - CONTENTS DIFFERENT!! zip it and write a new one" .format(config_filename)) zipThisFile(config_filename, d.isoformat()) outputFile(config_filename, deviceConfigResponse) else: print( "\t {0} ALREADY EXISTS - CONTENTS THE SAME!! leave it alone" .format(config_filename)) except requests.exceptions.HTTPError as exc_info: print(exc_info) else: print("\t", "Device has no hostname?!")
def main(): platform_obj_list = [] device_dictionary = build_device_dict() parser = ArgumentParser(description='Arguments for running oneLiner.py') parser.add_argument('-c', '--csv', required=False, action='store', help='Location of CSV file') args = parser.parse_args() if args.csv: ssh_username = input("SSH username: "******"r") as file: reader = csv.DictReader(file) for device_row in reader: device_detail = process_csv_device(device_row, ssh_username, ssh_password) if device_detail is not None: add_platform(platform_obj_list, device_detail[0], device_detail[1], device_detail[2], None, device_detail[3]) else: apic = login() all_devices_response = apic.networkdevice.getAllNetworkDevice() for device in all_devices_response.response: if device.platformId is not None: os_type = determine_platform2(device.platformId.split()[0], device_dictionary) add_platform(platform_obj_list, device.platformId.replace(" ", "").split(","), device.softwareVersion, device.hostname, device.id, os_type) print_platform_object_count(platform_obj_list) print("Pickle the data!") with open("data.dmp", "wb") as output: pickle.dump(platform_obj_list, output, 2)
def main(): apic = login() platform_obj_list = [] device_dictionary = build_device_dict() all_devices_response = apic.networkdevice.getAllNetworkDevice() for device in all_devices_response.response: if device.platformId is not None: os_type = determine_platform2(device.platformId.split()[0], device_dictionary) add_platform(platform_obj_list, device.platformId.replace(" ", "").split(","), device.softwareVersion, device.hostname, device.id, os_type) print_platform_object_count(platform_obj_list) print("Pickle the data!") with open("data.dmp", "wb") as output: pickle.dump(platform_obj_list, output, 2)
def main(): parser = argparser() args = parser.parse_args() apic = login(args) d = date.today() device_str = "" refresh = False using_parser = False if args.cluster or args.username or args.password: using_parser = True ssh_username = args.username or None ssh_password = args.password or None ftp_ip = args.ftpip or None ftp_username = args.ftpusername or None ftp_password = args.ftppassword or None # client = None ssh_username_prompt = 'SSH Username[{}]: '.format( ssh_username) if ssh_username else 'SSH Username: '******'SSH password[{}]: '.format( ssh_password) if ssh_password else 'SSH Password: '******'FTP IP address[{}]: '.format( ftp_ip) if ftp_ip else 'FTP IP address: ' ftp_username_prompt = 'FTP username[{}] '.format( ftp_username) if ftp_username else 'FTP Username: '******'FTP password[{}] '.format( ftp_password) if ftp_password else 'FTP Password: '******'Password: '******'Password: ') or ftp_password network_device_list = [] device_dictionary = build_device_dict() ftp_session = create_ftp_connection(ftp_ip, ftp_username, ftp_password) prepare_ftp_destination2(ftp_session, "licenseHarvest/{0}".format(d.isoformat())) tag_id = create_apic_device_tag(apic, "licensed") print("TAG name: {0}, Tag ID: {1}".format("licensed", tag_id)) if refresh: device_ids = get_apic_tag_association(apic, "licensed") print("Performing a refresh for {0} APIC-EM network devices".format( len(device_ids))) else: device_ids = get_apic_tag_association(apic, None) print("Performing a complete audit of {0} APIC-EM network devices". format(len(device_ids))) for device_id in device_ids: apic_response = apic.networkdevice.getNetworkDeviceById(id=device_id) device = apic_response.response if device.platformId is None: break # if device.platformId.find("N5K-") != -1: if device.platformId.find("WS-C3850") != -1: ssh_session = SSHSession(ip_address=device.managementIpAddress, username=ssh_username, secret=ssh_password, enable=None) current_device = NetworkDevice( device.id, device.hostname, determine_platform2(device.platformId, device_dictionary), determine_ip_vrf(ssh_session, device.managementIpAddress)) get_license_state(ssh_session, current_device) print(current_device) device_str += str(current_device) + "\n" if len(current_device.licenses) > 0: backup_license_files(ssh_session, current_device, ftp_ip, ftp_username, ftp_password, "licenseHarvest", d.isoformat()) apply_apic_device_tag(apic, device, tag_id) network_device_list.append(current_device) try: ftp_session.voidcmd("NOOP") except all_errors as e: print(e) print("Retrying FTP connection...") ftp_session = create_ftp_connection(ftp_ip, ftp_username, ftp_password) ftp_session.cwd("licenseHarvest/{0}".format(d.isoformat())) try: print(ftp_session.pwd()) with open("temp.txt", "w") as text_file: text_file.write(device_str) ftp_session.storlines("STOR device_details.txt", open("temp.txt", "br")) finally: os.remove("temp.txt") ftp_session.quit()