6

How can I read data from a big remote file using subprocess and ssh?

sdfasdfasdf
  • 149
  • 2
  • 3
  • 6
  • Do you want to run programs on the remote server with SSH that need to access the file? Do you want to copy the file to your computer with SCP? Do you want Python to be able to read or write directly to the file? You need to give more details about what you're trying to do for anyone to be able to answer this question. – agf Aug 21 '11 at 01:23
  • In my program I would like to list the files under a specific directory on a remote server. The user then selects to operate on one of these files. The files are quite big so I want to be able to read on the fly (to avoid memory issues) ie line by line. If the line meets a condition; do something with the line until end of file is reached. – sdfasdfasdf Aug 21 '11 at 10:14

4 Answers4

11
import subprocess
ssh = subprocess.Popen(['ssh', 'user@host', 'cat', 'path/to/file'],
                       stdout=subprocess.PIPE)
for line in ssh.stdout:
    line  # do stuff
Ross Patterson
  • 5,702
  • 20
  • 38
1

The answer above will work, but you'll have to setup your ssh login to use no password between your boxes. There are other ways to transfer files between computers using Python. A simple way, without authentication is to setup an apache server and use an http request.

0

For performance improvement, which is important when the file is big, there is rsync. For more information about the exact improvement see following post and the answer from Rafa: How does `scp` differ from `rsync`?

The algorithm would then be the following using rsync

import subprocess

subprocess.Popen(["rsync", host-ip+'/path/to/file'],stdout=subprocess.PIPE)
for line in ssh.stdout:
    line  # do stuff
Community
  • 1
  • 1
Paul Velthuis
  • 325
  • 4
  • 15
-1

Use iter with readline to read each full line:

for i in iter(f.stdout.readline,"")
Pokechu22
  • 4,984
  • 9
  • 37
  • 62
user2921139
  • 1,669
  • 4
  • 17
  • 25