Skip to content

Instantly share code, notes, and snippets.

@hartsock
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hartsock/9549073 to your computer and use it in GitHub Desktop.
Save hartsock/9549073 to your computer and use it in GitHub Desktop.
Login with pyVmomi (any technique) and use it with SUDS
#!/usr/bin/python
import argparse
import cookielib
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')
parser.add_argument('-o', '--port',
required=False,
action='store',
help="port to use, default 443", default=443)
args = parser.parse_args()
url = "https://%s/sdk/vimService.wsdl" % args.host
client = suds.client.Client(url, location=url)
def get_current_session(client):
si = suds.sudsobject.Property("ServiceInstance")
si._type = "ServiceInstance"
sc = client.service.RetrieveServiceContent(si)
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:
if not hasattr(obj, 'propSet'):
return None
for prop in obj.propSet:
if prop.name == name:
return prop.val
results.__class__.get_property = get_property
return results.get_property('currentSession')
print "pyVmomi login... "
import pyVim.connect as connect
si = connect.SmartConnect(host=args.host,
user=args.user,
pwd=args.password,
port=int(args.port))
print "current session id: %s" % si.content.sessionManager.currentSession.key
pyvmomi_cookie = si._stub.cookie
print "current cookie contents: %s" % pyvmomi_cookie
VMWARE_COOKIE_NAME = 'vmware_soap_session'
def inject_vmware_cookie_suds(client, cookie_value, domain):
cookie = cookielib.Cookie(0,
VMWARE_COOKIE_NAME,
cookie_value,
None,
None,
domain,
None,
None,
"/",
None,
None,
None,
None,
None,
None,
None,
None,)
client.options.transport.cookiejar.set_cookie(cookie)
client.__class__.set_vmware_cookie = inject_vmware_cookie_suds
print "=" * 80
print "pyvmomi to suds"
si._stub.cookie = pyvmomi_cookie
cookie_value = pyvmomi_cookie[
pyvmomi_cookie.index("=") + 1:pyvmomi_cookie.index(";")]
session_id = si.content.sessionManager.currentSession.key
print "current pyVmomi session id: %s" % session_id
client.set_vmware_cookie(cookie_value, args.host)
soap_session_id = get_current_session(client).key
print "current suds session id: %s" % soap_session_id
assert session_id == soap_session_id
@hartsock
Copy link
Author

Note: this will need clean up before I include it in a library. I plan on shipping a pypy lib with this inside it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment