Files
minara-autoreg/tunnel_keep.py
T

34 lines
1.2 KiB
Python

#!/usr/bin/env python3
"""Держит reverse-туннель прод:48200 -> локальный pc_agent.
Зачем: ssh -R, запущенный из background-bash, умирает вместе с оболочкой,
а pc_agent должен быть доступен new-api постоянно.
"""
import subprocess
import sys
import time
ARGS = ["ssh", "-N", "-R", "48200:127.0.0.1:48200", "-o", "BatchMode=yes",
"-o", "ExitOnForwardFailure=yes", "-o", "StrictHostKeyChecking=no",
"-o", "ServerAliveInterval=20", "-o", "ServerAliveCountMax=3",
"root@2.27.98.196"]
DETACH = 0x00000008 | 0x00000200
def main():
child = None
while True:
if child is None or child.poll() is not None:
if child is not None:
print("tunnel died rc=%s, restart" % child.returncode, flush=True)
child = subprocess.Popen(ARGS, creationflags=DETACH,
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
print("tunnel up pid=%d" % child.pid, flush=True)
time.sleep(15)
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
sys.exit(0)