-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCode.py
More file actions
174 lines (144 loc) · 3.04 KB
/
Copy pathCode.py
File metadata and controls
174 lines (144 loc) · 3.04 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#! /usr/bin/python3
import sys
import os
f_name = 'InternalRule.txt'
def address(file_name):
table_add = []
table_ip=[]
start1 = "firewall address"
end1 = "end"
nxt1 = "next"
sig1='edit'
sig4='subnet'
wrd1=''
wrd2=''
file_cp = open(file_name)
while True:
read_l = file_cp.readline()
if start1 in read_l:
break
while not ((end1 in read_l)&(len(read_l)==4)):
read_l = file_cp.readline()
if sig1 in read_l:
wrd1 = (read_l.split(sig1,1)[1]).strip("\n")
if sig4 in read_l:
wrd2 = (read_l.split(sig4,1)[1]).strip("\n")
if ((nxt1 in read_l) & (len(read_l)==9)):
table_add.append(wrd1)
table_ip.append(wrd2)
wrd1=''
wrd2=''
print('Done')
file_cp.close()
return (table_add,table_ip)
print ("Hello Fortinet")
start = "config firewall policy"
end = "end"
nxt = "next"
#KEYs
key1='edit'
key2='name'
key3='srcintf'
key4='srcaddr'
key5='dstintf'
key6='dstaddr'
key7='action'
key8='service'
key9='logtraffic'
key10='status'
#TABLE
title = "No#,Name,SrcINT,SrcIP,S_IP,DstINT,DstIP,D_IP,Action,Service,Log,Status"
a1=""
a2=""
a3=""
a4=""
a4_1=""
a5=""
a6=""
a6_1=""
a7=""
a8=""
a9=""
a10=""
s_ip=[]
d_ip=[]
db=address(f_name)
file = open(f_name)
output = open('PolicyBase.csv', 'w')
output.write(title + '\n')
while True:
line = file.readline()
if start in line:
print('start')
break
while not ((end in line)&(len(line)==4)):
line = file.readline()
if key1 in line:
a1 = (line.split(key1,1)[1]).strip("\n")
if key2 in line:
a2 = (line.split(key2,1)[1]).strip("\n")
if key3 in line:
a3 = (line.split(key3,1)[1]).strip("\n")
if key4 in line:
a4 = (line.split(key4,1)[1]).strip("\n")
a4_1 = a4.split()
i=0
while(i<len(a4_1)):
j=0
while (j<len(db[0])):
if a4_1[i] in db[0][j]:
s_ip.append(db[1][j])
i=i+1
break
else:
j=j+1
if (j==len(db[0])):
s_ip.append(a4_1[i])
i=i+1
if key5 in line:
a5 = (line.split(key5,1)[1]).strip("\n")
if key6 in line:
a6 = (line.split(key6,1)[1]).strip("\n")
a6_1 = a6.split()
i=0
while(i<len(a6_1)):
j=0
while (j<len(db[0])):
if a6_1[i] in db[0][j]:
d_ip.append(db[1][j])
i=i+1
break
else:
j=j+1
if (j==len(db[0])):
d_ip.append(a6_1[i])
i=i+1
if key7 in line:
a7 = (line.split(key7,1)[1]).strip("\n")
if key8 in line:
a8 = (line.split(key8,1)[1]).strip("\n")
if key9 in line:
a9 = (line.split(key9,1)[1]).strip("\n")
if key10 in line:
a10 = (line.split(key10,1)[1]).strip("\n")
if ((nxt in line) & (len(line)==9)):
row=",".join((a1,a2,a3,a4,str(s_ip),a5,a6,str(d_ip),a7,a8,a9,a10))
output.write(row + '\n')
a1=""
a2=""
a3=""
a4=""
a4_1=""
a5=""
a6=""
a6_1=""
a7=""
a8=""
a9=""
a10=""
row=""
s_ip=[]
d_ip=[]
print("end of line")
file.close()
output.close()