Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python3
- #-*- coding:utf-8 -*-
- # Author: dele
- import socket
- import json
- from ipmi_management.ipmi_util import ipmi_handler
- HOST = '3.224.24.9'
- PORT = 65432
- def run_server():
- with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
- s.bind((HOST, PORT))
- s.listen()
- while True:
- conn, addr = s.accept()
- with conn:
- print('Connected by', addr)
- while True:
- try:
- b_data = conn.recv(1024)
- if not b_data:
- break
- data = json.loads(b_data)
- msg = ipmi_handler(data)
- conn.sendall(json.dumps(msg).encode('utf-8'))
- except Exception as e:
- conn.sendall(b'exception')
- import os
- from subprocess import PIPE, run
- import json
- STATUS = 'status'
- ON = 'on'
- OFF = 'off'
- RESET = 'reset'
- def out(command):
- result = run(command, stdout=PIPE, stderr=PIPE, universal_newlines=True, shell=True)
- return result.stdout
- def ipmi_status(ipmi_addr, bmc_uname='ADMIN', bmc_pwd='ADMIN'):
- if not bmc_uname: bmc_uname = 'ADMIN'
- if not bmc_pwd: bmc_pwd = 'ADMIN'
- try:
- command = 'ipmitool -H ' + ipmi_addr + ' -I lanplus -U ' + bmc_uname + ' -P ' + bmc_pwd + ' power ' + STATUS
- message = out(command)
- return message
- except Exception as e:
- raise e
- def ipmi_on(ipmi_addr, bmc_uname='ADMIN', bmc_pwd='ADMIN'):
- if not bmc_uname: bmc_uname = 'ADMIN'
- if not bmc_pwd: bmc_pwd = 'ADMIN'
- try:
- message = out('ipmitool -H ' + ipmi_addr + ' -I lanplus -U ' + bmc_uname + ' -P ' + bmc_pwd + ' power ' + ON)
- return message
- except Exception as e:
- raise e
- def ipmi_off(ipmi_addr, bmc_uname='ADMIN', bmc_pwd='ADMIN'):
- if not bmc_uname: bmc_uname = 'ADMIN'
- if not bmc_pwd: bmc_pwd = 'ADMIN'
- try:
- message = out('ipmitool -H ' + ipmi_addr + ' -I lanplus -U ' + bmc_uname + ' -P ' + bmc_pwd + ' power ' + OFF)
- return message
- except Exception as e:
- raise e
- def ipmi_reset(ipmi_addr, bmc_uname='ADMIN', bmc_pwd='ADMIN'):
- if not bmc_uname: bmc_uname = 'ADMIN'
- if not bmc_pwd: bmc_pwd = 'ADMIN'
- try:
- message = out('ipmitool -H ' + ipmi_addr + ' -I lanplus -U ' + bmc_uname + ' -P ' + bmc_pwd + ' power ' + RESET)
- return message
- except Exception as e:
- raise e
- def ipmi_handler(data):
- action = data.get('action')
- if action == STATUS:
- try:
- ipmi_data = data.get('data')
- msg = ipmi_status(ipmi_data.get('ipmi_addr'), ipmi_data.get('bmc_uname'), ipmi_data.get('bmc_pwd'))
- return msg
- except Exception as e:
- raise e
- if action == ON:
- try:
- ipmi_data = data.get('data')
- msg = ipmi_on(ipmi_data.get('ipmi_addr'), ipmi_data.get('bmc_uname'), ipmi_data.get('bmc_pwd'))
- return msg
- except Exception as e:
- raise e
- if action == OFF:
- try:
- ipmi_data = data.get('data')
- msg = ipmi_off(ipmi_data.get('ipmi_addr'), ipmi_data.get('bmc_uname'), ipmi_data.get('bmc_pwd'))
- return msg
- except Exception as e:
- raise e
- if action == RESET:
- try:
- ipmi_data = data.get('data')
- msg = ipmi_reset(ipmi_data.get('ipmi_addr'), ipmi_data.get('bmc_uname'), ipmi_data.get('bmc_pwd'))
- return msg
- except Exception as e:
- raise e
- python3 -m ipmi_management &
- [dele@localhost qiyun_ipmi_management]$ Traceback (most recent call last):
- File "/data/ldl/repo/qiyun_ipmi_management/ipmi_management/ipmi_server.py", line 35, in run_server
- b_data = conn.recv(1024)
- TimeoutError: [Errno 110] Connection timed out
- During handling of the above exception, another exception occurred:
- Traceback (most recent call last):
- File "/opt/Python/lib/python3.7/runpy.py", line 193, in _run_module_as_main
- "__main__", mod_spec)
- File "/opt/Python/lib/python3.7/runpy.py", line 85, in _run_code
- exec(code, run_globals)
- File "/data/ldl/repo/qiyun_ipmi_management/ipmi_management/__main__.py", line 11, in <module>
- main()
- File "/data/ldl/repo/qiyun_ipmi_management/ipmi_management/__main__.py", line 8, in main
- run_server()
- File "/data/ldl/repo/qiyun_ipmi_management/ipmi_management/ipmi_server.py", line 47, in run_server
- conn.sendall(b'exception')
- BrokenPipeError: [Errno 32] Broken pipe
- [1]+ quit 1 python3 -m ipmi_management
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement