-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfortigate5.py
More file actions
270 lines (231 loc) · 6.48 KB
/
Copy pathfortigate5.py
File metadata and controls
270 lines (231 loc) · 6.48 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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
#!/usr/bin/env python
__author__ = 'dwilliams'
import iptools
avaimodels = ['1. 200D', '2. 100D', '3. 90D', '4. 60D', '5. 30D']
template200d = "/usr/local/etc/Fortigate/200d_template"
template100d = "/usr/local/etc/Fortigate/100d_template"
template90d = "/usr/local/etc/Fortigate/90d_template"
template60d = "/usr/local/etc/Fortigate/60d_template"
template30d = "/usr/local/etc/Fortigate/30d_template"
def replace(file, newfile, mydict):
# Read contents from file as a single string
file_handle = open(file, 'r')
file_string = file_handle.read()
file_handle.close()
# Search and replace using dictionary keys
for k, v in mydict.iteritems():
file_string = file_string.replace(k, v)
# Write contents to file.
# Using mode 'w' truncates the file.
file_handle = open(newfile, 'w')
file_handle.write(file_string)
file_handle.close()
print "created file "+hostname+".conf"
def get_dg():
global extIPandMask
global defaultgateway
defaultgateway = raw_input('Default Gateway IP: ')
r = iptools.IpRangeList(extIPandMask).__contains__(defaultgateway)
x = extIPandMask.split('/')
address = x[0]
if r is False:
print 'Make sure it is a valid IP within the external IP network'
get_dg()
if defaultgateway == address:
print 'Your gateway cannot be the same as your local IP'
get_dg()
else:
pass
def validateEmail(a):
sep=[x for x in a if not x.isalpha()]
sepjoined=''.join(sep)
## sep joined must be ..@.... form
if sepjoined.strip('.') != '@': return False
end=a
for i in sep:
part,i,end=end.partition(i)
if len(part)<2: return False
return True
def validate_cidr_mask(s):
if not s.isdigit():
print "Invalid or missing cidr mask"
return False
i = int(s)
if i < 1 or i > 32:
return False
else:
return True
def validate_ipv4_format(s):
a = s.split('.')
if len(a) != 4:
print 'Not a Valid IP Address'
return False
for x in a:
if not x.isdigit():
return False
i = int(x)
if i < 0 or i > 255:
return False
return True
def validatemod(s):
if int(s) > 5:
print('Invalid Entry')
return False
def outputipv4(s):
x = iptools.ipv4.cidr2block(s)
n = x[0]
return n
def modselect(model):
global models
model = raw_input('Enter Number: ')
if model == '1':
return template200d
elif model == '2':
return "template100d"
elif model == '3':
return "template90d"
elif model == '4':
return "template60d"
elif model == '5':
return "template30d"
else:
v = validatemod(model)
if v == False:
modselect(model)
else:
pass
def validate_ip_cidr_format(s):
if iptools.ipv4.validate_cidr(s) == False:
print "Invalid cidr format"
getexternalIPMask(i)
else:
pass
def validate_int_ip_cidr_format(s):
if iptools.ipv4.validate_cidr(s) == False:
print "Invalid cidr format"
getinternalip()
else:
pass
def getexternalIPMask():
global extIPandMask
extIPandMask = raw_input('External IP Address and Mask(cidr): ')
validate_ip_cidr_format(extIPandMask)
x = extIPandMask.split('/')
address = x[0]
cidr = x[1]
v = validate_ipv4_format(address)
c = validate_cidr_mask(cidr)
if v == False:
getexternalIPMask()
elif c == False:
print "Invalid Mask"
getexternalIPMask()
else:
pass
print('Select the number corresponding to the model being provisioned')
for i in avaimodels:
print i
def getinternalip():
global intIPandMask
global internalnet
intIPandMask = raw_input('Internal IP Address and Mask(cidr): ')
validate_int_ip_cidr_format(intIPandMask)
x = intIPandMask.split('/')
address = x[0]
cidr = x[1]
v = validate_ipv4_format(address)
c = validate_cidr_mask(cidr)
internalnet = outputipv4(intIPandMask)+'/'+cidr
if v == False:
getinternalip()
elif c == False:
print "Invalid Mask"
getinternalip()
else:
pass
def getdns1():
global DNS1
DNS1 = raw_input('Primary DNS Server IP: ')
validation = validate_ipv4_format(DNS1)
if validation == False:
print 'Not a valid IP address'
getdns1()
else:
pass
def getdns2():
global DNS2
DNS2 = raw_input('Secondary DNS Server IP: ')
validation = validate_ipv4_format(DNS2)
if validation == False:
print 'Not a valid IP address'
getdns2()
else:
pass
def getemail():
global emailaddr
emailaddr = raw_input('Source Email Address [email@example.com]: ')
x = validateEmail(emailaddr)
if x == False:
print "Email is not formatted properly"
getemail()
else:
pass
def validateEmail(a):
sep=[x for x in a if not x.isalpha()]
sepjoined=''.join(sep)
## sep joined must be ..@.... form
if sepjoined.strip('.') != '@': return False
end=a
for i in sep:
part,i,end=end.partition(i)
if len(part)<2: return False
return True
templatefile = ()
defaultgateway = ()
extIPandMask = ()
models = ()
intIPandMask = ()
DNS1 = ()
DNS2 = ()
internalnet = ()
emailaddr = ()
models = modselect(models)
hostname = raw_input('Hostname [SID-Profile]: ')
getexternalIPMask()
get_dg()
getinternalip()
getdns1()
getdns2()
NTP1 = DNS1
NTP2 = DNS2
domain = raw_input('Domain name [example.com]: ')
getemail()
password = raw_input('Enter a password containing 1 number, uppercase letter, lowercase letter, and 1 special character: ')
snmpcomm = raw_input('Enter a password to be used as the snmp read community: ')
SID = raw_input('Service ID (SID): ')
if models == "template200d":
templatefile = template200d
elif models == "template100d":
templatefile = template100d
elif models == "template90d":
templatefile = template90d
elif models == "template60d":
templatefile = template60d
elif models == "template30d":
templatefile = template30d
repldict = {"#HOSTNAME#":hostname,
"#EXTERNALIPADDRESS#":extIPandMask,
"#InternalIPADDRESS#":intIPandMask,
"#InternalNetwork#":internalnet,
"#DNS1#":DNS1,
"#DNS2#":DNS2,
"#NTP1#":NTP1,
"#NTP2#":NTP2,
"#InternalNetwork2#":internalnet,
"#DOMAIN1#":domain,
"#SRCEMAIL#":emailaddr,
"#DEFAULTGW#":defaultgateway,
"#PASSWORD#":password,
"#snmp_community#":snmpcomm,
"#SID#":SID}
replace(templatefile, hostname+'.conf', repldict)