def openConnection(myvc, myadmin, mypwd): """ Opens a connection to a VC/ESXi Args: myvc (str): vCenter/ESXi IP or FQDN Raises: exception: [Timeout, Invalid Login, etc] Returns: [connection,vms]: Returns both the connection open as well as a list of VMs present on the VC/ESXi """ print("\n========== %s ==========" % myvc) #Open VC Connection try: client = VConnector(user=myadmin, pwd=mypwd, host=myvc) #print("Connecting to %s using account %s" % (myvc,myadmin)) client.connect() vms = client.get_vm_view() #Obtain VMs globals.hostReport += '-' + myvc + ' OK!\n' return client, vms except Exception as exception: print(exception.__class__.__name__) print("Unexpected error:", sys.exc_info()[0]) print( "Please confirm\n1) The vCenter/ESXi is reachable\n2) If FQDN is used, that it is resolvable\n3) Credentials are correct\n" ) raise exception print("Connected to VC %s" % myvc)
def create_agents(self): """ Prepares the vSphere Agents used by the vPoller Worker Raises: VPollerException """ logger.debug('Creating vSphere Agents') db = VConnectorDatabase(self.config.get('db')) agents = db.get_agents(only_enabled=True) if not agents: logger.warning('No registered or enabled vSphere Agents found') raise VPollerException( 'No registered or enabled vSphere Agents found') for agent in agents: a = VConnector( user=agent['user'], pwd=agent['pwd'], host=agent['host'], cache_enabled=self.config.get('cache_enabled'), cache_maxsize=self.config.get('cache_maxsize'), cache_ttl=self.config.get('cache_ttl'), cache_housekeeping=self.config.get('cache_housekeeping')) self.agents[a.host] = a logger.info('Created vSphere Agent for %s', agent['host'])
def openConnection(): print("========== %s ==========" % myvc) #Open VC Connection try: client = VConnector(user=myadmin, pwd=mypwd, host=myvc) client.connect() return client.get_vm_view() #Obtain VMs except Exception as exception: print(exception.__class__.__name__) print("Unexpected error:", sys.exc_info()[0]) print( "Please confirm\n1) The vCenter/ESXi is reachable\n2) If FQDN is used, that it is resolvable\n3) Credentials are correct\n" ) hostReport += myvc + ' NOT OK!\n' raise exception print("Connected to VC %s" % myvc)
def check_login(user, pw, host): try: client = VConnector(user=user, pwd=pw, host=host) client.connect() client.disconnect() del client return True except: client.disconnect() del client return False
#------------------------------------------------------------------------------- # Name: module1 # Purpose: # # Author: Shoblin # # Created: 27.03.2019 # Copyright: (c) Shoblin 2019 # Licence: <your licence> #------------------------------------------------------------------------------- from __future__ import print_function from vconnector.core import VConnector username = '******' password = '******' hostname = 'msk-vcenter1.office.finam.ru' client = VConnector( user=username, pwd=password, host=hostname) client.connect() vms = client.get_vm_view() print(vms.view) client.disconnect()
def login(self): """ Login to the VMware vSphere host Returns: True on successful connect, False otherwise """ form_text = ('Enter IP address or DNS name ' 'of the VMware vSphere host you wish ' 'to connect to.\n') elements = [ pvc.widget.form.FormElement(label='Hostname'), pvc.widget.form.FormElement(label='Username'), pvc.widget.form.FormElement(label='Password', attributes=0x1), ] form = pvc.widget.form.Form( dialog=self.dialog, form_elements=elements, mixed_form=True, title='Login Details', text=form_text, ) while True: code, fields = form.display() if code in (self.dialog.CANCEL, self.dialog.ESC): return False if not all(fields.values()): self.dialog.msgbox( title='Error', text='Invalid login details, please try again.\n') continue self.dialog.infobox( title='Establishing Connection', text='Connecting to {} ...'.format(fields['Hostname']), ) self.agent = VConnector( host=fields['Hostname'], user=fields['Username'], pwd=fields['Password'], ) try: self.agent.connect() text = '{} - {} - Python vSphere Client version {}' background_title = text.format( self.agent.host, self.agent.si.content.about.fullName, __version__) self.dialog.set_background_title(background_title) return True except Exception as e: if isinstance(e, pyVmomi.vim.MethodFault): msg = e.msg else: msg = e self.dialog.msgbox(title='Login failed', text='Failed to login to {}\n\n{}\n'.format( self.agent.host, msg))
class MainApp(object): """ Main App class """ def __init__(self): self.dialog = Dialog(autowidgetsize=True) self.dialog.add_persistent_args(['--no-mouse']) self.dialog.set_background_title( 'Python vSphere Client version {}'.format(__version__)) self.agent = None def about(self): welcome = ('Welcome to the Python vSphere Client version {}.\n\n' 'PVC is hosted on Github. Please contribute by reporting ' 'issues, suggesting features and sending patches using ' 'pull requests.\n\n' 'https://github.com/dnaeon/pvc') self.dialog.msgbox(title='Welcome', text=welcome.format(__version__)) def login(self): """ Login to the VMware vSphere host Returns: True on successful connect, False otherwise """ form_text = ('Enter IP address or DNS name ' 'of the VMware vSphere host you wish ' 'to connect to.\n') elements = [ pvc.widget.form.FormElement(label='Hostname'), pvc.widget.form.FormElement(label='Username'), pvc.widget.form.FormElement(label='Password', attributes=0x1), ] form = pvc.widget.form.Form( dialog=self.dialog, form_elements=elements, mixed_form=True, title='Login Details', text=form_text, ) while True: code, fields = form.display() if code in (self.dialog.CANCEL, self.dialog.ESC): return False if not all(fields.values()): self.dialog.msgbox( title='Error', text='Invalid login details, please try again.\n') continue self.dialog.infobox( title='Establishing Connection', text='Connecting to {} ...'.format(fields['Hostname']), ) self.agent = VConnector( host=fields['Hostname'], user=fields['Username'], pwd=fields['Password'], ) try: self.agent.connect() text = '{} - {} - Python vSphere Client version {}' background_title = text.format( self.agent.host, self.agent.si.content.about.fullName, __version__) self.dialog.set_background_title(background_title) return True except Exception as e: if isinstance(e, pyVmomi.vim.MethodFault): msg = e.msg else: msg = e self.dialog.msgbox(title='Login failed', text='Failed to login to {}\n\n{}\n'.format( self.agent.host, msg)) def disconnect(self): """ Disconnect from the remote vSphere host """ if not self.agent: return self.dialog.infobox(title='Disconnecting Connection', text='Disconnecting from {} ...'.format( self.agent.host)) self.agent.disconnect() def run(self): try: self.about() if not self.login(): return home = pvc.widget.home.HomeWidget(agent=self.agent, dialog=self.dialog) home.display() except KeyboardInterrupt: pass finally: self.disconnect()
def login(self): """ Login to the VMware vSphere host Returns: True on successful connect, False otherwise """ form_text = ( 'Enter IP address or DNS name ' 'of the VMware vSphere host you wish ' 'to connect to.\n' ) elements = [ pvc.widget.form.FormElement(label='Hostname'), pvc.widget.form.FormElement(label='Username'), pvc.widget.form.FormElement(label='Password', attributes=0x1), ] form = pvc.widget.form.Form( dialog=self.dialog, form_elements=elements, mixed_form=True, title='Login Details', text=form_text, ) while True: code, fields = form.display() if code in (self.dialog.CANCEL, self.dialog.ESC): return False if not all(fields.values()): self.dialog.msgbox( title='Error', text='Invalid login details, please try again.\n' ) continue self.dialog.infobox( title='Establishing Connection', text='Connecting to {} ...'.format(fields['Hostname']), ) self.agent = VConnector( host=fields['Hostname'], user=fields['Username'], pwd=fields['Password'], ) try: self.agent.connect() text = '{} - {} - Python vSphere Client version {}' background_title = text.format( self.agent.host, self.agent.si.content.about.fullName, __version__ ) self.dialog.set_background_title(background_title) return True except Exception as e: self.dialog.msgbox( title='Login failed', text='Failed to login to {}\n\n{}\n'.format(self.agent.host, e.msg) )
class MainApp(object): """ Main App class """ def __init__(self): self.dialog = Dialog(autowidgetsize=True) self.dialog.set_background_title( 'Python vSphere Client version {}'.format(__version__) ) self.agent = None def about(self): welcome = ( 'Welcome to the Python vSphere Client version {}.\n\n' 'PVC is hosted on Github. Please contribute by reporting ' 'issues, suggesting features and sending patches using ' 'pull requests.\n\n' 'https://github.com/dnaeon/pvc' ) self.dialog.msgbox( title='Welcome', text=welcome.format(__version__) ) def login(self): """ Login to the VMware vSphere host Returns: True on successful connect, False otherwise """ form_text = ( 'Enter IP address or DNS name ' 'of the VMware vSphere host you wish ' 'to connect to.\n' ) elements = [ pvc.widget.form.FormElement(label='Hostname'), pvc.widget.form.FormElement(label='Username'), pvc.widget.form.FormElement(label='Password', attributes=0x1), ] form = pvc.widget.form.Form( dialog=self.dialog, form_elements=elements, mixed_form=True, title='Login Details', text=form_text, ) while True: code, fields = form.display() if code in (self.dialog.CANCEL, self.dialog.ESC): return False if not all(fields.values()): self.dialog.msgbox( title='Error', text='Invalid login details, please try again.\n' ) continue self.dialog.infobox( title='Establishing Connection', text='Connecting to {} ...'.format(fields['Hostname']), ) self.agent = VConnector( host=fields['Hostname'], user=fields['Username'], pwd=fields['Password'], ) try: self.agent.connect() text = '{} - {} - Python vSphere Client version {}' background_title = text.format( self.agent.host, self.agent.si.content.about.fullName, __version__ ) self.dialog.set_background_title(background_title) return True except Exception as e: self.dialog.msgbox( title='Login failed', text='Failed to login to {}\n\n{}\n'.format(self.agent.host, e.msg) ) def run(self): self.about() if not self.login(): return home = pvc.widget.home.HomeWidget( agent=self.agent, dialog=self.dialog ) home.display() self.dialog.infobox( title='Disconnecting Connection', text='Disconnecting from {} ...'.format(self.agent.host) ) self.agent.disconnect()