from jinja2 import Environment, FileSystemLoader # 1. 定义你的客制化参数字典 config_vars = { # 全局变量 "gateway_lan_ip": "192.168.10.1", "gateway_lan_cidr": "192.168.10.0/24", "wan_interface": "eth0", # 网关主机外网网卡 "lan_interface": "eth1", # 网关主机内网网卡 "client_lan_interface": "eth0", # 内网主机网卡 # 网关主机独有变量 "dhcp_start_ip": "192.168.10.100", "dhcp_end_ip": "192.168.10.200", "dhcp_lease_time": "7200", "remote_ssh_user": "your_remote_user", "remote_ssh_ip": "your_remote_server_ip", # !!! 替换为你的远程SSH服务器IP "remote_ssh_port": "22", "local_socks5_port": "1080", "redsocks_port": "12345", "domestic_dns": "223.5.5.5,114.114.114.114", "foreign_dns": "8.8.8.8,1.1.1.1", "gfwlist2new_repo": "https://github.com/louisabrahams/gfwlist2new.git", "gfwlist2new_dir": "/opt/gfwlist2new", } # 2. 设置 Jinja2 环境 # 假设你的 .j2 模板文件与这个 Python 脚本在同一目录下 env = Environment(loader=FileSystemLoader('.')) # 3. 渲染 gateway_config.sh gateway_template = env.get_template('gateway_config.sh.j2') rendered_gateway_script = gateway_template.render(config_vars) with open('gateway_config.sh', 'w') as f: f.write(rendered_gateway_script) print("Generated gateway_config.sh") # 4. 渲染 client_config.sh client_template = env.get_template('client_config.sh.j2') rendered_client_script = client_template.render(config_vars) # 客户端脚本也需要一些全局变量 with open('client_config.sh', 'w') as f: f.write(rendered_client_script) print("Generated client_config.sh") print("\n!!! 请务必检查生成的 .sh 文件,特别是 SSH 代理和网卡名称等配置。") print("在执行前,给它们添加可执行权限:chmod +x gateway_config.sh client_config.sh")