Receive file contents from remote SSH accountΒΆ

Here is some small server code that you can use to retrieve contents of remote files:

# content of servefiles.py

def servefiles(channel):
    for fn in channel:
        f = open(fn, 'rb')
        channel.send(f.read())
        f.close()

if __name__ == "__channelexec__":
    servefiles(channel)

And here is some code to use it to retrieve remote contents:

import execnet
import servefiles
gw = execnet.makegateway("ssh=codespeak.net")
channel = gw.remote_exec(servefiles)

for fn in ('/etc/passwd', '/etc/group'):
    channel.send(fn)
    content = channel.receive()
    print(fn)
    print(content)