Last active
December 17, 2015 15:59
-
-
Save aslpavel/5635610 to your computer and use it in GitHub Desktop.
Cat remote file over ssh (with pretzel framework)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""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