In this case, we will collect cpu levels from devices and than write them to excel file.


Import modules

import paramiko													
import time
import re
from pandas import DataFrame


Open command list text file. Than Create a List from Command_List file

c = open("Command_List.txt", "r")								   
command_list = c.read().split("\n")    


Open device list text file Create a List from Device_List file

d = open("Device_List.txt", "r")							   	   
hosts = d.read().split("\n")       


Enter port,user and password information

port = 22
username = "root"
password = "Test1234."


Create empty lists for later usage

logs = []
logs1= []
lista = []
listb = []


Loop each ip in hosts list

for ip in hosts:                                                   
    print("Try to login:", ip)
    conn = paramiko.SSHClient()
    conn.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    conn.connect(ip, port, username, password)
    comm = conn.invoke_shell()
    comm.send("s 0 t \n")


Loop each commmand in command_list 

  for command in command_list:                                    
        comm.send(' %s \n' %command)
        time.sleep(.5)
        output = comm.recv(65535)
        output = output.decode("utf-8")


Convert string to List without any change

        logs = output.split("xxxxx")   
        print("logs",logs)


Extend list with new logs list in for loop

        logs1.extend(logs)               


Convert list to string

    logs1 = ''.join(map(str, logs1))     


Find device name between < >

    device_name = re.findall("<\w+\W+\w+>", logs1)


Find CPU Usage value line

    cpu_level = re.findall("CPU Usage\s+: \d+%", logs1)      
    device = device_name[0]
    device = ''.join(map(str, device))


Remove "<" and ">" from Device name

    device = re.sub("<","",device)                                     
    device = re.sub(">","",device)                                     
    cpu = ''.join(map(str, cpu_level))


Find CPU value

    cpu = re.findall("[0-9]+%",cpu)                                    
    cpu = ''.join(map(str, cpu))


Add each device name to lista. Add each cpu value to lista

    lista.append(device)                                               
    listb.append(cpu)         


Open txt file named by hosts(ip) in for loop. Write string to file. Empty list after each for loop

    with open("{}.txt".format(ip), "w") as f:                          
        f.write(logs1)                                                 
    logs1 = []              


Create data frame with device name and cpu level

df = DataFrame({'NE Name': lista, 'CPU Value': listb })                


Create excel file and insert dataframe into excel file

df.to_excel('CPU Levels.xlsx', sheet_name='CPU LEVELS', index=False)   


Full code is as below:

import paramiko													
import time
import re
from pandas import DataFrame

c = open("Command_List.txt", "r")								 
command_list = c.read().split("\n")                                

d = open("Device_List.txt", "r")							   	 
hosts = d.read().split("\n")                                       
port = 22
username = "root"
password = "Test1234."
logs = []
logs1= []
lista = []
listb = []

for ip in hosts:                                                  
    print("Try to login:", ip)
    conn = paramiko.SSHClient()
    conn.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    conn.connect(ip, port, username, password)
    comm = conn.invoke_shell()
    comm.send("s 0 t \n")
    for command in command_list:                                   
        comm.send(' %s \n' %command)
        time.sleep(.5)
        output = comm.recv(65535)
        output = output.decode("utf-8")
        logs = output.split("xxxxx")   
        print("logs",logs)
        logs1.extend(logs)                                           
    logs1 = ''.join(map(str, logs1))                                  
    device_name = re.findall("<\w+\W+\w+>", logs1)                    
    cpu_level = re.findall("CPU Usage\s+: \d+%", logs1)       
    device = device_name[0]
    device = ''.join(map(str, device))
    device = re.sub("<","",device)                                    
    device = re.sub(">","",device)                                   
    cpu = ''.join(map(str, cpu_level))
    cpu = re.findall("[0-9]+%",cpu)                                   
    cpu = ''.join(map(str, cpu))
    lista.append(device)                                             
    listb.append(cpu)                                                  
    with open("{}.txt".format(ip), "w") as f:                        
        f.write(logs1)                                                
    logs1 = []                                                        

print("lista=",lista)
print("listb",listb)
df = DataFrame({'NE Name': lista, 'CPU Value': listb })               

df.to_excel('CPU Levels.xlsx', sheet_name='CPU LEVELS', index=False)