-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharuba-cert-uploader.py
352 lines (309 loc) · 14.6 KB
/
aruba-cert-uploader.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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
import json
import os
import requests
import ssl
import requests
import xml.etree.ElementTree as ET
from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_v1_5 as Cipher_PKCS1_v1_5
import binascii
from xml.etree.ElementTree import Element, SubElement, tostring
from urllib.parse import urlparse
import datetime
class ArubaSwitch:
def __init__(self,hostName: str, port:int, user: str, password: str) -> None:
self.hostName = hostName
self.port = port
self.user = user
self.password = password
self.session = requests.Session()
self.magic=None
def __debug(self,message:str):
print(f'DEBUG { datetime.datetime.now()} {message}')
def _getURL(self, url:str):
self.__debug(f'GET {url}')
result = self.session.get(url)
self.__debug(f'GET Done.')
return result
def _getMagic(self):
if self.magic is None:
result = self._getURL(f'http://{self.hostName}/')
if result:
a = urlparse(result.url)
self.magic = a.path.split('hpe/')[0]
return self.magic
def encrypt_data(self,publicKey,data:str):
key = RSA.importKey(publicKey)
cipher = Cipher_PKCS1_v1_5.new(key)
return cipher.encrypt(data.encode())
def bin2hex(self,binStr):
return binascii.hexlify(binStr)
def _encryptionSettingsGetPasswordEncryptEnable(self,root: Element):
passwEncryptEnable = root.find('.//passwEncryptEnable')
if passwEncryptEnable is None:
return False
else:
passwEncryptEnable=passwEncryptEnable.text
return passwEncryptEnable == '1'
def _encryptionSettingsGetPublicKey(self,root:Element):
publicKey = root.find('.//rsaPublicKey')
if publicKey is None:
raise RuntimeError('Error publicKey missing from encyption settings')
return publicKey.text
def _encryptionSettingsGetLoginToken(self, root:Element):
loginToken = root.find('.//loginToken')
if loginToken is None:
raise RuntimeError('Could not get Login token!')
return loginToken.text
def _resultExtractStatus(self,resultStr):
root = ET.fromstring(resultStr)
statusCode = root.find('.//statusCode')
statusString = root.find('.//statusString')
if statusCode is not None:
statusCode = statusCode.text
else:
statusCode = 500
if statusString is not None:
statusString = statusString.text
else:
statusString = 'ERRORS'
return int(statusCode), statusString
def parseEncryptionSettings(self,hostname,xml,username,password):
root = ET.fromstring(xml)
if not self._encryptionSettingsGetPasswordEncryptEnable(root):
newPath = './system.xml?action=login&user=' + username + '&password=' + password + "&ssd=true&"
else:
publicKey = self._encryptionSettingsGetPublicKey(root)
loginToken = self._encryptionSettingsGetLoginToken(root)
res = self.encrypt_data(publicKey,f'user={username}&password={password}&ssd=true&token={ loginToken }&')
res = self.bin2hex(res)
res= res.decode()
newPath = './system.xml?action=login&cred=' + res
result = self._getURL(f'http://{hostname}/{newPath}')
if result.ok:
code, errorMessage = self._resultExtractStatus(result.text)
if code > 0:
raise RuntimeError(f'ERROR statusCode={code} {errorMessage} ')
return True
raise RuntimeError(f'ERROR failed to log on {result.text}')
def authenticate(self):
self.__debug('>> authenticate')
r = self._getURL(f'http://{self.hostName}/device/wcd?{{EncryptionSetting}}' )
if not r:
raise RuntimeError('ERROR authenticate - Could not load encryption settings from switch')
xml = r.text
return self. parseEncryptionSettings(self.hostName,xml,self.user,self.password)
def loadPrivateKey(self,privateKeyFile):
privateKey = []
with open(privateKeyFile,'r') as f:
privateKey = f.readlines()
privKey =''
for line in privateKey:
privKey=privKey+(line)
return privKey.strip()
def loadPublicKey(self,publicKeyFile):
publicKey = []
with open(publicKeyFile,'r') as f:
publicKey = f.readlines()
pubKey =''
for line in publicKey:
pubKey=pubKey+(line)
return pubKey.strip()
def loadCertificate(self,certificateFile):
certificate = []
with open(certificateFile,'r') as f:
certificate = f.readlines()
cert =''
for line in certificate:
cert=cert+(line )
return cert.strip()
def generateCertificateXML(self, privateKeyText, publicKeyText, certificateText):
deviceConfiguration = Element('DeviceConfiguration')
SSLCryptoCertificateImportList = SubElement(deviceConfiguration, "SSLCryptoCertificateImportList")
SSLCryptoCertificateImportList.set('action','set')
Entry = SubElement(SSLCryptoCertificateImportList,'Entry')
instance = SubElement(Entry,'instance')
instance.text='1'
certificate = SubElement(Entry,'certificate')
certificate.text = certificateText
publicKey = SubElement(Entry,'publicKey')
publicKey.text = publicKeyText
privateKeySSDType = SubElement(Entry,'privateKeySSDType')
privateKeySSDType.text='3'
privateKey = SubElement(Entry,'privateKey')
privateKey.text = privateKeyText
return tostring(deviceConfiguration).decode()
def uploadSSLCertificate(self, privateKeyFile, publicKeyFile, certificateFile):
privKey = self.loadPrivateKey(privateKeyFile)
pubKey = self.loadPublicKey(publicKeyFile)
cert = self.loadCertificate(certificateFile)
xml = self.generateCertificateXML(privKey,pubKey,cert)
xmlHeader="<?xml version=\'1.0\' encoding=\'utf-8\'?>"
data= (xmlHeader+xml)
url = f'http://{self.hostName}/{self._getMagic()}/hpe/wcd?{{EWSGlobalSetting}}{{SSLCryptoCertificateList}}{{SSLCryptoCertificateImportList}}'
result = self.session.post(url, data)
print(result.text)
def downloadStartupConfig(self):
result = self._getURL(f'http://{self.hostName}/{self._getMagic()}/hpe/http_download?action=3&ssd=4')
if result.ok:
with open(f'./{self.hostName}.startup.config.txt','wt') as f:
f.write(result.text)
def downloadRunningConfig(self):
result = self._getURL(f'http://{self.hostName}/{self._getMagic()}/hpe/http_download?action=2&ssd=4')
if result.ok:
with open(f'./{self.hostName}.running.config.txt','wt') as f:
f.write(result.text)
def parseMacTableXML(self,resultText):
responseData = ET.fromstring(resultText)
entries = responseData.findall('.//Entry')
print('Mac Table')
for entry in entries:
vlanName=entry.findtext('.//VLANName')
vlanID=entry.findtext('.//VLANID')
macAddress=entry.findtext('.//MACAddress')
interfaceType=entry.findtext('.//interfaceType')
interfaceName=entry.findtext('.//interfaceName')
if 'none' in interfaceName:
interfaceName = 'CPU'
addressType=entry.findtext('.//addressType')
description =''
if interfaceType == '0' and addressType == '4':
description = 'Management'
elif interfaceType == '1' and addressType == '3':
description = 'Learned'
print(f'port {interfaceName:3} (type {interfaceType}) {macAddress} (type={addressType}) vlan: {vlanID:4} {vlanName} {description}')
#print(tostring(entry))
def diagnostics_mac_table(self):
self.__debug('>> diagnostics_mac_table')
result = self._getURL(f'http://{self.hostName}/{self._getMagic()}/hpe/wcd?{{ForwardingTable}}&_=1234')
if result.ok:
self.parseMacTableXML(result.text)
def vlan_current_status(self):
self.__debug('>> vlan_current_status')
result = self._getURL(f'http://{self.hostName}/{self._getMagic()}/hpe/wcd?{{VLANList}}')
if result.ok:
#print(result.text)
responseData = ET.fromstring(result.text)
vlans = responseData.findall('.//VLAN')
for vlan in vlans:
vlanID=vlan.findtext('.//VLANID')
vlanName=vlan.findtext('.//VLANName')
authType=vlan.findtext('.//authorizationType')
vlanType=vlan.findtext('.//VLANType')
print(f'vlan id={vlanID:4} {vlanName:16} auth={authType} vlanType={vlanType}')
def get_ports_info(self):
self.__debug('>> get_ports_info')
result = self._getURL(f'http://{self.hostName}/{self._getMagic()}/hpe/wcd?{{Ports}}')
if result.ok:
#print(result.text)
responseData = ET.fromstring(result.text)
for port in responseData.findall('.//port'):
portObj = {
'poeSupported': port.find('.//POESupported').text,
'ifIndex': port.find('.//ifIndex').text,
'portName': port.find('.//portName').text,
'relUnit': port.find('.//relUnit').text,
'transType': port.find('.//transType').text,
'ifSpeed': port.find('.//ifSpeed').text,
'swIfType': port.find('.//swIfType').text,
'operStatus': port.find('.//operStatus').text,
'adminStatus': port.find('.//adminStatus').text,
'suspStatus': port.find('.//suspStatus').text,
}
print(portObj)
def get_ports_info_alt(self):
self.__debug('>> get_ports_info_alt')
result = self._getURL(f'http://{self.hostName}/{self._getMagic()}/hpe/wcd?{{Standard802_3List}}')
if result.ok:
#print(result.text)
responseData = ET.fromstring(result.text)
standard8023list = responseData.find('.//Standard802_3List')
for entry in standard8023list.findall('.//Entry'):
#print(tostring(entry).decode())
linkState = entry.find('.//linkState').text
if linkState == '1':
linkState = 'Up'
elif linkState == '2':
linkState = 'Down'
mediaType = entry.find('.//mediaType').text
if mediaType == '1':
mediaType = 'Copper'
elif mediaType =='2':
mediaType = 'Optical'
duplex = entry.find('.//duplexOperMode').text
if duplex == '2':
duplex='Full'
elif duplex == '4':
duplex='Unknown'
portType = entry.find('.//interfaceType').text
data = {
# 'id': entry.find('.//interfaceID').text,
'name': entry.find('.//interfaceName').text,
#'type': entry.find('.//interfaceType').text,
'description': entry.find('.//interfaceDescription').text,
'linkState': linkState ,
'speed': entry.find('.//speedOper').text,
'duplex': duplex,
'mediaType': mediaType,
}
if portType == '1':
print(data)
def _bytesToHuman(self,bytes: int):
if bytes < 1024:
bytes = str(bytes) +' B'
elif bytes >= 1024 and bytes < (1024*1024):
bytes = str(int(bytes/1024)) +' KB'
elif bytes >= (1024*1024) and bytes < (1024*1024*1024):
bytes = str(round((bytes/(1024*1024)),2)) +' MB'
elif bytes >= (1024*1024+1024) and bytes < (1024*1024*1024*1024):
bytes = str(round(bytes/(1024*1024*1024),2)) +' GB'
return bytes
def get_ports_stats(self):
self.__debug('>> get_ports_stats')
result = self._getURL(f'http://{self.hostName}/{self._getMagic()}/hpe/wcd?{{StatisticsList}}{{EtherlikeStatisticsList}}') #
if result.ok:
import json
import xmltodict
#print(result.text)
responseData = ET.fromstring(result.text)
StatisticsList= responseData.find('.//StatisticsList')
for entry in StatisticsList.findall('.//InterfaceStatisticsEntry'):
recvBytes = int(entry.find('.//receivePacketByteCount').text)
recvBytes = self._bytesToHuman(recvBytes)
xmitBytes = int(entry.find('.//transmitPacketByteCount').text)
xmitBytes = self._bytesToHuman(xmitBytes)
data = {
'id': entry.find('.//interfaceID').text,
'name': entry.find('.//interfaceName').text,
'type': entry.find('.//interfaceType').text,
'receiveBytes': recvBytes,
'transmitBytes': xmitBytes,
}
print(data)
# data_dict = xmltodict.parse(tostring(entry))
# json_data = json.dumps(data_dict,indent=2)
# print(json_data)
# EtherlikeStatisticsList = responseData.find('.//EtherlikeStatisticsList')
# for entry in EtherlikeStatisticsList.findall('.//InterfaceStatisticsEntry'):
# data_dict = xmltodict.parse(tostring(entry))
# json_data = json.dumps(data_dict,indent=2)
# print(json_data)
# code starts here
print('loading certificate config...')
with open('config.json') as f:
config = json.load(f)
privateKeyFile = config['certificates']['privateKeyFile']
publicKeyFile = config['certificates']['publicKeyFile']
certificateFile = config['certificates']['certificateFile']
switches = config['switches']
for switch in switches:
hostname=switch['hostname']
user=switch['user']
password=switch['password']
try:
aSwitch = ArubaSwitch(hostname,443,user,password)
if aSwitch.authenticate():
aSwitch.uploadSSLCertificate(privateKeyFile,publicKeyFile,certificateFile)
print(f'Uploaded Certificate to https://{hostname}/')
except RuntimeError as e:
print(e)