cisco_fmc_api_via_excel_operations

Форк
0
192 строки · 5.8 Кб
1
#!/usr/bin/python3
2
from pathlib import Path
3
import yaml
4
import requestToken as token # requestToken is function from another file requestToken.py (should be in the same folder)
5
import getpass
6
# import constants  # constants import from the separate file constants.py
7
import time
8
# from datetime import datetime
9
import logging
10

11

12
logging.basicConfig(format='%(threadName)s %(name)s %(levelname)s: %(message)s',level=logging.INFO)
13

14

15
fmc_ip = str()
16
auth_header = dict()
17
headers_json = dict()
18
global_domain_uuid = dict()
19
all_domains_json = dict()
20
all_obj_domain = dict()
21
all_ids_domain = dict()
22
all_devices = dict()
23
all_detailed_networkgroups = dict()
24
system_hosts = list()
25
system_networks = list()
26
system_networkgroups = list()
27
system_objects = list()
28
system_ports = list()
29
object_types = list()
30
start_time = float()
31
api_counter = int()
32
auth_token_start = float()
33
input_xlsx = str()
34
output_xlsx = str()
35
output_acp_xlsx = str()
36
diff_before_filename = str()
37
diff_after_filename = str()
38
sorted_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

46
def init():
47
    ''' Set global variables to access FMC and its credentials'''
48
    global fmc_ip
49
    global auth_header
50
    global headers_json
51
    global global_domain_uuid
52

53
    global system_hosts
54
    global system_networks
55
    global system_networkgroups
56
    global system_objects
57
    global system_ports
58
    global object_types
59
    
60
    global start_time
61
    global api_counter
62
    global auth_token_start
63
    
64
    global all_domains_json
65
    global all_obj_domain
66
    global all_ids_domain
67
    global all_devices
68
    global all_detailed_networkgroups
69
    
70
    global input_xlsx
71
    global output_xlsx
72
    global diff_before_filename
73
    global diff_after_filename
74
    global output_acp_xlsx
75
    
76
    global sorted_sheets
77
    
78
    fmc_ip = str()
79
    headers_json = dict()
80
    auth_header = dict()
81
    auth_token_path = "/api/fmc_platform/v1/auth/generatetoken"
82
    
83
    input_xlsx = 'FMC_objects.xlsx'
84
    output_xlsx = 'FMC_downloaded_objects.xlsx'
85
    output_acp_xlsx = 'FMC_ACP_rules_downloaded.xlsx'
86
    diff_before_filename = 'outputs/diff_before_FMC_objects'
87
    diff_after_filename = 'outputs/diff_after_FMC_objects'
88
    
89
    start_time = time.time()
90
    auth_token_start = time.time()
91
    api_counter = 0
92
    
93
    
94
    if Path('fmc_credentials.yaml').is_file():
95
        credentials_vars_file = 'fmc_credentials.yaml'
96
        credentials_vars = read_credentials(credentials_vars_file)
97
        fmc_ip = credentials_vars['fmc_ip']
98
        ''' call the token generating function and populate our header '''
99
        auth_header = token.get_token(
100
            credentials_vars['fmc_ip'],
101
            auth_token_path,
102
            credentials_vars['username'],
103
            credentials_vars['password'],
104
            
105
        )
106
    else:
107
        fmc_ip = input('enter FMC ip address: ')
108
        username = input('enter api username: ')
109
        password = getpass.getpass(prompt='enter api password: ')
110
        ''' call the token generating function and populate our header '''
111
        auth_header = token.get_token(
112
            fmc_ip,
113
            auth_token_path,
114
            username,
115
            password
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
    
122
    system_hosts = ['any-ipv6']
123
    system_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']
134
    system_networkgroups = ['IPv4-Private-All-RFC1918', 'any']
135
    system_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
    
165
    system_objects = list()
166
    system_objects += system_hosts
167
    system_objects += system_networks
168
    system_objects += system_networkgroups
169

170
    object_types = [
171
        'hosts', 'ranges', 'networks', 'urls', 'networkgroups', 'urlgroups']
172
    headers_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

176
    global_domain_uuid = auth_header['DOMAIN_UUID']
177

178

179
def check_if_file_exist(filename):
180
    if Path(filename).is_file():
181
        return True
182
    else:
183
        return False
184

185
def read_credentials(credentials_yaml_file):
186
    with open(credentials_yaml_file) as src:
187
        credentials = yaml.safe_load(src)
188
    return credentials
189

190

191
if __name__ == "__main__":
192
    init()
193

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.