-
Notifications
You must be signed in to change notification settings - Fork 26
/
pycimlistener.py
executable file
·181 lines (154 loc) · 5.93 KB
/
pycimlistener.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
#!/usr/bin/python
#
# Simple indication receiver using Twisted Python. HTTP post requests
# are listened for on port 5988 and port 5899 using SSL.
#
# Requires Twisted Python and
#
import sys
import optparse
import pywbem
from twisted.internet import reactor
from twisted.web import server, resource
global conn
conn=None
class WBEMConn:
_shared_state = {}
conn = None
def __init__(self, options=None):
# Borgness
self.__dict__ = WBEMConn._shared_state
self.conn = pywbem.SFCBUDSConnection()
'''
if options:
proto = 'http'
if options.secure:
proto = 'https'
url = '%s://%s' % (proto, options.host)
self.conn = pywbem.WBEMConnection(
url,
(options.user, options.password),
default_namespace = options.namespace)
'''
global conn
conn = self.conn
class CIMOM(resource.Resource):
isLeaf = 1
def render_POST(self, request):
for line in request.content.readlines():
print(line)
return ''
from OpenSSL import SSL
class ServerContextFactory:
def getContext(self):
"""Create an SSL context with a dodgy certificate."""
ctx = SSL.Context(SSL.SSLv23_METHOD)
ctx.use_certificate_file('server.pem')
ctx.use_privatekey_file('server.pem')
return ctx
from twisted.internet import ssl, reactor
from twisted.python import log
from socket import getfqdn
import time
def _createFilter(query,
ns,
querylang='WQL',
src_ns='root/cimv2',
in_name=None):
name = in_name or 'cimfilter%s'%time.time()
filterinst=pywbem.CIMInstance('CIM_IndicationFilter')
filterinst['CreationClassName']='CIM_IndicationFilter'
filterinst['SystemCreationClassName']='CIM_ComputerSystem'
filterinst['SystemName']=getfqdn()
filterinst['Name']=name
filterinst['Query']=query
filterinst['QueryLanguage']=querylang
filterinst['SourceNamespace']=src_ns
cop = pywbem.CIMInstanceName('CIM_IndicationFilter')
cop.keybindings = { 'CreationClassName':'CIM_IndicationFilter',
'SystemClassName':'CIM_ComputerSystem',
'SystemName':getfqdn(),
'Name':name }
cop.namespace=ns
filterinst.path = cop
filtercop = conn.CreateInstance(filterinst)
return filtercop
def _createDest(destination,
ns,
in_name=None):
name = in_name or 'cimlistener%s'%time.time()
destinst=pywbem.CIMInstance('CIM_ListenerDestinationCIMXML')
destinst['CreationClassName']='CIM_ListenerDestinationCIMXML'
destinst['SystemCreationClassName']='CIM_ComputerSystem'
destinst['SystemName']=getfqdn()
print("destname=%s" % name)
destinst['Name']=name
destinst['Destination']=destination
cop = pywbem.CIMInstanceName('CIM_ListenerDestinationCIMXML')
cop.keybindings = { 'CreationClassName':'CIM_ListenerDestinationCIMXML',
'SystemClassName':'CIM_ComputerSystem',
'SystemName':getfqdn(),
'Name':name }
cop.namespace=ns
destinst.path = cop
destcop = conn.CreateInstance(destinst)
return destcop
def _createSubscription(ns,
handler,
indfilter):
subinst=pywbem.CIMInstance('CIM_IndicationSubscription')
subinst['Filter']=indfilter
subinst['Handler']=indhandler
cop = pywbem.CIMInstanceName('CIM_IndicationSubscription')
cop.keybindings = { 'Filter':indfilter,
'Handler':indhandler }
cop.namespace=ns
subinst.path = cop
subcop = conn.CreateInstance(subinst)
return subcop
if __name__ == '__main__':
global conn
parser = optparse.OptionParser()
parser.add_option('--level',
'-l',
action='store',
type='int',
dest='dbglevel',
help='Indicate the level of debugging statements to display (default=2)',
default=2)
parser.add_option('-s', '--UDS', help="Use the SFCBUDSConnection to the cimom", default=False )
parser.add_option('-u', '--url', default='https://localhost',
help='Specify the url of the CIMOM (default=https://localhost)')
parser.add_option('-n', '--namespace', default='root/interop',
help='Specify the namespace the test runs against (default=root/interop)')
parser.add_option('', '--user', default='pegasus',
help='Specify the user name used when connection to the CIMOM (default=pegasus)')
parser.add_option('', '--password', default='',
help='Specify the password for the user (default=<empty>)')
parser.add_option('--verbose', '', action='store_true', default=False,
help='Show verbose output')
parser.add_option('-q', '--query', help='Query string for Filter')
parser.add_option('-g', '--qlang', help='Query Language (default=WQL)', default="WQL")
parser.add_option('-d', '--dest', help='Destination for the CIM_ListenerDestination')
parser.add_option('-p', '--provider', help='Name of provider to setup listener for')
options, arguments = parser.parse_args()
conn = WBEMConn().conn
indhandler=None
indfilter=None
indsub=None
try:
indhandler = _createDest(options.dest, options.namespace)
indfilter = _createFilter(options.query, options.namespace, querylang=options.qlang)
indsub = _createSubscription(options.namespace, indhandler, indfilter)
log.startLogging(sys.stdout)
site = server.Site(CIMOM())
reactor.listenTCP(5998, site)
reactor.listenSSL(5999, site, ServerContextFactory())
reactor.run()
finally:
if indsub:
conn.DeleteInstance(indsub)
if indfilter:
conn.DeleteInstance(indfilter)
if indhandler:
conn.DeleteInstance(indhandler)