Read a file from server with SSH using Python

前端 未结 5 1197
北恋
北恋 2020-11-28 05:11

I am trying to read a file from a server using SSH from Python. I am using Paramiko to connect. I can connect to the server and run a command like cat filename

5条回答
  •  时光说笑
    2020-11-28 05:50

    It looks like back in Sept 2013 paramiko added the ability for these objects to support context managers natively, so if you want both Matt's clean answer with jfs's context manager, now all you need is:

    with ssh_client.open_sftp() as sftp_client:
        with sftp_client.open('remote_filename') as remote_file:
            for line in remote_file:
                # process line
    

提交回复
热议问题