-
Notifications
You must be signed in to change notification settings - Fork 0
/
ipmi_cmdexec.py
109 lines (91 loc) · 3.04 KB
/
ipmi_cmdexec.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
#!/usr/bin/env python
import sys
import subprocess
import dbus
import string
import os
import fcntl
import glib
import gobject
import dbus.service
import dbus.mainloop.glib
# import dbus.maincontext.glib
DBUS_NAME = 'org.openbmc.HostIpmi'
OBJ_NAME = '/org/openbmc/HostIpmi/1'
def header(seq, netfn, lun, cmd):
return (
'seq: 0x%02x\nnetfn: 0x%02x\n\nlun: 0x%02d\ncmd: 0x%02x\n') % (
seq, netfn, lun, cmd)
def print_request(seq, netfn, lun, cmd, data):
str = header(seq, netfn, lun, cmd)
str += 'data: [%s]' % ', '.join(['0x%02x' % x for x in data])
# print str
def print_response(seq, netfn, lun, cmd, cc, data):
str = header(seq, netfn, lun, cmd)
str += 'cc: 0x%02x\ndata: [%s]' % (
cc, ', '.join(['0x%02x' % x for x in data])
)
# print str
class IpmiDebug(dbus.service.Object):
def __init__(self,bus,name):
dbus.service.Object.__init__(self,bus,name)
@dbus.service.signal(DBUS_NAME, "yyyyay")
def ReceivedMessage(self, seq, netfn, lun, cmd, data):
print "IPMI packet from host:"
print_request(seq, netfn, lun, cmd, data)
# return 0
# sys.exit()
@dbus.service.method(DBUS_NAME, "yyyyyay", "x")
def sendMessage(self, seq, netfn, lun, cmd, ccode, data):
print "IPMI packet sent to host:"
print_response(seq, netfn, lun, cmd, ccode, data)
return 0
@dbus.service.method(DBUS_NAME)
# def setAttention(self):
# print "IPMI SMS_ATN set"
# def unmask_signals(self):
def unmask_signals(self):
print "Unmask Signal"
sys.exit()
# return
class ConsoleReader(object):
def __init__(self, ipmi_obj):
self.buffer = ''
self.seq = 0
self.ipmi_obj = ipmi_obj
flags = fcntl.fcntl(sys.stdin.fileno(), fcntl.F_GETFL)
flags |= os.O_NONBLOCK
fcntl.fcntl(sys.stdin.fileno(), fcntl.F_SETFL, flags)
glib.io_add_watch(sys.stdin, glib.IO_IN, self.io_callback)
def io_callback(self, fd, condition):
chunk = fd.read()
for char in chunk:
self.buffer += char
if char == '\n':
self.line(self.buffer)
self.buffer = ''
return False
def line(self, data):
s = data.split(' ')
if len(s) < 2:
print "Not enough bytes to form a valid IPMI packet"
return
try:
data = [int(c, 16) for c in s]
except ValueError:
return
self.seq += 1
self.ipmi_obj.ReceivedMessage(self.seq, data[0], 0, data[1], data[2:])
def main():
dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
bus = dbus.SystemBus()
name = dbus.service.BusName(DBUS_NAME, bus)
obj = IpmiDebug(bus, OBJ_NAME)
mainloop = gobject.MainLoop()
# mainloop = gobject.MainContext()
r = ConsoleReader(obj)
# print ("Enter IPMI packet as hex values. First three bytes will be used"
# "as netfn and cmd.\nlun will be zero.")
mainloop.run()
if __name__ == '__main__':
sys.exit(main())