Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read output line by line in python

hi i want to make ssh connection and parse some datas. Im using paramiko and here is part of my code:

ssh=ssh_pre.invoke_shell()
ssh.send("display ospf peer brief \n")
output = ssh.recv(10000)

everything work until this part

buf=StringIO.StringIO(output)
for lines in buf.read()
    print lines

this code print chars line by line . I want to print lines . what should i do?

like image 251
zeto Avatar asked May 13 '26 10:05

zeto


1 Answers

The issue is StringIO.read() returns a string, a sequence of characters, not lines. Try doing this:

buf=StringIO.StringIO(output)
for lines in buf.read().split("\n"):
    print lines

This will split your buffer by newlines and create a list of each line, rather than looping over each individual character in the string.

like image 172
David Reeve Avatar answered May 16 '26 01:05

David Reeve



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!