49 lines
1 KiB
Python
Executable file
49 lines
1 KiB
Python
Executable file
#! /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()))
|
|
|
|
def safe_get(d, k, alt):
|
|
"""
|
|
Returns d[k] or alt
|
|
"""
|
|
|
|
if k in d:
|
|
return d[k]
|
|
else:
|
|
return alt
|
|
|
|
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", "-o", "ClearAllForwardings=yes", "-N", "-p", str(safe_get(cfg, "port", 22))]
|
|
|
|
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)
|