cisco_fmc_api_via_excel_operations
192 строки · 5.8 Кб
1#!/usr/bin/python3
2from pathlib import Path3import yaml4import requestToken as token # requestToken is function from another file requestToken.py (should be in the same folder)5import getpass6# import constants # constants import from the separate file constants.py
7import time8# from datetime import datetime
9import logging10
11
12logging.basicConfig(format='%(threadName)s %(name)s %(levelname)s: %(message)s',level=logging.INFO)13
14
15fmc_ip = str()16auth_header = dict()17headers_json = dict()18global_domain_uuid = dict()19all_domains_json = dict()20all_obj_domain = dict()21all_ids_domain = dict()22all_devices = dict()23all_detailed_networkgroups = dict()24system_hosts = list()25system_networks = list()26system_networkgroups = list()27system_objects = list()28system_ports = list()29object_types = list()30start_time = float()31api_counter = int()32auth_token_start = float()33input_xlsx = str()34output_xlsx = str()35output_acp_xlsx = str()36diff_before_filename = str()37diff_after_filename = str()38sorted_sheets = list()39
40# OBJECT_HOST_NAME_START = constants.object_host
41# OBJECT_SUBNET_NAME_START = constants.object_subnet
42# OBJECT_RANGE_NAME_START = constants.object_range
43# OBJECT_GROUP_NAME_START = constants.object_group
44
45
46def init():47''' Set global variables to access FMC and its credentials'''48global fmc_ip49global auth_header50global headers_json51global global_domain_uuid52
53global system_hosts54global system_networks55global system_networkgroups56global system_objects57global system_ports58global object_types59
60global start_time61global api_counter62global auth_token_start63
64global all_domains_json65global all_obj_domain66global all_ids_domain67global all_devices68global all_detailed_networkgroups69
70global input_xlsx71global output_xlsx72global diff_before_filename73global diff_after_filename74global output_acp_xlsx75
76global sorted_sheets77
78fmc_ip = str()79headers_json = dict()80auth_header = dict()81auth_token_path = "/api/fmc_platform/v1/auth/generatetoken"82
83input_xlsx = 'FMC_objects.xlsx'84output_xlsx = 'FMC_downloaded_objects.xlsx'85output_acp_xlsx = 'FMC_ACP_rules_downloaded.xlsx'86diff_before_filename = 'outputs/diff_before_FMC_objects'87diff_after_filename = 'outputs/diff_after_FMC_objects'88
89start_time = time.time()90auth_token_start = time.time()91api_counter = 092
93
94if Path('fmc_credentials.yaml').is_file():95credentials_vars_file = 'fmc_credentials.yaml'96credentials_vars = read_credentials(credentials_vars_file)97fmc_ip = credentials_vars['fmc_ip']98''' call the token generating function and populate our header '''99auth_header = token.get_token(100credentials_vars['fmc_ip'],101auth_token_path,102credentials_vars['username'],103credentials_vars['password'],104
105)106else:107fmc_ip = input('enter FMC ip address: ')108username = input('enter api username: ')109password = getpass.getpass(prompt='enter api password: ')110''' call the token generating function and populate our header '''111auth_header = token.get_token(112fmc_ip,113auth_token_path,114username,115password
116)117# else:118# logging.info(f'no input file {input_xlsx} found ')119# raise FileNotFoundError(120# errno.ENOENT, os.strerror(errno.ENOENT), input_xlsx)121
122system_hosts = ['any-ipv6']123system_networks = ['any-ipv4',124'IPv4-Benchmark-Tests',125'IPv4-Link-Local',126'IPv4-Multicast',127'IPv4-Private-10.0.0.0-8',128'IPv4-Private-172.16.0.0-12',129'IPv4-Private-192.168.0.0-16',130'IPv6-IPv4-Mapped',131'IPv6-Link-Local',132'IPv6-Private-Unique-Local-Addresses',133'IPv6-to-IPv4-Relay-Anycast']134system_networkgroups = ['IPv4-Private-All-RFC1918', 'any']135system_ports = ['AOL',136'Bittorrent',137'DNS_over_TCP',138'DNS_over_UDP',139'FTP',140'HTTP',141'HTTPS',142'IMAP',143'LDAP',144'NFSD-TCP',145'NFSD-UDP',146'NTP-TCP',147'NTP-UDP',148'POP-2',149'POP-3',150'RADIUS',151'RIP',152'SIP',153'SMTP',154'SMTPS',155'SNMP',156'SSH',157'SYSLOG',158'TCP_high_ports',159'TELNET',160'TFTP',161'YahooMessenger_Voice_Chat_TCP',162'YahooMessenger_Voice_Chat_UDP',163'Yahoo_Messenger_Messages', ]164
165system_objects = list()166system_objects += system_hosts167system_objects += system_networks168system_objects += system_networkgroups169
170object_types = [171'hosts', 'ranges', 'networks', 'urls', 'networkgroups', 'urlgroups']172headers_json = {"Accept": "application/json", "Content-Type": "application/json",173"X-auth-access-token": auth_header['X-auth-access-token'],174"X-auth-refresh-token": auth_header['X-auth-refresh-token']}175
176global_domain_uuid = auth_header['DOMAIN_UUID']177
178
179def check_if_file_exist(filename):180if Path(filename).is_file():181return True182else:183return False184
185def read_credentials(credentials_yaml_file):186with open(credentials_yaml_file) as src:187credentials = yaml.safe_load(src)188return credentials189
190
191if __name__ == "__main__":192init()193