3

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?

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
zeto
  • 53
  • 2
  • 7
  • You want write all buffer in one shot. Isn't it? – Michele d'Amico Dec 07 '14 at 18:24
  • my main purpose i will read what is return from the server and find specific line. for example the line includes 'apple'. if i write line by line i believe i can do what i want – zeto Dec 07 '14 at 18:56

1 Answers1

2

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.

David Reeve
  • 863
  • 9
  • 16