3
# (C) Copyright 2008 Hewlett-Packard Development Company, L.P.
5
# This program is free software; you can redistribute it and/or modify
6
# it under the terms of the GNU Lesser General Public License as
7
# published by the Free Software Foundation; either version 2 of the
10
# This program is distributed in the hope that it will be useful, but
11
# WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
# Lesser General Public License for more details.
15
# You should have received a copy of the GNU Lesser General Public
16
# License along with this program; if not, write to the Free Software
17
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19
# Author: Tim Potter <tpot@hp.com>
22
# A small utility to wrap up a PyWBEM session in a Python interactive
27
# wbemcli.py HOSTNAME [-u USERNAME -p PASSWORD] [-n namespace] [--no-ssl] \
30
# CIM operations can be executed by using the PyWBEM connection object
31
# called 'cli' in the global scope. There are two sets of aliases
32
# available for usage in the interpreter. For example the following
33
# three commands are equivalent:
35
# >>> cli.EnumerateInstanceNames('SMX_ComputerSystem')
36
# >>> EnumerateInstanceNames('SMX_ComputerSystem')
37
# >>> ein('SMX_ComputerSystem')
39
# Pretty-printing of results is also available using the 'pp'
40
# function. For example:
42
# >>> cs = ei('SMX_ComputerSystem')[0]
44
# [(u'RequestedState', 12L),
45
# (u'Dedicated', [1L]),
46
# (u'StatusDescriptions', [u'System is Functional']),
47
# (u'IdentifyingNumber', u'6F880AA1-F4F5-11D5-8C45-C0116FBAE02A'),
51
import os, stat, sys, string, getpass, errno
53
from code import InteractiveConsole
54
from optparse import OptionParser
55
from pprint import pprint as pp
57
# Conditional support of readline module
64
except ImportError, arg:
68
# Parse command line args
71
optparser = OptionParser(
72
usage = '%prog HOSTNAME [-u USER -p PASS] [-n NAMESPACE] [--no-ssl]')
74
# Username and password
76
optparser.add_option('-u', '--user', dest = 'user',
77
action = 'store', type = 'string',
78
help = 'user to connect as')
80
optparser.add_option('-p', '--password', dest = 'password',
81
action = 'store', type = 'string',
82
help = 'password to connect user as')
84
# Change the default namespace used
86
optparser.add_option('-n', '--namespace', dest = 'namespace',
87
action = 'store', type = 'string', default = 'root/cimv2',
88
help = 'default namespace to use')
90
# Don't use SSL for remote connections
92
optparser.add_option('--no-ssl', dest = 'no_ssl', action = 'store_true',
93
help = 'don\'t use SSL')
95
# Specify non-standard port
97
optparser.add_option('--port', dest = 'port', action = 'store', type = 'int',
98
help = 'port to connect as', default = None)
102
(opts, argv) = optparser.parse_args()
105
optparser.print_usage()
109
# Set up a client connection
112
def remote_connection():
113
"""Initiate a remote connection, via PyWBEM."""
115
if argv[0][0] == '/':
123
url = '%s://%s' % (proto, argv[0])
125
if opts.port is not None:
126
url += ':%d' % opts.port
130
if opts.user is not None and opts.password is None:
131
opts.password = getpass.getpass('Enter password for %s: ' % opts.user)
133
if opts.user is not None or opts.password is not None:
134
creds = (opts.user, opts.password)
136
cli = WBEMConnection(url, creds, default_namespace = opts.namespace)
142
cli = remote_connection()
145
# Create some convenient global functions to reduce typing
148
def EnumerateInstanceNames(classname, namespace = None):
149
"""Enumerate the names of the instances of a CIM Class (including the
150
names of any subclasses) in the target namespace."""
152
return cli.EnumerateInstanceNames(classname, namespace = namespace)
154
def EnumerateInstances(classname, namespace = None, LocalOnly = True,
155
DeepInheritance = True, IncludeQualifiers = False,
156
IncludeClassOrigin = False):
157
"""Enumerate instances of a CIM Class (includeing the instances of
158
any subclasses in the target namespace."""
160
return cli.EnumerateInstances(classname,
161
namespace = namespace,
162
DeepInheritance = DeepInheritance,
163
IncludeQualifiers = IncludeQualifiers,
164
IncludeClassOrigin = IncludeClassOrigin)
167
def GetInstance(instancename, LocalOnly = True, IncludeQualifiers = False,
168
IncludeClassOrigin = False):
169
"""Return a single CIM instance corresponding to the instance name
172
return cli.GetInstance(instancename,
173
LocalOnly = LocalOnly,
174
IncludeQualifiers = IncludeQualifiers,
175
IncludeClassOrigin = IncludeClassOrigin)
177
def DeleteInstance(instancename):
178
"""Delete a single CIM instance."""
180
return cli.DeleteInstance(instancename)
182
def ModifyInstance(*args, **kwargs):
183
return cli.ModifyInstance(*args, **kwargs)
185
def CreateInstance(*args, **kwargs):
186
return cli.CreateInstance(*args, **kwargs)
188
def InvokeMethod(*args, **kwargs):
189
return cli.InvokeMethod(*args, **kwargs)
191
def AssociatorNames(*args, **kwargs):
192
return cli.AssociatorNames(*args, **kwargs)
194
def Associators(*args, **kwargs):
195
return cli.Associators(*args, **kwargs)
197
def ReferenceNames(*args, **kwargs):
198
return cli.ReferenceNames(*args, **kwargs)
200
def References(*args, **kwargs):
201
return cli.References(*args, **kwargs)
203
def EnumerateClassNames(*args, **kwargs):
204
return cli.EnumerateClassNames(*args, **kwargs)
206
def EnumerateClasses(*args, **kwargs):
207
return cli.EnumerateClasses(*args, **kwargs)
209
def GetClass(*args, **kwargs):
210
return cli.GetClass(*args, **kwargs)
212
def DeleteClass(*args, **kwargs):
213
return cli.DeleteClass(*args, **kwargs)
215
def ModifyClass(*args, **kwargs):
216
return cli.ModifyClass(*args, **kwargs)
218
def CreateClass(*args, **kwargs):
219
return cli.CreateClass(*args, **kwargs)
221
def EnumerateQualifiers(*args, **kwargs):
222
return cli.EnumerateQualifiers(*args, **kwargs)
224
def GetQualifier(*args, **kwargs):
225
return cli.GetQualifier(*args, **kwargs)
227
def SetQualifier(*args, **kwargs):
228
return cli.SetQualifier(*args, **kwargs)
230
def DeleteQualifier(*args, **kwargs):
231
return cli.DeleteQualifier(*args, **kwargs)
233
# Aliases for global functions above
235
ein = EnumerateInstanceNames
236
ei = EnumerateInstances
249
ecn = EnumerateClassNames
250
ec = EnumerateClasses
256
eq = EnumerateQualifiers
262
# Enter interactive console
269
# Note how we are connected
271
result += 'Connected to %s' % cli.url
272
if cli.creds is not None:
273
result += ' as %s' % cli.creds[0]
277
# Read previous command line history
279
histfile = '%s/.wbemcli_history' % os.environ['HOME']
283
readline.read_history_file(histfile)
285
if arg[0] != errno.ENOENT:
290
i = InteractiveConsole(globals())
291
i.interact(get_banner())
293
# Save command line history
296
readline.write_history_file(histfile)