#! /usr/bin/env python3 # encoding: utf-8 import jsmin import json import subprocess import sys def load_cfg(path): with open(path) as f: return json.loads(jsmin.jsmin(f.read())) if __name__ == "__main__": # try config files in the order they are passed to us # first one wins cfg = None for path in sys.argv[1:]: try: cfg = load_cfg(path) print("> using %s" %(path)) break except Exception as e: print("! cfg file %s failed to load (%s)" %(path, e)) if not cfg: print("! unable to load any configuration") sys.exit(1) command = ["/usr/bin/ssh", "-o", "ServerAliveInterval 45", "-o", "ServerAliveCountMax 2", "-o", "ExitOnForwardFailure yes", "-N"] for m in cfg["mapping"]: command.append("-R") command.append("%d:localhost:%d" %(m["remote"], m["local"])) command.append("%s@%s" %(cfg["user"], cfg["host"])) subprocess.run(command)