0

I am reading a .exp file opened in VI on a remote server using python. While Option 2 works for me, it can't read ~1024 bytes at a time. Is my usage of iterator object incorrect in Option 1? Also, I can't figure out why I am unable to read from the stdout buffer more than 512 byte chunks at a time.

Option 1:

#Output: Can't Read File!
target = open("out.txt", 'w')

def readFile():
       return (str(stdout.read(512))).splitlines(True)

try:
   stdin, stdout, stderr = ssh.exec_command('vi <path\filename.exp>')
   for line in iter(readFile,''):
      target.write(line)
   stdin, stdout, stderr=ssh.exec_command(':q!')
except:
   print("Can't Read File!") 

Option 2:

#works, but hangs when using stdout.read(1024)
try:
    stdin, stdout, stderr = ssh.exec_command('vi <path\filename.exp>')
    to_string = (str(stdout.read(512))).splitlines(True)
    for line in to_string:
        target.write(line)
    stdin, stdout, stderr=ssh.exec_command(':q!')
except:
    print("Can't Read File!")
sg491
  • 11
  • 5
  • 1
    there are several issues in the code. Why do you use `vi` to read a file? `paramiko` gives you access to a sftp client that you could use to read the file. – jfs Jun 08 '15 at 20:37
  • `vi` won't close stdout until it quits, so you'll only get an EOF (`''` in this case) _after_ you've sent the quit command, until then reads will block if the buffer is empty... I can't really see any reason to do this... – mata Jun 08 '15 at 20:52
  • Thanks! That saved me precious hours! The SFTP documentation was a little unclear for me, but [ssh programming with paramiko | completely different by Jesse Noller](http://jessenoller.com/blog/2009/02/05/ssh-programming-with-paramiko-completely-different) was a great starting point for someone who has never used paramiko. – sg491 Jun 09 '15 at 18:43
  • related: [Read a file from server with ssh using python](http://stackoverflow.com/questions/1596963/read-a-file-from-server-with-ssh-using-python) – jfs Jun 09 '15 at 18:51

0 Answers0