Advertisement
Guest User

Untitled

a guest
Jun 11th, 2016
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.38 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # coding=utf-8
  3. #
  4. # Copyright © 2016 VMware, Inc. All Rights Reserved.
  5. #
  6. # Licensed under the X11 (MIT) (the “License”) set forth below;
  7. #
  8. # you may not use this file except in compliance with the License. Unless required by applicable law or agreed to in
  9. # writing, software distributed under the License is distributed on an “AS IS” BASIS, without warranties or conditions
  10. # of any kind, EITHER EXPRESS OR IMPLIED. See the License for the specific language governing permissions and
  11. # limitations under the License. Permission is hereby granted, free of charge, to any person obtaining a copy of this
  12. # software and associated documentation files (the "Software"), to deal in the Software without restriction, including
  13. # without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
  14. # Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
  15. #
  16. # The above copyright notice and this permission notice shall be included in all copies or substantial portions of
  17. # the Software.
  18. #
  19. # "THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  20. # INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  21. # IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
  22. # AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  23. # DEALINGS IN THE SOFTWARE.”
  24.  
  25. __author__ = 'yfauser'
  26.  
  27. from pyVim import connect
  28. from pyVmomi import vim
  29. import ssl
  30. import atexit
  31. import time
  32. from functools import wraps
  33.  
  34.  
  35. vchost = 'vc.yves.local'
  36. user = 'administrator@yves.local'
  37. pwd = 'vmware'
  38. searched_vm = '10.10.10.90'
  39.  
  40.  
  41. VIM_TYPES = {'datacenter': [vim.Datacenter]}
  42.  
  43.  
  44. def reauth():
  45. def reauth_decorator(f):
  46. @wraps(f)
  47. def function_reauth(*args):
  48. if args[0]['content'].sessionManager.currentSession:
  49. print '#### Session is still active'
  50. return f(*args)
  51. else:
  52. print '#### Session timed out, reconnecting'
  53. args[0]['content'].sessionManager.Login(userName=user, password=pwd)
  54. return f(*args)
  55. return function_reauth
  56. return reauth_decorator
  57.  
  58.  
  59. def connect_to_api(vchost, user='root', pwd='vmware'):
  60. if hasattr(ssl, 'SSLContext'):
  61. context = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
  62. context.verify_mode = ssl.CERT_NONE
  63. else:
  64. context = None
  65. if context:
  66. service_instance = connect.SmartConnect(host=vchost, user=user, pwd=pwd, sslContext=context)
  67. else:
  68. service_instance = connect.SmartConnect(host=vchost, user=user, pwd=pwd)
  69.  
  70. atexit.register(connect.Disconnect, service_instance)
  71.  
  72. return service_instance.RetrieveContent()
  73.  
  74. @reauth()
  75. def get_vm_by_ip(vccontent, ip):
  76. return vccontent['content'].searchIndex.FindByIp(None, ip, True)
  77.  
  78. @reauth()
  79. def get_vm_name(vccontent, vm):
  80. return vm.name
  81.  
  82. def test_loop(vccontent):
  83. while True:
  84. vm = get_vm_by_ip(vccontent, searched_vm)
  85. print vm.name
  86. time.sleep(10800)
  87.  
  88. def main():
  89. content = connect_to_api(vchost, user=user, pwd=pwd)
  90. vccontent = {'content': content, 'user': user, 'pwd': pwd}
  91. test_loop(vccontent)
  92.  
  93. if __name__ == '__main__':
  94. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement