将令牌和 url 换为自己的,然后 run 就可以了!

import requests
import os

# 声明一个session连接
session = requests.session()
session.cookies["sess_key"] = "YourSession"

# 定制request-headers部分
urls = "YourIP:YourPort/Action/call"
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36"
}

# 获取在线设备列表和IP值
online_request = {"func_name":"monitor_lanip","action":"show","param":{"TYPE":"data,total","ORDER_BY":"ip_addr_int","orderType":"IP","limit":"0,100","ORDER":""}}
online_response = session.post(url=urls, headers=headers, json=online_request)
online_json = online_response.json()
# 格式化json字符串
print("当前在线的设备数为 {} 台设备,列表如下:".format(online_json["Data"]["total"]))
online_inner = online_json["Data"]["data"]
# 格式化输出在线设备开始
s = 1
for i in online_inner:
m = i["mac"]
n = i["ip_addr"]
j = i["comment"]
str = "第{}台设备,IP地址为{},描述为{},MAC地址为{};".format(s, n, j, m)
s += 1
print(str)
# 格式化输出在线设备结束

# 获取在线设备端口
online_port0 = {"func_name":"dnat","action":"show","param":{"TYPE":"total,data","limit":"0,100","ORDER_BY":"","ORDER":""}}
online_port1 = session.post(url=urls, headers=headers, json=online_port0)
online_port = online_port1.json()["Data"]["data"]

# 选择需要开放端口的设备
k = True
while k:
choose_inner = int(input("请选择需要开放端口的设备:\n")) - 1
inner = online_inner[choose_inner]
nl = inner["ip_addr"]
jl = inner["comment"]
print("您选择了 {} ,它的IP地址为 {}".format(jl, nl))

choose_true = input("确定是上面的设备吗?确定请输入1,其他任意键重新选择\n")
try:
if choose_true == "1":
k = False
else:
pass
except:
pass

def edit_port(nl, online_port):
# 修改已有的端口 edit操作

for im in online_port:
if im["lan_addr"] == nl:
print("当前设备开放的端口有:")
print("内网端口部分:{}".format(im["lan_port"]))
print("外网端口部分:{}".format(im["wan_port"]))
port_id = im["id"]
lan_addr_int = im["lan_addr_int"]
port_comment = im["comment"]
lan_port = im["lan_port"]
wan_port = im["lan_port"]
p1 = input("请选择添加[输入1]或者删减[输入2]端口\n")
if p1 == "1":
nai_port = "," + input("请输入要添加的内网端口号\n")
wei_port = "," + input("请输入对应的外网端口号\n")
lan_port += nai_port
wan_port += wei_port
elif p1 == "2":
ln = lan_port.split(",")
lw = wan_port.split(",")
np = input("请输入要删除的内网端口号\n")
wp = input("请输入对应的外网端口号\n")
ln.remove(np)
lw.remove(wp)
lan_port = ",".join(ln)
wan_port = ",".join(lw)
param = {
"comment": port_comment,
"enabled": "yes",
"id": port_id,
"interface": "wan1",
"lan_addr": nl,
"lan_addr_int": lan_addr_int,
"lan_port": lan_port,
"protocol": "tcp",
"wan_port": wan_port,
}

data = {
"action": "edit",
"func_name": "dnat",
"param": param,
}
return data

def del_port():
print("功能撰写中")
pass

def add_port():
# 放行新的设备端口 add操作
ml = input("1. 请输入端口的描述!\n")
in_port = input("2. 请输入内网端口号!\n")
out_port = input("3. 请输入外网端口号!\n")
data = {
"func_name": "dnat",
"action": "add",
"param": {
"comment": ml,
"enabled": "yes",
"interface": "wan1",
"lan_addr": nl,
"lan_port": in_port,
"protocol": "tcp+udp",
"wan_port": out_port,
}
}
return data

# 选择操作函数
choose = input("请选择操作:1-修改/添加端口, 2-删除设备所有端口, 3-给新设备部署端口\n")
if choose == "1":
print("您将修改/添加已有设备端口")
data = edit_port(nl, online_port)
elif choose == "2":
print("您将删除此设备的所有端口")
del_port()
elif choose == "3":
print("您正在部署新的设备端口!请先确保目前端口列表无此设备!")
data = add_port()
# 定制request-json部分

print("尝试发送数据至服务端,请稍候!")
request = session.post(url=urls ,headers=headers, json=data)
print("返回的状态码为:{},脚本执行完毕,请前往客户机验证".format(request))

os.system("pause")