-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdiscover.py
246 lines (207 loc) · 9.07 KB
/
discover.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
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#@Author: Carl Dubois
#@Email: [email protected]
#@Description: BIGIQ / BIGIP Trust and Discover LTM
#@Product: BIGIQ
#@VersionIntroduced: 5.0.0
import sys
import simplejson as json
import base64
import string
import os.path
import httplib
import time
def device_trust(config):
print "\n"
print '####Begin a trust task between BIGIQ and BIGIP####'
## Connection to localhost:8100
connection = httplib.HTTPConnection('localhost:8100')
connection.set_debuglevel(0) # user can set debug level
## Request DATA
data = {'address':config['bigip'],'userName':'admin', 'password':'admin', 'clusterName':'', 'useBigiqSync':'false'}
## Request POST
connection.request('POST', '/cm/global/tasks/device-trust', json.dumps(data), config['headers'])
## Parse Response
response = connection.getresponse()
print response.status, response.reason
print response.read()
time.sleep(1)
if response.status in [200, 202]:
print "Trust task successfully started."
else:
return False
## get status of task
t=1
while True:
connection.connect()
connection.request('GET', '/cm/global/tasks/device-trust', None, config['headers'])
response = connection.getresponse()
j_out = json.loads(response.read())
# Test current step to verify trust is complete.
for item in j_out['items']:
if item['address'] == config['bigip']:
if item['currentStep'] == 'DONE':
return True, item['machineId']
else:
print str(t) + " sec"
time.sleep(1)
t+=1
continue
# If not DONE is 30 sec fail.
if t>=30:
return False
def device_discover(config, devid, adc=None, afm=None, asm=None):
print "\n"
print '####Discover modules selected LTM, AFM, ASM via resource BIGIP####'
## Connection to localhost:8100
connection = httplib.HTTPConnection('localhost:8100')
connection.set_debuglevel(0) # user can set debug level
## Reference link to BIGIP
link = 'cm/system/machineid-resolver/' + devid
## Request DATA
adc_data = {'deviceReference': {"link": link}, 'moduleList': [{'module': 'adc_core'}], 'userName': config['username'], 'password': config['password'], 'rootUser': config['root_username'], 'rootPassword' : config['root_password'], 'automaticallyUpdateFramework' : 'true'}
adc_afm_data = {'deviceReference': {"link": link}, 'moduleList': [{'module': 'adc_core'}, {'module': 'firewall'},{'module': 'security_shared'}], 'userName': config['username'], 'password': config['password'], 'rootUser': config['root_username'], 'rootPassword' : config['root_password'], 'automaticallyUpdateFramework' : 'true'}
adc_asm_data = {'deviceReference': {"link": link}, 'moduleList': [{'module': 'adc_core'}, {'module': 'asm'},{'module': 'security_shared'}], 'userName': config['username'], 'password': config['password'], 'rootUser': config['root_username'], 'rootPassword' : config['root_password'], 'automaticallyUpdateFramework' : 'true'}
all_data = {'deviceReference': {"link": link}, 'moduleList': [{'module': 'adc_core'}, {'module': 'firewall'}, {'module': 'asm'},{'module': 'security_shared'}], 'userName': config['username'], 'password': config['password'], 'rootUser': config['root_username'], 'rootPassword' : config['root_password'], 'automaticallyUpdateFramework' : 'true'}
## Request POST
if config['module'] == 'adc':
connection.request('POST', '/cm/global/tasks/device-discovery', json.dumps(adc_data), config['headers'])
elif config['module'] == 'afm':
connection.request('POST', '/cm/global/tasks/device-discovery', json.dumps(adc_afm_data), config['headers'])
elif config['module'] == 'asm':
connection.request('POST', '/cm/global/tasks/device-discovery', json.dumps(adc_asm_data), config['headers'])
elif config['module'] == 'all':
connection.request('POST', '/cm/global/tasks/device-discovery', json.dumps(all_data), config['headers'])
else:
print "Module {0} is not avalible.".format(config['module'])
return False
## Parse Response
response = connection.getresponse()
print response.status, response.reason
print response.read()
time.sleep(1)
if response.status in [200, 202]:
print "Device discover task successfully started."
else:
return False
## Get status of task
t=1
while True:
connection.connect()
connection.request('GET', '/cm/global/tasks/device-discovery', None, config['headers'])
response = connection.getresponse()
j_out = json.loads(response.read())
# Test current step to verify trust is complete.
for item in j_out['items']:
if item['deviceReference']['link'] == link and item['status'] == 'STARTED':
time.sleep(1)
if t>=40:
return True
else:
t+=1
print str(t) + " sec"
def device_import(config, devid):
print "\n"
print '####Import module configuration selected LTM, AFM, ASM via resource BIGIP####'
uri = []
## Connection to localhost:8100
connection = httplib.HTTPConnection('localhost:8100')
connection.set_debuglevel(0) # user can set debug level
## Reference link to BIGIP
link = 'cm/system/machineid-resolver/' + devid
data = {'deviceReference': {'link': link}, 'uuid': devid, 'deviceUri': 'http://' + config['bigip'] + ':443', 'machineId': devid}
if config['module'] == 'afm':
print "ADC and AFM import"
uri_adc = '/cm/adc-core/tasks/declare-mgmt-authority'
uri.append(uri_adc)
uri_afm = '/cm/firewall/tasks/declare-mgmt-authority'
uri.append(uri_afm)
elif config['module'] == 'asm':
print "ADC and ASM import"
uri_adc = '/cm/adc-core/tasks/declare-mgmt-authority'
uri.append(uri_adc)
uri_asm = '/cm/asm/tasks/declare-mgmt-authority'
uri.append(uri_asm)
else:
print "ADC, AFM and ASM import"
uri_adc = '/cm/adc-core/tasks/declare-mgmt-authority'
uri.append(uri_adc)
uri_afm = '/cm/firewall/tasks/declare-mgmt-authority'
uri.append(uri_afm)
uri_asm = '/cm/asm/tasks/declare-mgmt-authority'
uri.append(uri_asm)
## POST
for i in range(len(uri)):
connection.request('POST', uri[i], json.dumps(data), config['headers'])
## Parse Response
response = connection.getresponse()
print response.status, response.reason
print response.read()
time.sleep(1)
if response.status in [200, 202]:
print "Device import task successfully started."
else:
return False
## Get status of task
t=1
flag=0
while True:
connection.connect()
connection.request('GET', uri[i], None, config['headers'])
response = connection.getresponse()
j_out = json.loads(response.read())
# Test current step to verify trust is complete.
for item in j_out['items']:
if item['deviceIp'] == config['bigip']:
if item['currentStep'] == 'DONE':
flag=1
else:
print str(t) + " sec"
time.sleep(1)
t+=1
continue
# If not DONE is 60 sec fail.
if flag==1:
break
elif t==60:
return False
return True
if __name__ == '__main__':
result = {}
config = {}
config['username'] = 'admin'
config['password'] = 'admin'
config['root_username'] = 'root'
config['root_password'] = 'default'
try:
config['bigip'] = sys.argv[1]
except:
print "Please specify the BIGIP address in which you want to form trust. ex. ./discover.py x.x.x.x"
sys.exit(1)
try:
config['module'] = sys.argv[2]
except:
print "Please tell me via command line which BIGIP module you wish to discover. Thank you"
sys.exit(1)
#==========================
# Header used for httplib
#==========================
## Header used in the connection request
config['headers'] = {"Authorization" : "Basic %s" % base64.b64encode(config['username'] + ':' + config['password']), "Content-Type" : "application/json"}
#==========================
# device_trust
#==========================
result_trust = device_trust(config)
#==========================
# device_discovery
#==========================
result_discovery = device_discover(config, result_trust[1])
#==========================
# ltm_import
#==========================
result_import = device_import(config, result_trust[1])
if result_trust[0] == True and result_discovery == True and result_import == True:
print "Trust, Discovery and Import successfully established."
else:
print "Trust and Discovery failed to establish"