forked from sambyers/fmcapi-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rmv_objects.py
89 lines (64 loc) · 2.73 KB
/
rmv_objects.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
from fmcapi import *
from re import search
from argparse import ArgumentParser
def get_args():
'''
Get args from CLI.
'''
parser = ArgumentParser(description='Get arguments for rmv_objects script.')
parser.add_argument('server', type=str, help='IP or DNS of the FMC Server')
parser.add_argument('username', type=str, help='Username for FMC.')
parser.add_argument('password', type=str, help='Password for FMC.')
parser.add_argument('object_type', type=str, help='Object type to remove. protocolport, ipnetwork, networkgroup, iphost, iprange are supported today.')
parser.add_argument('regex', type=str, help='The regular expression to match objects to remove. Use double quotes around the pattern.')
args = parser.parse_args()
return args
def del_obj(obj1, result, regex):
print('Let\'s delete some stuff! -->')
del_count = 0
for k in result['items']:
if search(regex, k['name']):
print("Deleting {}".format(k['name']))
try:
obj1.delete(id=k['id'])
del_count = del_count + 1
except TypeError:
print("We can't delete objects that are in use.")
if del_count:
return del_count
def main():
args = get_args()
fmc_server = args.server
username = args.username
password = args.password
object_type = args.object_type
regex = args.regex
object_type.lower()
implemented_objects = ('protocolport', 'ipnetwork', 'networkgroup', 'iphost', 'iprange')
if object_type in implemented_objects:
with FMC(host=fmc_server, username=username, password=password, autodeploy=False) as fmc1:
if object_type == 'protocolport':
obj1 = ProtocolPort(fmc=fmc1)
result = obj1.get()
del_count = del_obj(obj1, result, regex)
elif object_type == 'ipnetwork':
obj1 = IPNetwork(fmc=fmc1)
result = obj1.get()
del_count = del_obj(obj1, result, regex)
elif object_type == 'networkgroup':
obj1 = NetworkGroup(fmc=fmc1)
result = obj1.get()
del_count = del_obj(obj1, result, regex)
elif object_type == 'iphost':
obj1 = IPHost(fmc=fmc1)
result = obj1.get()
del_count = del_obj(obj1, result, regex)
elif object_type == 'iprange':
obj1 = IPRange(fmc=fmc1)
result = obj1.get()
del_count = del_obj(obj1, result, regex)
print("Deleted {} {} object(s).".format(del_count, object_type))
else:
print('Not an implemented object type.')
if __name__ == '__main__':
main()