#!/usr/bin/python import sys import pywbem def usage(): print """Usage: %s
[username] [password] Connect to CIM server at address and issue a command on the machine. Available commands and their parameters: list_user - list users, parameter is user name or * for all list_group - list groups, parameter is group name or * for all group_members - list members of group, parameter is group name Example: %s https://127.0.0.1:5989 list_user * root redhat""" % (sys.argv[0], sys.argv[0]) if len(sys.argv) < 6: usage() sys.exit(1) url = sys.argv[1] command = sys.argv[2] parameter = sys.argv[3] username = None password = None if len(sys.argv) > 4: username = sys.argv[4] if len(sys.argv) > 5: password = sys.argv[5] cliconn = pywbem.WBEMConnection(url, (username, password)) if command == "list_user": # Listintg users is simple, just query all instances from Cura_Account # or select only by given Name if parameter == "*": instances = cliconn.ExecQuery('WQL', 'select * from Cura_Account') else: instances = cliconn.ExecQuery('WQL', 'select * from Cura_Account where Name = "%s"' % parameter) for instance in instances: print instance.tomof() elif command == "list_group": # Listintg gruops is simple, just query all instances from Cura_Group # or select only by given Name if parameter == "*": instances = cliconn.ExecQuery('WQL', 'select * from Cura_Group') else: instances = cliconn.ExecQuery('WQL', 'select * from Cura_Group where Name = "%s"' % parameter) for instance in instances: print instance.tomof() elif command == "group_members": # Group members is a bit tricky. You need to select group (Cura_Group) # by given Name, then you need to select identities (Cura_Identity), which # are connected through Cura_MemberOfGroup to Cura_Group. # And finally select all accounts (Cura_Account) which are connected through # Cura_AssignedAccountIdentity with selected identities (this should be # 1 to 1) groups = cliconn.ExecQuery('WQL', 'select * from Cura_Group where Name = "%s"' % parameter) for group in groups: identities = cliconn.Associators(group.path, AssocClass='Cura_MemberOfGroup') for identity in identities: accounts = cliconn.Associators(identity.path, AssocClass='Cura_AssignedAccountIdentity') for account in accounts: print account.tomof() else: # unknown command print "Unknown command", command usage() sys.exit(1)