Read remote file using python subprocess and ssh?
26,014
Solution 1
import subprocess
ssh = subprocess.Popen(['ssh', 'user@host', 'cat', 'path/to/file'],
stdout=subprocess.PIPE)
for line in ssh.stdout:
line # do stuff
Solution 2
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.
Author by
sdfasdfasdf
Updated on February 22, 2020Comments
-
sdfasdfasdf about 4 years
How can I read data from a big remote file using subprocess and ssh?
-
user2921139 over 9 yearsRoss, I have the exact same command that you suggest but that only reads the first line and does not read the other lines.. How do I make it read all the lines in the file?
-
user2921139 over 9 yearsI should say that by using the above you have, the file contents are read character by character.. how can i read a whole line completely? "split('\n')" does not seem to work..
-
Elle over 9 yearsRemove
.readline()
afterssh.stdout
, you want to loop on the output not the first line from it. -
Ross Patterson over 9 years@JordanTrudgett Hmm, I submitted the edit when I said, but I can't find anything about it being rejected. Submitted it again.
-
Haifeng Zhang almost 9 years@RossPatterson will this affect the servers bandwidth?
-
Ross Patterson almost 9 years@haifzhan No more or less than any other way to transfer the file.
-
CutePoison over 4 yearsI am not getting any feedback - dont know if it can connect but not find the file or..