Read remote file using python subprocess and ssh?

前端 未结 4 1170
感情败类
感情败类 2021-02-10 02:25

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

相关标签:
4条回答
  • 2021-02-10 03:14
    import subprocess
    ssh = subprocess.Popen(['ssh', 'user@host', 'cat', 'path/to/file'],
                           stdout=subprocess.PIPE)
    for line in ssh.stdout:
        line  # do stuff
    
    0 讨论(0)
  • 2021-02-10 03:19

    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
    
    0 讨论(0)
  • 2021-02-10 03:20

    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 讨论(0)
  • 2021-02-10 03:22

    Use iter with readline to read each full line:

    for i in iter(f.stdout.readline,"")
    
    0 讨论(0)
提交回复
热议问题