python automation

python 1 week, 1 day ago
import re from collections import defaultdict def parse_hypervisor_details(text): data = {} current_section = None for line in text.splitlines(): line = line.strip() # Skip empty lines and separators if not line or "=" in line: continue # Detect Sections section_match = re.match(r'^(.*) Details', line) if section_match: current_section = section_match.group(1).replace(" ", "_").lower() data[current_section] = {} continue # Match key-value pairs if ":" in line: key, value = map(str.strip, line.split(":", 1)) key = key.replace(" ", "_").lower() data[current_section][key] = value continue # Process Hard Drives if current_section == "hard_drives" and len(line.split()) == 3: name, model, size = line.split(maxsplit=2) if "hard_drives" not in data: data["hard_drives"] = [] data["hard_drives"].append({"name": name, "model": model, "size": size}) continue # Process LLDP Switch Information if current_section == "lldp_switch_information" and "|" in line: interface, switch_name, port = map(str.strip, line.split("|")) if "lldp_switch_information" not in data: data["lldp_switch_information"] = [] data["lldp_switch_information"].append({"interface": interface, "switch_name": switch_name, "port": port}) continue return data def parse_vm_details(text): vms = [] vm_data = None for line in text.splitlines(): line = line.strip() # Match VM Name vm_match = re.match(r'---- VM: (.*?) ----', line) if vm_match: if vm_data: vms.append(vm_data) # Save previous VM data vm_data = {"VM Name": vm_match.group(1), "Disks": []} # Start new VM entry continue if not vm_data: continue # Skip lines until we find a VM entry # Match CPU, RAM, OS, and IP Address if line.startswith("CPU Assigned:"): vm_data["CPU"] = int(line.split(":")[1].strip()) elif line.startswith("RAM Assigned:"): vm_data["RAM"] = line.split(":")[1].strip() elif line.startswith("OS Version:"): vm_data["OS"] = line.split(":")[1].strip() elif line.startswith("IP Address:"): vm_data["IP Address"] = line.split(":")[1].strip() elif re.match(r'^[a-z]+\s+/.*\.qcow2$', line): disk_parts = line.split() vm_data["Disks"].append({"Device": disk_parts[0], "Path": disk_parts[1]}) if vm_data: vms.append(vm_data) # Add last VM entry return vms # Sample text input text_output = """ ================================================================== KVM Hypervisor Details for Host: mtznjrsv208 ================================================================== IPv4 Address: 135.25.186.161 IPv6 Address: fe80::211:aff:fe6a:bf78/64 iDRAC/iLO IP Address: 135.25.186.158 Default Gateway: 135.25.186.1 DNS Server IP's: 135.25.120.104 ================================================================== System Hardware Details ================================================================== Total CPU's: 48 Cores per Socket: 12 Total Sockets: 2 Threads per Core: 2 Total RAM: 251Gi ================================================================== Hard Drives ================================================================== Name Model Size sda LOGICAL_VOLUME 111.8G disk sdb LOGICAL_VOLUME 1.1T disk sdc LOGICAL_VOLUME 1.1T disk sdd LOGICAL_VOLUME 1.1T disk sde LOGICAL_VOLUME 1.1T disk ================================================================== Operating System ================================================================== OS: Ubuntu 20.04.5 LTS Kernel Version: 5.4.0-128-generic ================================================================== Server Hardware and BIOS ================================================================== Server Model: ProLiant DL380 Gen9 CPU Model: Intel(R) Xeon(R) CPU E5-2680 v3 @ 2.50GHz BIOS Version: P89 BIOS Release Date: 10/25/2017 Server Serial Number: MXQ546057N ATT Patch Level: Not Available Running VMs: 7 ================================================================== LLDP Switch Information ================================================================== Interface Switch Name Port ens3f0 | nasa-arista-C | ifname Ethernet18 ens6f1 | nasa-qfx-B | local 677 ens4f0 | nasa-arista-D | ifname Ethernet18 ================================================================== Virtual Machine Details ================================================================== ---- VM: mtznjv1tunl01 ---- CPU Assigned: 4 RAM Assigned: 8GB OS Version: Ubuntu 22.04.4 LTS Disks Assigned: ------------------------------------------------------------ vda /kvmhome7/tunl/mtznjv1tunl01/mtznjv1tunl01.qcow2 IP Address: 135.25.186.88 ---- VM: mtznjv1pinclts01 ---- CPU Assigned: 8 RAM Assigned: 14GB OS Version: Ubuntu 22.04.4 LTS Disks Assigned: --------------------------------------------------------------------------- hda /kvmhome7/pinclts/mtznjv1pinclts01/mtznjv1pinclts01.qcow2 vda /kvmhome7/pinclts/mtznjv1pinclts01/mtznjv1pinclts01-disk1.qcow2 vdb /kvmhome7/pinclts/mtznjv1pinclts01/mtznjv1pinclts01-disk2.qcow2 IP Address: 135.25.186.89 ---- VM: mtznjv1cnro11 ---- CPU Assigned: 16 RAM Assigned: 14GB OS Version: Rocky Linux 8.10 (Green Obsidian) Disks Assigned: ------------------------------------------------------------ hda /kvmhome7/cnro/mtznjv1cnro11/mtznjv1cnro11.qcow2 IP Address: 135.25.186.206 ---- VM: mtznjv1cnro12 ---- CPU Assigned: 4 RAM Assigned: 14GB OS Version: Rocky Linux 8.10 (Green Obsidian) Disks Assigned: ------------------------------------------------------------ hda /kvmhome7/cnro/mtznjv1cnro12/mtznjv1cnro12.qcow2 IP Address: 135.25.186.226 ---- VM: mtznjv1arub18 ---- CPU Assigned: 8 RAM Assigned: 8GB OS Version: Ubuntu 18.04.6 LTS Disks Assigned: ---------------------------------------------------------------- vda /kvmhome7/Arcadian/mtznjv1arub18/mtznjv1arub18.qcow2 IP Address: 135.25.186.29 ---- VM: mtznjv1arrky8 ---- CPU Assigned: 8 RAM Assigned: 8GB OS Version: Rocky Linux 8.9 (Green Obsidian) Disks Assigned: ---------------------------------------------------------------- hda /kvmhome7/Arcadian/mtznjv1arrky8/mtznjv1arrky8.qcow2 """ text2 = """ ================================================================== KVM Hypervisor Details for Host: mtznjrsv208 ================================================================== IPv4 Address: 135.25.186.161 IPv6 Address: fe80::211:aff:fe6a:bf78/64 iDRAC/iLO IP Address: 135.25.186.158 Default Gateway: 135.25.186.1 DNS Server IP's: 135.25.120.104 ================================================================== System Hardware Details ================================================================== Total CPU's: 48 Cores per Socket: 12 Total Sockets: 2 Threads per Core: 2 Total RAM: 251Gi ================================================================== Hard Drives ================================================================== Name Model Size sda LOGICAL_VOLUME 111.8G disk sdb LOGICAL_VOLUME 1.1T disk sdc LOGICAL_VOLUME 1.1T disk sdd LOGICAL_VOLUME 1.1T disk sde LOGICAL_VOLUME 1.1T disk ================================================================== Operating System ================================================================== OS: Ubuntu 20.04.5 LTS Kernel Version: 5.4.0-128-generic ================================================================== Server Hardware and BIOS ================================================================== Server Model: ProLiant DL380 Gen9 CPU Model: Intel(R) Xeon(R) CPU E5-2680 v3 @ 2.50GHz BIOS Version: P89 BIOS Release Date: 10/25/2017 Server Serial Number: MXQ546057N ATT Patch Level: Not Available Running VMs: 7 ================================================================== LLDP Switch Information ================================================================== Interface Switch Name Port ens3f0 | nasa-arista-C | ifname Ethernet18 ens6f1 | nasa-qfx-B | local 677 ens4f0 | nasa-arista-D | ifname Ethernet18 """ # Parse and print the result parsed_data = parse_vm_details(text_output) print(parsed_data) print(parse_hypervisor_details(text_output))
56
Python Script to create AWS beanstalk
#!/usr/bin/python
  
import boto
python aws beanstalk
sandeep sandeep
List all files and folders using python os mo
import os

def list_files_folders(path):
python python-os
kishore_kumar
Get current environment variables in python
import os
env = os.environ

python python-os
kishore_kumar
Get os details using python os
import os
print os.uname()
# Don't use os.system('uname -a'), its j
python python-os
kishore_kumar
Get stats ( lines, words, char count ) of fil
def file_stats(path):
    f = open(path, 'r')
    lines = f.readlines()
python
kishore_kumar
Use map function in python
def get_double(num):
    return num * 2

python
kishore_kumar
Python sample codes for beginners
print "Welcome to python"
python
gaya38 gaya38
Python program for even number checking
a=input("Enter a value:")
if (a%2==0):
    print "The given number is even numb
python
gaya38 gaya38
Python program for prime number check
a=input("Enter a value:")
k=0
b=(a/2)+1
python
gaya38 gaya38
Pass command line arguments in python
import sys
x=len(sys.argv)
a=[]
python
gaya38 gaya38
Python program for the largest number in an a
a = [1,43,98,5]#Dummy data
for l in range(len(a)-1):
        if (a[l]>a[l+1]):
python
gaya38 gaya38
print list of even numbers within a range
n=100
a=[10,20,30,40,50]
b=[60,70,80,90]
python
gaya38 gaya38
generate fibonacci series in python
n=input("Enter the constraint to print n
m=input("Enter the maximum value to prin
a=0
python
gaya38 gaya38
Generate Random number within the range in py
import random
print random.uniform(10,500)
python
gaya38 gaya38
Shuffle list elements in python
import random;
z = [1,90,4,2]
z = random.shuffle(z)
python
gaya38 gaya38
use python requests to get contents of url (
import requests

req = requests.get("https://httpbin.org/
python python-requests
kishore_kumar
how to iterate or get values in python dictio
sample_dict = { "number": 1, "fruits": [

for key in sample_dict:
python
kishore_kumar
create matrix and multiply using numpy in pyt
import numpy as np

matrix = [[1,2,3], [4,5,6], [7,8,9]]
python numpy
kishore_kumar
generate random numbers matrix with numpy pyt
import numpy as np

random_arr = np.random.randint(1,50,9)
python numpy
kishore_kumar
Find min , max and mean for numpy arrays
import numpy as np

random_arr = np.random.randint(1,50,9)
python numpy
kishore_kumar