-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathextract_firewalls_configuration.py
96 lines (79 loc) · 3.73 KB
/
extract_firewalls_configuration.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
90
91
92
93
94
95
from netmiko import ConnectHandler
def ReadFile(inFile):
#function to read .txt files into python list
with open(inFile, "r") as f:
content = f.readlines()
content = [x.strip() for x in content]
return (content)
def Parse_FW_configuration(FW):
# read FW configuration/routes and add them to .txt files
FW_name = FW[0]
FW_IP = FW[1]
FW_Platform = FW[2]
FW_Username = FW[3]
FW_Password = FW[4]
#To be Tested
if(FW_Platform == 'Fortinet'):
device = ConnectHandler(device_type='fortinet', ip= FW_IP , username=FW_Username, password=FW_Password)
device.send_command_timing('cli',2,1500)#to decide where to start(2,1500 ) used for delay and waiting time
configLines = device.send_command_timing('show full-configuration',2,1500)
routeLines = device.send_command_timing('get router info routing-table details',2,1500)
configLines = str(configLines)
configFile = open(FW_name+".txt" , "w")
configFile.write(configLines)
routeFile = open(FW_name+"_routes.txt" , "w")
routeFile.write(routeLines)
configFile.close()
routeFile.close()
device.disconnect()
if(FW_Platform == 'Juniper Junos'):
device = ConnectHandler(device_type='juniper', ip= FW_IP , username=FW_Username, password=FW_Password)
device.send_command_timing('cli',2,1500)#to decide where to start(2,1500 ) used for delay and waiting time
configLines = device.send_command_timing('show configuration | display set | no-more',2,1500)
routeLines = device.send_command_timing('show route | no-more',2,1500)
configLines = str(configLines)
configFile = open(FW_name+".txt" , "w")
configFile.write(configLines)
routeFile = open(FW_name+"_routes.txt" , "w")
routeFile.write(routeLines)
configFile.close()
routeFile.close()
device.disconnect()
if(FW_Platform == 'Juniper ScreenOS'):
device = ConnectHandler(device_type='juniper', ip= FW_IP , username=FW_Username, password=FW_Password)
configLines = device.send_command_timing('get config',2,1500)#to decide where to start(2,1500 ) used for delay and waiting time
configLines = str(configLines)
routeLines = device.send_command_timing('get route',2,1500)
routeLines = str(routeLines)
configFile = open(FW_name+".txt" , "w")
configFile.write(configLines)
routeFile = open(FW_name+"_routes.txt" , "w")
routeFile.write(routeLines)
configFile.close()
routeFile.close()
device.disconnect()
#extract configuration and routes from the FW :
firewallsData = ReadFile("FirewallsData.csv")
FWsDic = {}
unParsedFW = []
#print firewallsData
for line in firewallsData[1:]: #start from 1 to neglect the header line
name, ip, platform, UserName, Password = line.split(',')
FWsDic[name+"_"+ip] = [name+"_"+ip, ip, platform, UserName, Password] #created FW dictionary with all needed Data
unParsedFW.append(name+"_"+ip)
for FW in FWsDic.values():
print FW
#raw_input("FW Date read")
while len(unParsedFW) != 0:# for the code to retry till all the FW configuration is completed
for fw in unParsedFW:
try:
print("try ",fw)
Parse_FW_configuration(FWsDic[fw])
unParsedFW.remove(fw)
print(fw,"Done")
print "Remaining FWs List:"
print unParsedFW
except Exception as error:
print(error)
print(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>error "+str(fw))
continue