mezantrop

hcs_client-xml.py

Nov 30th, 2016
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.47 KB | None | 0 0
  1. #!/usr/bin/python3
  2.  
  3. # -----------------------------------------------------------------------------
  4. # "THE BEER-WARE LICENSE" (Revision 42):
  5. # zmey20000@yahoo.com wrote this file. As long as you retain this notice you
  6. # can do whatever you want with this stuff. If we meet some day, and you think
  7. # this stuff is worth it, you can buy me a beer in return Mikhail Zakharov
  8. # -----------------------------------------------------------------------------
  9.  
  10. # Hitachi Command Suite XML API usage example in Pyton.
  11.  
  12. import base64
  13. import http.client
  14. import xml.etree.ElementTree as ET
  15.  
  16. # ----------------------------------------------------------------------------
  17. hcs_server = 'hcs8.server.name'
  18. login = 'system'
  19. password = 'manager'
  20. command = 'StorageArray'
  21. option = 'all'
  22.  
  23. # ----------------------------------------------------------------------------
  24. b64lp = base64.b64encode((login + ':' + password).encode('ascii'))
  25.  
  26. body = """<?xml version="1.0" encoding="UTF-8"?>
  27. <HiCommandServerMessage>
  28.    <APIInfo version="7.6" />
  29.    <Request>
  30.       <StorageManager>
  31.            <Get target=""" + '"' + command + '" ' + """option=""" + '"' + option + '" ' + """>
  32.                <StorageArray />
  33.            </Get>
  34.        </StorageManager>
  35.    </Request>
  36. </HiCommandServerMessage>"""
  37.  
  38. headers = {'Content-Type': 'text/xml',
  39.            'User-Agent': 'Deck Eight:One Step:0',
  40.            'Authorization': 'Basic ' + b64lp.decode('utf-8')}
  41.  
  42. httpc = http.client.HTTPConnection(hcs_server, 2001, timeout=30)
  43. httpc.request('POST', '/service/ServerAdmin', body, headers)
  44. resp = httpc.getresponse()
  45. #print(resp.status, resp.reason)
  46.  
  47. data = resp.read()
  48. data = data.decode('utf-8')
  49.  
  50. root = ET.fromstring(data)
  51. for array in root.iter('StorageArray'):
  52.     name = array.get('name')
  53.     hardwareRevision = array.get('hardwareRevision')
  54.     cacheInMB = array.get('cacheInMB')
  55.     totalFreeSpaceInKB = array.get('totalFreeSpaceInKB')
  56.     allocatedCapacityInKB = array.get('allocatedCapacityInKB')
  57.     capacityInKB = array.get('capacityInKB')
  58.     numberOfControllers = array.get('numberOfControllers')
  59.     controllerVersion = array.get('controllerVersion')
  60.     productName = array.get('productName')
  61.     arrayType = array.get('arrayType')
  62.     serialNumber = array.get('serialNumber')
  63.  
  64.     print(name, serialNumber, arrayType, productName, totalFreeSpaceInKB, allocatedCapacityInKB, capacityInKB, controllerVersion, numberOfControllers, cacheInMB, hardwareRevision)
  65.  
  66. httpc.close()
Add Comment
Please, Sign In to add comment