Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
2.6 kB
1
Indexable
Never
# Get VM Details for all VMs from agent.properties file
def get_vm_details():
    #VMList = get_VMList()
    All_VM_Details = []

    for i in VMList:
        vm_id = i
        try:
            VM_Details = {vm_id:{'vm_ip':[],'vm_username':[],'vm_user_password':[],'vm_user_password_expiry_date':[]} }

            with open(agent_file, 'rt', encoding='latin-1') as file:
                for line in file:
                    if str(i) + ".privatip" in line:
                        vm_ip = str(line.split('='))
                        vm_ip = re.findall( r'[0-9]+(?:\.[0-9]+){3}', vm_ip)
                        #vm_ip = re.findall(r"^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$", vm_ip)
                        vm_ip = list(map(str, vm_ip))
                        vm_ip = vm_ip[0]
                        VM_Details[vm_id]['vm_ip'].append(vm_ip)
                    if str(i)+".ssh.host_username" in line:
                        vm_username = line.split('=')
                        vm_username= list(map(str,vm_username))
                        vm_username = vm_username[1].strip()
                        VM_Details[vm_id]['vm_username'].append(vm_username)
                    if str(i)+".ssh.host_password" in line:
                        vm_user_password = line.split('password=')
                        vm_user_password= list(map(str,vm_user_password))
                        vm_user_password = vm_user_password[1].strip()
                        VM_Details[vm_id]['vm_user_password'].append(vm_user_password)
                    if str(i)+".ssh.pwd_expiry_date" in line:
                        vm_user_password_expiry_date = line.split('expiry_date=')
                        vm_user_password_expiry_date = list(map(str,vm_user_password_expiry_date))
                        vm_user_password_expiry_date = vm_user_password_expiry_date[1].strip()
                        VM_Details[vm_id]['vm_user_password_expiry_date'].append(vm_user_password_expiry_date)

            if VM_Details[vm_id]['vm_ip'] and VM_Details[vm_id]['vm_user_password_expiry_date']:  # Check if vm_ip is not empty
                All_VM_Details.append(VM_Details)

        except FileNotFoundError:
            print(f"{agent_file} not found")
            logging.error("Agent file not found, %s", agent_file)
        except Exception as e:
            print(f"Failed to get details for VM {vm}: {e}")
            logging.error("Failed to get details for VM %s", e)
            continue

    all_vm_details = dict(zip(VMList, All_VM_Details))
    return all_vm_details