I have a requirement to log into multiple devices,run multiple commands and store the output.I am currently achieving this using paramiko for remote SSH and then storing the results in an excel sheet using xlswriter. This is the code that I currently have :
import getpass
import paramiko
import xlsxwriter
username = raw_input('Enter username for device login:')
def enterPassword():
while True: # repeat forever
password = getpass.getpass('Enter corresponding password:')
password_again = getpass.getpass('Confirm password:')
if password != password_again:
print 'Password and confirmation do not match.Please try again!!'
else:
return password
password = enterPassword()
print "Running the tests..this might take some time.."
# Opens file in read mode
f1 = open('hostnames','r')
f2 = open('commandlist','r')
# Creates list based on f1
devices = f1.readlines()
commands = f2.readlines()
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
data = []
for device in devices:
column = device.split()
data.append([column[0]])
print column[0]
for command in commands:
try:
conn=ssh.connect(column[1], username=username, password=password, timeout=4)
if conn is None:
stdin, stdout, stderr = ssh.exec_command(command)
data[-1].append(stdout.read())
ssh.close()
except paramiko.AuthenticationException:
output = "Authentication Failed"
data[-1].append(output)
break
except paramiko.SSHException:
output = "Issues with SSH service"
data[-1].append(output)
break
except socket.error, e:
output = "Connection Error"
data[-1].append(output)
break
data[-1] = tuple(data[-1])
f1.close()
f2.close()
book = xlsxwriter.Workbook('finalresults.xlsx')
sheet = book.add_worksheet('sheet1')
for row, data_in_row in enumerate(data):
for col, text in enumerate(data_in_row):
sheet.write(row + 1, col, text)
book.close()
This works perfectly fine on remote machines running bash and I get just the output of the commands that I run.However,on certain machines that don't run bash,I get the command run and extra prompts in the output as follows:
How do I strip the first line and the last two lines from the stdout.read() for each command within the loop.I have heard of using grep with subprocess but was looking for inbuilt python string operators
EDIT:
So I did a bit more reading,trolled a few sites and this is what I have :
data = []
offending = [">"]
for device in devices:
column = device.split()
data.append([column[0]])
print column[0]
for command in commands:
try:
conn=ssh.connect(column[1], username=username, password=password, timeout=4)
if conn is None:
stdin, stdout, stderr = ssh.exec_command(command)
for line in stdout.readline():
if not True in [item in line for item in offending]:
output = line
data[-1].append(output)
ssh.close()
However,now I have blank cells.I tried this on the command line interpreter and it worked fine.What could be wrong ??