Skip to content

Instantly share code, notes, and snippets.

@hartsock
Created March 11, 2014 22:27
Show Gist options
  • Save hartsock/9496429 to your computer and use it in GitHub Desktop.
Save hartsock/9496429 to your computer and use it in GitHub Desktop.
Here's a quick and dirty script that proves you can share a session between pyVmomi and SUDS clients in either direction. Here's how.
#!/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
print "Python suds..."
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)
def get_current_session(client):
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')
current_session = get_current_session(client)
if current_session:
print "current session id: %s" % current_session.key
cookies = client.options.transport.cookiejar
for cookie in cookies:
print "cookie '%s' contents: %s" % (cookie.name, cookie.value)
else:
print "not logged in"
# now to move the current session ID over to pyVmomi
print "now pyVmomi ... "
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 extract_vmware_cookie_suds(client):
cookiejar = client.options.transport.cookiejar
for cookie in cookiejar:
if cookie.name == VMWARE_COOKIE_NAME:
return '%s=%s' % (cookie.name, cookie.value)
client.__class__.extract_vmware_cookie = extract_vmware_cookie_suds
def inject_vmware_cookie_suds(client, cookie_value, domain):
for cookie in client.options.transport.cookiejar:
if cookie.name == VMWARE_COOKIE_NAME:
cookie.value = cookie_value
return
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 "suds session to pyvmomi "
si._stub.cookie = client.extract_vmware_cookie()
print "current suds session id: %s" % get_current_session(client).key
print "current pyVmomi session id: %s" % si.content.sessionManager.currentSession.key
print "=" * 80
print "pyvmomi to suds"
si._stub.cookie = pyvmomi_cookie
cookie_value = pyvmomi_cookie[pyvmomi_cookie.index("=")+1:pyvmomi_cookie.index(";")]
print "current pyVmomi session id: %s" % si.content.sessionManager.currentSession.key
client.set_vmware_cookie(cookie_value, args.host)
print "current suds session id: %s" % get_current_session(client).key
@hartsock
Copy link
Author

Note: This work needs to be cleaned up for use in a library.

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