Looping through a list of hosts and commands in Paramiko and store the result in excel with column headers

StackOverflow https://stackoverflow.com/questions/21576794

  •  07-10-2022
  •  | 
  •  

Вопрос

I have a file which has a list of hosts and I have another file which has a list of commands.I am trying to loop through host file and run all the commands in the command file using paramiko.The commands are basically checking parameters like uptime,current cpu,number of users logged in etc. This is the code that I have :

#! /usr/bin/python

import sys
import paramiko


username = "log_me_in"
password = "secret"

# Opens files in read mode
f1 = open(hostfile,"r")
f2 = open(commandfile,"r")

# Creates list based on f1 and f2
devices = f1.readlines()
commands = f2.readlines()



for device in devices:
    device = device.rstrip()
    for command in commands:
        command = command.rstrip()
        ssh = paramiko.SSHClient()
        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        ssh.connect(device, username=username, password=password)
        stdin, stdout, stderr = ssh.exec_command(command)
        output = stdout.read()
        data =[]
        data.append(output)
        ssh.close()
f1.close()
f2.close()

The problem I have is after I execute the commands on the remote machine.I want the stdout of each command that is executed to be stored in an excel sheet in one row for each device description herelike this. I have heard of python libraries like xlwt but was looking for help on how to accomplish this.

Это было полезно?

Решение

For the next two pieces of code, I'll assume the existence of the following:

header = ["Device Name", "Uptime", "CPU", "Count of users logged in"]
data = [
    ('ns1', '200 days', '10%', '15'),
    ('ns2', '23 days', '12%', '23'),
    ('ns3', '45 days', '56%', '108')
]

Using CSV makes it for a bit easier code and it's easy to deploy on any machine, because it doesn't require to install any further package (csv comes standard with Python):

import csv

with open('output.csv', 'wb') as csvfile:
    spamwriter = csv.writer(csvfile, dialect='excel')
    spamwriter.writerow(header)
    for row in data:
        spamwriter.writerow(row)

would produce the following output.csv file:

Device Name,Uptime,CPU,Count of users logged in
ns1,200 days,10%,15
ns2,23 days,12%,23
ns3,45 days,56%,108

Now, if you really want to use xlwt, here would be the equivalent (adding extra yellow background color for the header):

import xlwt

book = xlwt.Workbook()
sheet = book.add_sheet("foobar")

# Put the header in the appropriate cells...
style = xlwt.easyxf('pattern: pattern solid, fore-colour yellow')
for col, text in enumerate(header):
    sheet.write(0, col, text, style)

# Now, let's write the contents

for row, data_in_row in enumerate(data):
    for col, text in enumerate(data_in_row):
        sheet.write(row + 1, col, text)

book.save("example.xls")
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top