Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # coding=utf-8
- #
- # Copyright © 2016 VMware, Inc. All Rights Reserved.
- #
- # Licensed under the X11 (MIT) (the “License”) set forth below;
- #
- # you may not use this file except in compliance with the License. Unless required by applicable law or agreed to in
- # writing, software distributed under the License is distributed on an “AS IS” BASIS, without warranties or conditions
- # of any kind, EITHER EXPRESS OR IMPLIED. See the License for the specific language governing permissions and
- # limitations under the License. Permission is hereby granted, free of charge, to any person obtaining a copy of this
- # software and associated documentation files (the "Software"), to deal in the Software without restriction, including
- # without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
- # Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
- #
- # The above copyright notice and this permission notice shall be included in all copies or substantial portions of
- # the Software.
- #
- # "THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
- # INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
- # IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
- # AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
- # DEALINGS IN THE SOFTWARE.”
- __author__ = 'yfauser'
- from pyVim import connect
- from pyVmomi import vim
- import ssl
- import atexit
- import time
- from functools import wraps
- vchost = 'vc.yves.local'
- user = 'administrator@yves.local'
- pwd = 'vmware'
- searched_vm = '10.10.10.90'
- VIM_TYPES = {'datacenter': [vim.Datacenter]}
- def reauth():
- def reauth_decorator(f):
- @wraps(f)
- def function_reauth(*args):
- if args[0]['content'].sessionManager.currentSession:
- print '#### Session is still active'
- return f(*args)
- else:
- print '#### Session timed out, reconnecting'
- args[0]['content'].sessionManager.Login(userName=user, password=pwd)
- return f(*args)
- return function_reauth
- return reauth_decorator
- def connect_to_api(vchost, user='root', pwd='vmware'):
- if hasattr(ssl, 'SSLContext'):
- context = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
- context.verify_mode = ssl.CERT_NONE
- else:
- context = None
- if context:
- service_instance = connect.SmartConnect(host=vchost, user=user, pwd=pwd, sslContext=context)
- else:
- service_instance = connect.SmartConnect(host=vchost, user=user, pwd=pwd)
- atexit.register(connect.Disconnect, service_instance)
- return service_instance.RetrieveContent()
- @reauth()
- def get_vm_by_ip(vccontent, ip):
- return vccontent['content'].searchIndex.FindByIp(None, ip, True)
- @reauth()
- def get_vm_name(vccontent, vm):
- return vm.name
- def test_loop(vccontent):
- while True:
- vm = get_vm_by_ip(vccontent, searched_vm)
- print vm.name
- time.sleep(10800)
- def main():
- content = connect_to_api(vchost, user=user, pwd=pwd)
- vccontent = {'content': content, 'user': user, 'pwd': pwd}
- test_loop(vccontent)
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement