Advertisement
Guest User

Untitled

a guest
Jun 25th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.57 KB | None | 0 0
  1. #!/usr/bin/python3
  2. #-*- coding:utf-8 -*-
  3. # Author: dele
  4.  
  5.  
  6. import socket
  7. import json
  8. from ipmi_management.ipmi_util import ipmi_handler
  9.  
  10.  
  11. HOST = '3.224.24.9'
  12. PORT = 65432
  13.  
  14. def run_server():
  15. with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
  16. s.bind((HOST, PORT))
  17. s.listen()
  18. while True:
  19. conn, addr = s.accept()
  20. with conn:
  21. print('Connected by', addr)
  22.  
  23. while True:
  24. try:
  25. b_data = conn.recv(1024)
  26.  
  27. if not b_data:
  28. break
  29.  
  30. data = json.loads(b_data)
  31.  
  32. msg = ipmi_handler(data)
  33. conn.sendall(json.dumps(msg).encode('utf-8'))
  34.  
  35. except Exception as e:
  36.  
  37. conn.sendall(b'exception')
  38.  
  39. import os
  40. from subprocess import PIPE, run
  41. import json
  42.  
  43. STATUS = 'status'
  44. ON = 'on'
  45. OFF = 'off'
  46. RESET = 'reset'
  47.  
  48. def out(command):
  49. result = run(command, stdout=PIPE, stderr=PIPE, universal_newlines=True, shell=True)
  50. return result.stdout
  51.  
  52.  
  53. def ipmi_status(ipmi_addr, bmc_uname='ADMIN', bmc_pwd='ADMIN'):
  54.  
  55. if not bmc_uname: bmc_uname = 'ADMIN'
  56. if not bmc_pwd: bmc_pwd = 'ADMIN'
  57.  
  58. try:
  59.  
  60. command = 'ipmitool -H ' + ipmi_addr + ' -I lanplus -U ' + bmc_uname + ' -P ' + bmc_pwd + ' power ' + STATUS
  61. message = out(command)
  62.  
  63. return message
  64. except Exception as e:
  65.  
  66. raise e
  67.  
  68.  
  69. def ipmi_on(ipmi_addr, bmc_uname='ADMIN', bmc_pwd='ADMIN'):
  70.  
  71. if not bmc_uname: bmc_uname = 'ADMIN'
  72. if not bmc_pwd: bmc_pwd = 'ADMIN'
  73.  
  74. try:
  75. message = out('ipmitool -H ' + ipmi_addr + ' -I lanplus -U ' + bmc_uname + ' -P ' + bmc_pwd + ' power ' + ON)
  76. return message
  77. except Exception as e:
  78. raise e
  79.  
  80. def ipmi_off(ipmi_addr, bmc_uname='ADMIN', bmc_pwd='ADMIN'):
  81.  
  82. if not bmc_uname: bmc_uname = 'ADMIN'
  83. if not bmc_pwd: bmc_pwd = 'ADMIN'
  84.  
  85. try:
  86. message = out('ipmitool -H ' + ipmi_addr + ' -I lanplus -U ' + bmc_uname + ' -P ' + bmc_pwd + ' power ' + OFF)
  87. return message
  88. except Exception as e:
  89. raise e
  90.  
  91. def ipmi_reset(ipmi_addr, bmc_uname='ADMIN', bmc_pwd='ADMIN'):
  92.  
  93. if not bmc_uname: bmc_uname = 'ADMIN'
  94. if not bmc_pwd: bmc_pwd = 'ADMIN'
  95.  
  96. try:
  97. message = out('ipmitool -H ' + ipmi_addr + ' -I lanplus -U ' + bmc_uname + ' -P ' + bmc_pwd + ' power ' + RESET)
  98. return message
  99. except Exception as e:
  100. raise e
  101.  
  102.  
  103.  
  104. def ipmi_handler(data):
  105.  
  106. action = data.get('action')
  107. if action == STATUS:
  108. try:
  109. ipmi_data = data.get('data')
  110. msg = ipmi_status(ipmi_data.get('ipmi_addr'), ipmi_data.get('bmc_uname'), ipmi_data.get('bmc_pwd'))
  111. return msg
  112. except Exception as e:
  113. raise e
  114.  
  115. if action == ON:
  116. try:
  117. ipmi_data = data.get('data')
  118. msg = ipmi_on(ipmi_data.get('ipmi_addr'), ipmi_data.get('bmc_uname'), ipmi_data.get('bmc_pwd'))
  119. return msg
  120. except Exception as e:
  121. raise e
  122.  
  123. if action == OFF:
  124. try:
  125. ipmi_data = data.get('data')
  126. msg = ipmi_off(ipmi_data.get('ipmi_addr'), ipmi_data.get('bmc_uname'), ipmi_data.get('bmc_pwd'))
  127. return msg
  128. except Exception as e:
  129. raise e
  130.  
  131. if action == RESET:
  132. try:
  133. ipmi_data = data.get('data')
  134. msg = ipmi_reset(ipmi_data.get('ipmi_addr'), ipmi_data.get('bmc_uname'), ipmi_data.get('bmc_pwd'))
  135. return msg
  136. except Exception as e:
  137. raise e
  138.  
  139. python3 -m ipmi_management &
  140.  
  141. [dele@localhost qiyun_ipmi_management]$ Traceback (most recent call last):
  142. File "/data/ldl/repo/qiyun_ipmi_management/ipmi_management/ipmi_server.py", line 35, in run_server
  143. b_data = conn.recv(1024)
  144. TimeoutError: [Errno 110] Connection timed out
  145.  
  146. During handling of the above exception, another exception occurred:
  147.  
  148. Traceback (most recent call last):
  149. File "/opt/Python/lib/python3.7/runpy.py", line 193, in _run_module_as_main
  150. "__main__", mod_spec)
  151. File "/opt/Python/lib/python3.7/runpy.py", line 85, in _run_code
  152. exec(code, run_globals)
  153. File "/data/ldl/repo/qiyun_ipmi_management/ipmi_management/__main__.py", line 11, in <module>
  154. main()
  155. File "/data/ldl/repo/qiyun_ipmi_management/ipmi_management/__main__.py", line 8, in main
  156. run_server()
  157. File "/data/ldl/repo/qiyun_ipmi_management/ipmi_management/ipmi_server.py", line 47, in run_server
  158. conn.sendall(b'exception')
  159. BrokenPipeError: [Errno 32] Broken pipe
  160.  
  161. [1]+ quit 1 python3 -m ipmi_management
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement