Skip to content

Instantly share code, notes, and snippets.

@hartsock
Last active August 29, 2015 13:56
Show Gist options
  • Save hartsock/9339133 to your computer and use it in GitHub Desktop.
Save hartsock/9339133 to your computer and use it in GitHub Desktop.
A quick and dirty python SUDS script that shows how to get the current session ID using the vSphere API
#!/usr/bin/python
import argparse
import suds
parser = argparse.ArgumentParser()
parser.add_argument('-s', '--host', required=True, action='store', help='Remote host to connect to')
parser.add_argument('-u', '--user', required=True, action='store', help='User name to use when connecting to host')
parser.add_argument('-p', '--password', required=True, action='store', help='Password to use when connecting to host')
args = parser.parse_args()
url = "https://%s/sdk/vimService.wsdl" % args.host
client = suds.client.Client(url,location=url)
si = suds.sudsobject.Property("ServiceInstance")
si._type = "ServiceInstance"
sc = client.service.RetrieveServiceContent(si)
client.service.Login(sc.sessionManager, userName=args.user, password=args.password)
property_filter_spec = client.factory.create('ns0:PropertyFilterSpec')
property_spec = client.factory.create('ns0:PropertySpec')
property_spec.pathSet = ['currentSession']
#property_spec.all = True
property_spec.type = "SessionManager"
property_filter_spec.propSet = [property_spec]
object_spec = client.factory.create('ns0:ObjectSpec')
object_spec.obj = sc.sessionManager
object_spec.skip = False
property_filter_spec.objectSet = [object_spec]
options = client.factory.create('ns0:RetrieveOptions')
options.maxObjects = 1
results = client.service.RetrievePropertiesEx(sc.propertyCollector, specSet=[property_filter_spec], options=options)
def get_property(self, name):
for obj in self.objects:
for prop in obj.propSet:
if prop.name == name:
return prop.val
results.__class__.get_property = get_property
current_session = results.get_property('currentSession')
if current_session:
print "current session id: %s" % current_session.key
else:
print "not logged in"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment