daily pastebin goal
10%
SHARE
TWEET

Untitled

a guest Sep 19th, 2016 64 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env python
  2.  
  3. import argparse
  4. import getpass
  5.  
  6. import pyVim.connect as connect
  7. from datetime import datetime
  8. from datetime import timedelta
  9.  
  10. # The current session should show None as the script completes
  11.  
  12. # Example output:
  13. # > Logged in to vcsa
  14. # > current pyVmomi session id: 525471f5-eb36-67bc-260f-216cd36df9c7
  15. # > Listing then terminating all sessions older than 1 day:
  16. # > Username: VSPHERE.LOCAL\vpxd-extension-47a4f1e1-68aa-11e6-8a31-00505603244a; Last Active: 2016-09-12 20:43:23.982896+00:00
  17. # > Username: VSPHERE.LOCAL\vpxd-extension-47a4f1e1-68aa-11e6-8a31-00505603244a; Last Active: 2016-09-12 20:37:44.098979+00:00
  18. # > Username: VSPHERE.LOCAL\vpxd-extension-47a4f1e1-68aa-11e6-8a31-00505603244a; Last Active: 2016-09-12 20:40:01.575572+00:00
  19. # > Username: VSPHERE.LOCAL\vpxd-extension-47a4f1e1-68aa-11e6-8a31-00505603244a; Last Active: 2016-09-12 20:41:52.762972+00:00
  20. # > Terminating session 5201e9fd-0b25-252f-5af9-bfdaac1db480 of VSPHERE.LOCAL\vpxd-extension-47a4f1e1-68aa-11e6-8a31-00505603244a
  21. # > Terminating session 521470a5-3d92-a09a-029d-105f85ff4884 of VSPHERE.LOCAL\vpxd-extension-47a4f1e1-68aa-11e6-8a31-00505603244a
  22. # > Terminating session 52514270-b9e8-64d9-5070-a686ebb701d3 of VSPHERE.LOCAL\vpxd-extension-47a4f1e1-68aa-11e6-8a31-00505603244a
  23. # > Terminating session 52521e71-e3ed-c7b3-a2c1-6153fccebb42 of VSPHERE.LOCAL\vpxd-extension-47a4f1e1-68aa-11e6-8a31-00505603244a
  24. # > logout
  25. # > current pyVmomi session: None
  26.  
  27.  
  28. parser = argparse.ArgumentParser()
  29. parser.add_argument('-s', '--host',
  30.                     required=True,
  31.                     action='store',
  32.                     help='Remote host to connect to')
  33.  
  34. parser.add_argument('-u', '--user',
  35.                     required=True,
  36.                     action='store',
  37.                     help='User name to use when connecting to host')
  38.  
  39. parser.add_argument('-p', '--password',
  40.                     required=False,
  41.                     action='store',
  42.                     help='Password to use when connecting to host')
  43.  
  44. parser.add_argument('-o', '--port',
  45.                     required=False,
  46.                     action='store',
  47.                     help="port to use, default 443", default=443)
  48.  
  49. args = parser.parse_args()
  50. if args.password is None:
  51.     args.password = getpass.getpass(
  52.         prompt='Enter password for host {} and user {}: '.format(args.host,args.user))
  53.  
  54.  
  55. import ssl
  56. context = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
  57. context.verify_mode = ssl.CERT_NONE
  58.  
  59. si = connect.SmartConnect(host=args.host,
  60.                                         user=args.user,
  61.                                         pwd=args.password,
  62.                                         port=int(args.port),
  63.                                         sslContext=context)
  64.  
  65. print ("Logged in to {}".format(args.host))
  66. session_id = si.content.sessionManager.currentSession.key
  67. print ("current pyVmomi session id: {}".format(session_id))
  68.  
  69. inactiveSessionsKey = []
  70. idledays = datetime.now() - timedelta(days=1)
  71.  
  72. # Printing all sessions
  73. print ("Listing then terminating all sessions older than 1 day:")
  74. for session in si.content.sessionManager.sessionList:
  75.     if session.lastActiveTime.replace(tzinfo=None) < idledays:
  76.         inactiveSessionsKey.append([session.key,session.userName])
  77.         print('Username: {}; Last Active: {}'.format(session.userName,session.lastActiveTime))
  78.  
  79. # Comment this section out if you want to use only for listing old sessions
  80. for keyUsername in inactiveSessionsKey:
  81. print('Terminating session {} of {}'.format(keyUsername[0],keyUsername[1]))
  82. si.content.sessionManager.TerminateSession(keyUsername[0])
  83.  
  84. # Logging out this active session
  85. print ("logout")
  86. si.content.sessionManager.Logout()
  87.  
  88. # The current session will be None after logout
  89. session = si.content.sessionManager.currentSession
  90. print ("current pyVmomi session: {}".format(session))
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top