misc_python

Sean's miscellaneous Python code that's not big enough for its own repo.
Log | Files | Refs | README | LICENSE

commit d9973b453272f336c4fe8578442f23846c3d3b2c
parent 9ba936b52808716f0e67072e8549fad8d1945954
Author: Sean Lynch <sean.lynch@netskope.com>
Date:   Mon,  7 Oct 2019 14:11:41 -0700

Add sshfwd and README.md

Diffstat:
AREADME.md | 29+++++++++++++++++++++++++++++
Asshfwd | 68++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 97 insertions(+), 0 deletions(-)

diff --git a/README.md b/README.md @@ -0,0 +1,29 @@ +# Sean Lynch's miscellaneous python code + +This is a repository where I stick Python stuff that's too small to +warrant its own repo. I have written these over a period of years, so +some are Python 2 and others are Python 3, and the quality is +extremely variable. This is by no means a complete representation of +the Python code I've written, just the stuff I thought to stick here. + +# License + +Everything in here is licensed under the GNU AGPL v3 or (at your +option) later. The reason for choosing AGPL is to avoid helping +organizations that prohibit its use. If you are impacted by my choice +of license, please consider working for a company that allows AGPL. + +# The files + +* hashtree.py - I have forgotten what this does. If you can figure it out, please let me +know. +* lam.py - An implementation of the Lambda Calculus. I forget how + complete it is. +* online.py - A very slow implementation of online codes, a fountain + code that will generate an infinite number of blocks, any n of which + will reconstruct the original file with very high probability. +* rss2json.py - Convert an RSS feed to JSON for processing with other + programs, like in a shell pipeline with jq. +* maze.py - Generate a maze and return a list of (x, y, vertical) + tuples with the locations of the walls. +* sieve.py - Naive implementation of the Sieve of Eritosthenes. diff --git a/sshfwd b/sshfwd @@ -0,0 +1,68 @@ +#!/usr/bin/env python3 +__author__ = 'Sean Lynch <code@seanlynch.email>' +__copyright__ = '2019' +__license__ = 'GNU AGPL v3 or later' +__version__ = '0.0.1' + +""" +Forward a port over ssh using nc in order to bypass "AllowTcpForwarding no". + +See program help text for help. + +""" + +import argparse +import asyncio +import subprocess + + +class Server: + def __init__(self, jumphost, localport, remotehost, remoteport): + self.jumphost = jumphost + self.localport = localport + self.remotehost = remotehost + self.remoteport = remoteport + + async def run(self): + server = await asyncio.start_server(self.client_connected, host='localhost', port=self.localport) + async with server: + await server.serve_forever() + + async def client_connected(self, reader, writer): + print('Client connected') + proc = await asyncio.create_subprocess_exec( + 'ssh', '-q', self.jumphost, 'nc', str(self.remotehost), str(self.remoteport), + stdin=asyncio.subprocess.PIPE, + stdout=asyncio.subprocess.PIPE) + + print('Connection forwarded') + await asyncio.wait([self.pump(reader, proc.stdin), self.pump(proc.stdout, writer)]) + + async def pump(self, reader, writer): + while not reader.at_eof(): + data = await reader.read(1024) + writer.write(data) + await writer.drain() + + writer.close() + print('reader closed') + + +def main(): + p = argparse.ArgumentParser( + description='Forward a port over ssh using nc to get around ' + '"AllowTcpForwarding no".') + p.add_argument('jumphost', help='[user@]host to jump through. Must use agent ' + 'authentication.') + p.add_argument('localport', help='port on localhost to forward') + p.add_argument('remotehost', help='remote host to forward to via the jump host') + p.add_argument('remoteport', help='port on the remote host to forward to') + args = p.parse_args() + server = Server(args.jumphost, args.localport, args.remotehost, args.remoteport) + asyncio.run(server.run()) + + +if __name__ == '__main__': + main() + +