Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- import argparse
- import getpass
- import pyVim.connect as connect
- from datetime import datetime
- from datetime import timedelta
- # The current session should show None as the script completes
- # Example output:
- # > Logged in to vcsa
- # > current pyVmomi session id: 525471f5-eb36-67bc-260f-216cd36df9c7
- # > Listing then terminating all sessions older than 1 day:
- # > Username: VSPHERE.LOCAL\vpxd-extension-47a4f1e1-68aa-11e6-8a31-00505603244a; Last Active: 2016-09-12 20:43:23.982896+00:00
- # > Username: VSPHERE.LOCAL\vpxd-extension-47a4f1e1-68aa-11e6-8a31-00505603244a; Last Active: 2016-09-12 20:37:44.098979+00:00
- # > Username: VSPHERE.LOCAL\vpxd-extension-47a4f1e1-68aa-11e6-8a31-00505603244a; Last Active: 2016-09-12 20:40:01.575572+00:00
- # > Username: VSPHERE.LOCAL\vpxd-extension-47a4f1e1-68aa-11e6-8a31-00505603244a; Last Active: 2016-09-12 20:41:52.762972+00:00
- # > Terminating session 5201e9fd-0b25-252f-5af9-bfdaac1db480 of VSPHERE.LOCAL\vpxd-extension-47a4f1e1-68aa-11e6-8a31-00505603244a
- # > Terminating session 521470a5-3d92-a09a-029d-105f85ff4884 of VSPHERE.LOCAL\vpxd-extension-47a4f1e1-68aa-11e6-8a31-00505603244a
- # > Terminating session 52514270-b9e8-64d9-5070-a686ebb701d3 of VSPHERE.LOCAL\vpxd-extension-47a4f1e1-68aa-11e6-8a31-00505603244a
- # > Terminating session 52521e71-e3ed-c7b3-a2c1-6153fccebb42 of VSPHERE.LOCAL\vpxd-extension-47a4f1e1-68aa-11e6-8a31-00505603244a
- # > logout
- # > current pyVmomi session: None
- 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=False,
- 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()
- if args.password is None:
- args.password = getpass.getpass(
- prompt='Enter password for host {} and user {}: '.format(args.host,args.user))
- import ssl
- context = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
- context.verify_mode = ssl.CERT_NONE
- si = connect.SmartConnect(host=args.host,
- user=args.user,
- pwd=args.password,
- port=int(args.port),
- sslContext=context)
- print ("Logged in to {}".format(args.host))
- session_id = si.content.sessionManager.currentSession.key
- print ("current pyVmomi session id: {}".format(session_id))
- inactiveSessionsKey = []
- idledays = datetime.now() - timedelta(days=1)
- # Printing all sessions
- print ("Listing then terminating all sessions older than 1 day:")
- for session in si.content.sessionManager.sessionList:
- if session.lastActiveTime.replace(tzinfo=None) < idledays:
- inactiveSessionsKey.append([session.key,session.userName])
- print('Username: {}; Last Active: {}'.format(session.userName,session.lastActiveTime))
- # Comment this section out if you want to use only for listing old sessions
- for keyUsername in inactiveSessionsKey:
- print('Terminating session {} of {}'.format(keyUsername[0],keyUsername[1]))
- si.content.sessionManager.TerminateSession(keyUsername[0])
- # Logging out this active session
- print ("logout")
- si.content.sessionManager.Logout()
- # The current session will be None after logout
- session = si.content.sessionManager.currentSession
- print ("current pyVmomi session: {}".format(session))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement