Skip to content

Instantly share code, notes, and snippets.

@aslpavel
Last active December 17, 2015 15:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aslpavel/5635610 to your computer and use it in GitHub Desktop.
Save aslpavel/5635610 to your computer and use it in GitHub Desktop.
Cat remote file over ssh (with pretzel framework)
"""Cat remote file over ssh
pretzel framework: https://github.com/aslpavel/pretzel
"""
from __future__ import print_function
import os
import sys
from pretzel.app import app
from pretzel.monad import async
from pretzel.remoting import SSHConnection
SIZE_LIMIT = 1 << 20 # 1Mb
@app
def main():
"""Read remote file
"""
if len(sys.argv) < 3:
sys.stderr.write('usage: {} <host> <path>\n'.format(sys.argv[0]))
sys.exit(1)
host = sys.argv[1]
path = sys.argv[2]
with (yield SSHConnection(host)) as ssh: # create and connect
# ssh(target) returns targets proxy object. When proxy object is awaited
# request is send to remote side and executed. All exception are
# marshaled.
# check file access
if not (yield ssh(os.access)(path, os.R_OK)):
sys.stderr.write('[error] no such file: {}\n'.format(path))
sys.exit(1)
# check file size
size = yield ssh(os.path.getsize)(path)
if size > SIZE_LIMIT:
sys.stderr.write('[error] file is too big: {}\n'.format(size))
sys.exit(1)
# read file
print((yield ssh(open)(path).read()))
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment