import os
import sys


def command_ssh_tunnel(ports: list, target: str) -> None:
    """通过 SSH 建立本地端口转发隧道"""
    forward_args = []
    for p in ports:
        forward_args += ["-L", f"{p}:127.0.0.1:{p}"]

    cmd = ["ssh", "-N"] + forward_args + [target]
    print(f"[tunnel] {' '.join(cmd)}")
    print("Ctrl+C 退出")
    try:
        os.execvp("ssh", cmd)
    except FileNotFoundError:
        print("错误: 找不到 ssh 命令", file=sys.stderr)
        sys.exit(2)
