def get_dummy_ssh(self, retry=False): ssh = SSH("", "", "") ssh.test_connectivity = Mock(return_value=True) ssh.execute = Mock(return_value=("10", "", 0)) ssh.sftp_put_files = Mock(return_value=True) ssh.sftp_mkdir = Mock(return_value=True) ssh.sftp_put_dir = Mock(return_value=True) ssh.sftp_put = Mock(return_value=True) return ssh
def execute(self, command, timeout=None): return SSH.execute(self, command, timeout)
def execute(self, command, timeout = None): return SSH.execute(self, command, timeout)
def changeVMCredentials(vm, pk_file): if vm['os'] == "windows": if 'passwd' in vm and vm['passwd'] and 'new_passwd' in vm and vm[ 'new_passwd']: try: import winrm except: CtxtAgent.logger.exception("Error importing winrm.") return False try: url = "https://" + vm['ip'] + ":5986" s = winrm.Session(url, auth=(vm['user'], vm['passwd'])) r = s.run_cmd('net', ['user', vm['user'], vm['new_passwd']]) # this part of the code is never reached ... if r.status_code == 0: vm['passwd'] = vm['new_passwd'] return True else: CtxtAgent.logger.error( "Error changing password to Windows VM: " + r.std_out) return False except winrm.exceptions.AuthenticationError: # if the password is correctly changed the command returns this # error try: # let's check that the new password works s = winrm.Session(url, auth=(vm['user'], vm['new_passwd'])) r = s.run_cmd('echo', ['OK']) if r.status_code == 0: vm['passwd'] = vm['new_passwd'] return True else: CtxtAgent.logger.error( "Error changing password to Windows VM: " + r.std_out) return False except: CtxtAgent.logger.exception( "Error changing password to Windows VM: " + vm['ip'] + ".") return False except: CtxtAgent.logger.exception( "Error changing password to Windows VM: " + vm['ip'] + ".") return False else: # Linux VMs # Check if we must change user credentials in the VM if 'passwd' in vm and vm['passwd'] and 'new_passwd' in vm and vm[ 'new_passwd']: CtxtAgent.logger.info("Changing password to VM: " + vm['ip']) private_key = vm['private_key'] if pk_file: private_key = pk_file try: ssh_client = SSH(vm['ip'], vm['user'], vm['passwd'], private_key, vm['remote_port']) sudo_pass = "" if ssh_client.password: sudo_pass = "******" + ssh_client.password + "' | " (out, err, code) = ssh_client.execute( sudo_pass + 'sudo -S bash -c \'echo "' + vm['user'] + ':' + vm['new_passwd'] + '" | /usr/sbin/chpasswd && echo "OK"\' 2> /dev/null') except: CtxtAgent.logger.exception( "Error changing password to VM: " + vm['ip'] + ".") return False if code == 0: vm['passwd'] = vm['new_passwd'] return True else: CtxtAgent.logger.error("Error changing password to VM: " + vm['ip'] + ". " + out + err) return False if 'new_public_key' in vm and vm[ 'new_public_key'] and 'new_private_key' in vm and vm[ 'new_private_key']: CtxtAgent.logger.info("Changing public key to VM: " + vm['ip']) private_key = vm['private_key'] if pk_file: private_key = pk_file try: ssh_client = SSH(vm['ip'], vm['user'], vm['passwd'], private_key, vm['remote_port']) (out, err, code) = ssh_client.execute_timeout( 'echo ' + vm['new_public_key'] + ' >> .ssh/authorized_keys', 5) except: CtxtAgent.logger.exception( "Error changing public key to VM: " + vm['ip'] + ".") return False if code != 0: CtxtAgent.logger.error( "Error changing public key to VM:: " + vm['ip'] + ". " + out + err) return False else: vm['private_key'] = vm['new_private_key'] return True return False