Files
python----/智慧校园/校宣机mq.ipynb
T
admin aac9f5934d feat: 添加多个功能模块和工具脚本
- 新增websocket客户端和服务端实现
- 添加图片压缩工具和快速压缩脚本
- 实现学生信息处理相关API
- 添加MQTT客户端和消息处理功能
- 更新.gitignore忽略更多文件类型
- 添加数据库操作工具和示例
- 实现多个测试脚本和工具类
2026-04-13 14:47:50 +08:00

746 lines
20 KiB
Plaintext

{
"cells": [
{
"cell_type": "code",
"execution_count": 59,
"metadata": {},
"outputs": [],
"source": [
"from paho.mqtt import client as mqtt\n",
"import json\n",
"import time\n",
"from requests import get,post,delete"
]
},
{
"cell_type": "code",
"execution_count": 60,
"metadata": {},
"outputs": [],
"source": [
"broker = '100.68.18.111'\n",
"# broker = '192.168.0.111'\n",
"ip = \"http://100.68.18.111:9008\"\n",
"port = 1883\n",
"topic = \"/python/mqtt\"\n",
"client_id = f'python-mqtt-demo'"
]
},
{
"cell_type": "code",
"execution_count": 61,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0"
]
},
"execution_count": 61,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# MQTT客户端的回调函数\n",
"def on_connect(client, userdata, flags, rc):\n",
" print(\"Connected with result code \"+str(rc))\n",
" # 连接成功后订阅主题\n",
" # client.subscribe(\"#\")\n",
"\n",
"def on_message(client, userdata, msg):\n",
" print(msg.topic+\" \"+str(msg.payload))\n",
"\n",
"# 创建MQTT客户端实例\n",
"client = mqtt.Client(client_id=client_id)\n",
"\n",
"# 绑定回调函数\n",
"client.on_connect = on_connect\n",
"client.on_message = on_message\n",
"\n",
"# 连接到MQTT代理\n",
"client.connect(broker, port, 60)"
]
},
{
"cell_type": "code",
"execution_count": 62,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<paho.mqtt.client.MQTTMessageInfo at 0x250160b7bf0>"
]
},
"execution_count": 62,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"client.publish(\"demo/python\", \"Hello World!\")"
]
},
{
"cell_type": "code",
"execution_count": 63,
"metadata": {},
"outputs": [],
"source": [
"token = post(f\"{ip}/login\",json={\"username\":\"admin\",\"password\":\"admin123\"}).json()[\"token\"]"
]
},
{
"cell_type": "code",
"execution_count": 64,
"metadata": {},
"outputs": [],
"source": [
"headers = {\"Authorization\":f\"Bearer {token}\"}"
]
},
{
"cell_type": "code",
"execution_count": 72,
"metadata": {},
"outputs": [
{
"ename": "KeyError",
"evalue": "'data'",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mKeyError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[1;32mIn[72], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m records \u001b[38;5;241m=\u001b[39m get(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mip\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m/school/devices/listPage?temaId=19&groupId=0&pageNum=1&pageSize=40\u001b[39m\u001b[38;5;124m\"\u001b[39m,headers\u001b[38;5;241m=\u001b[39mheaders)\u001b[38;5;241m.\u001b[39mjson()[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mdata\u001b[39m\u001b[38;5;124m\"\u001b[39m][\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mrecords\u001b[39m\u001b[38;5;124m\"\u001b[39m]\n",
"\u001b[1;31mKeyError\u001b[0m: 'data'"
]
}
],
"source": [
"records = get(f\"{ip}/school/devices/listPage?temaId=19&groupId=0&pageNum=1&pageSize=40\",headers=headers).json()[\"data\"][\"records\"]"
]
},
{
"cell_type": "code",
"execution_count": 73,
"metadata": {},
"outputs": [],
"source": [
"macs =[]\n",
"for item in records:\n",
" macs.append(item.get(\"machineCode\", \"\"))"
]
},
{
"cell_type": "code",
"execution_count": 74,
"metadata": {},
"outputs": [],
"source": [
"#在线\n",
"onlines = []\n",
"offlines=[]\n",
"for item in records:\n",
" if item.get(\"online\",0)==1:\n",
" onlines.append(item['machineCode'])\n",
" else:\n",
" offlines.append(item['machineCode'])"
]
},
{
"cell_type": "code",
"execution_count": 75,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"27"
]
},
"execution_count": 75,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"len(onlines)"
]
},
{
"cell_type": "code",
"execution_count": 69,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"6"
]
},
"execution_count": 69,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"len(offlines)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 升级"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<paho.mqtt.client.MQTTMessageInfo at 0x1b5acdeda90>"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"topic = \"common/publicizeTelevision/00e066fd87b1/ota/upgrade\"\n",
"client.publish(topic,\"1\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"for item in macs:\n",
" topic = f\"common/publicizeTelevision/{item}/ota/upgrade\"\n",
" client.publish(topic,\"1\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 看门狗"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0 00e066fd898e\n",
"1 000000000001\n",
"2 00e0b413c78b\n",
"3 00e066fd86c5\n",
"4 000000000000\n",
"5 00e066fd8711\n",
"6 00e066fd8782\n",
"7 00e066fd87b1\n",
"8 00e066fd898d\n",
"9 00e066fd8710\n",
"10 00e066fd87ad\n",
"11 00e066fd890b\n",
"12 00e066fd882d\n",
"13 00e066fd61fc\n",
"14 00e066fd8927\n",
"15 00e066fd898c\n",
"16 00e066fd890c\n",
"17 00e066fd86c4\n",
"18 00e066fd8981\n",
"19 00e066fd87af\n",
"20 00e066fd891f\n",
"21 000ec6431230\n",
"22 00e066fd8988\n",
"23 00e066fd8832\n",
"24 00e066fd870d\n",
"25 00e066fd8990\n",
"26 00e066fd883a\n",
"27 00e066fd882f\n",
"28 00e066fd87b4\n",
"29 00e066fd8989\n",
"30 00e066fd8986\n",
"31 00e066fd8797\n",
"32 00e066fd898b\n",
"33 00e066fd8931\n"
]
}
],
"source": [
"for item,index in enumerate(macs):\n",
" print(item,index)\n",
" "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# netsh interface ipv4 set address name=\"以太网\" source=static address=192.168.0.110 mask=255.255.255.0 gateway=192.168.0.1"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'00E066FD8782'"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"'00e066fd8782'.upper()"
]
},
{
"cell_type": "code",
"execution_count": 81,
"metadata": {},
"outputs": [],
"source": [
"for index,item in enumerate(macs):\n",
" topic = f\"cmd/publicizeTelevision/{item}/dog\"\n",
" message = {\"name\":\"watchDog.exe\",\"url\":\"http://schoolserver.com:9008/common/download/app/watchDog.exe\",\"type\":\"down\"}\n",
" client.publish(topic,json.dumps(message,ensure_ascii=False))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# for index,item in enumerate(macs):\n",
"# time.sleep(1)\n",
"# topic = f\"cmd/publicizeTelevision/{item}/dog\"\n",
"# message = {\"name\":\"watchDog.exe\",\"url\":\"http://schoolserver.com:9008/common/download/app/HEU.exe\",\"type\":\"down\"}\n",
"# client.publish(topic,json.dumps(message,ensure_ascii=False))"
]
},
{
"cell_type": "code",
"execution_count": 76,
"metadata": {},
"outputs": [],
"source": [
"for index,item in enumerate(macs):\n",
" time.sleep(1)\n",
" topic = f\"cmd/publicizeTelevision/{item}/dog\"\n",
" message = {\"type\":\"stop\"}\n",
" client.publish(topic,json.dumps(message,ensure_ascii=False))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<paho.mqtt.client.MQTTMessageInfo at 0x250130fca90>"
]
},
"execution_count": 79,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# topic = f\"cmd/publicizeTelevision/00e066fd8989/dog\"\n",
"# message = {\"name\":\"watchDog.exe\",\"url\":\"http://schoolserver.com:9008/common/download/app/watchDog.exe\",\"type\":\"down\"}\n",
"# client.publish(topic,json.dumps(message,ensure_ascii=False))"
]
},
{
"cell_type": "code",
"execution_count": 77,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"<>:3: SyntaxWarning: invalid escape sequence '\\S'\n",
"<>:3: SyntaxWarning: invalid escape sequence '\\S'\n",
"C:\\Users\\Administrator\\AppData\\Local\\Temp\\ipykernel_8436\\3533498662.py:3: SyntaxWarning: invalid escape sequence '\\S'\n",
" message = '''reg query \"HKLM\\SOFTWARE\\WOW6432Node\\Microsoft\\Windows\\CurrentVersion\\Run\"'''\n"
]
}
],
"source": [
"for item in macs:\n",
" topic = f\"cmd/publicizeTelevision/{item}/cmd\"\n",
" message = '''reg query \"HKLM\\SOFTWARE\\WOW6432Node\\Microsoft\\Windows\\CurrentVersion\\Run\"'''\n",
"\n",
" client.publish(topic,message)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [],
"source": [
"for item in macs:\n",
" topic = f\"cmd/publicizeTelevision/{item}/cmd\"\n",
" message = '''systeminfo'''\n",
" client.publish(topic,message)"
]
},
{
"cell_type": "code",
"execution_count": 49,
"metadata": {},
"outputs": [],
"source": [
"for item in macs:\n",
" topic = f\"cmd/publicizeTelevision/{item}/cmd\"\n",
" message = \"shutdown /r\"\n",
" client.publish(topic,message)"
]
},
{
"cell_type": "code",
"execution_count": 48,
"metadata": {},
"outputs": [],
"source": [
"for item in macs:\n",
" topic = f\"cmd/publicizeTelevision/{item}/cmd\"\n",
" message = '''taskkill /f /t /im watchDog.exe'''\n",
" client.publish(topic,message)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# cmd"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [],
"source": [
"for item in macs:\n",
" topic = f\"cmd/televisionDog/{item.upper()}/ask\"\n",
" client.publish(topic,\"1\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'00E066FD8832'"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"'00e066fd8832'.upper()"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<paho.mqtt.client.MQTTMessageInfo at 0x20d4b920e00>"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"topic = f\"cmd/televisionDog/00E066FD8832/ask\"\n",
"client.publish(topic,\"1\")"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<paho.mqtt.client.MQTTMessageInfo at 0x21087171c60>"
]
},
"execution_count": 24,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# netsh interface ipv4 set address name=\"以太网\" source=static address=13.1.1.125 mask=255.255.255.0 gateway=13.1.1.1\n",
"# message = {\n",
"# \"common\":['netsh interface ipv4 set address name=\"以太网\" source=static address=13.1.1.119 mask=255.255.255.0 gateway=13.1.1.1','ping 13.1.1.1','shutdown /r /t 00']\n",
"# }\n",
"message = {\n",
" \"common\":['netsh interface ipv4 set address name=\"以太网\" source=static address=13.1.1.115 mask=255.255.255.0 gateway=13.1.1.1 & ping 13.1.1.1 & shutdown /r /t 00']\n",
"}\n",
"client.publish(\"cmd/televisionDog/00E066FD882D/common\",json.dumps(message, ensure_ascii=False))"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [],
"source": [
"for item in macs:\n",
" topic = f\"cmd/televisionDog/{item.upper()}/common\"\n",
" message = {\n",
" \"common\":['''slmgr /skms 10.19.64.158 & slmgr /ato''']\n",
"}\n",
" client.publish(topic,json.dumps(message, ensure_ascii=False))"
]
},
{
"cell_type": "code",
"execution_count": 85,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"<>:4: SyntaxWarning: invalid escape sequence '\\S'\n",
"<>:4: SyntaxWarning: invalid escape sequence '\\S'\n",
"C:\\Users\\Administrator\\AppData\\Local\\Temp\\ipykernel_8436\\2368358595.py:4: SyntaxWarning: invalid escape sequence '\\S'\n",
" \"common\":['''reg query \"HKLM\\SOFTWARE\\WOW6432Node\\Microsoft\\Windows\\CurrentVersion\\Run\"''']\n"
]
}
],
"source": [
"for item in macs:\n",
" topic = f\"cmd/televisionDog/{item.upper()}/common\"\n",
" message = {\n",
" \"common\":['''reg query \"HKLM\\SOFTWARE\\WOW6432Node\\Microsoft\\Windows\\CurrentVersion\\Run\"''']\n",
"}\n",
" client.publish(topic,json.dumps(message, ensure_ascii=False))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"\n",
"for item in macs:\n",
" topic = f\"cmd/televisionDog/{item.upper()}/common\"\n",
" message = {\n",
" \"common\":[\"shutdown /r \"]\n",
"}\n",
" client.publish(topic,json.dumps(message, ensure_ascii=False))"
]
},
{
"cell_type": "code",
"execution_count": 80,
"metadata": {},
"outputs": [],
"source": [
"for item in macs:\n",
" topic = f\"cmd/televisionDog/{item.upper()}/control\"\n",
" message = {\n",
" \"type\":\"capture\",\n",
" \"id\":\"1\",\n",
"}\n",
" client.publish(topic,json.dumps(message, ensure_ascii=False))"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<paho.mqtt.client.MQTTMessageInfo at 0x201c062b510>"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"message = {\n",
" \"common\":[\"ping 13.1.1.124\"]\n",
"}\n",
"\n",
"client.publish(\"cmd/televisionDog/00E066FD898E/common\",json.dumps(message, ensure_ascii=False))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"macs =['00e066fd883a']"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [],
"source": [
"data = {\"macs\":[\"00e0b413c78b\",\"00e066fd86c5\",\"00e066fd8711\",\"00e066fd8782\",\"00e066fd87b1\",\"00e066fd898d\",\"00e066fd8710\",\"00e066fd87ad\",\"00e066fd890b\",\"00e066fd882d\",\"00e066fd61fc\",\"00e066fd8927\",\"00e066fd898c\",\"00e066fd890c\",\"00e066fd86c4\",\"00e066fd8981\",\"00e066fd87af\",\"00e066fd891f\",\"000ec6431230\",\"00e066fd8988\",\"00e066fd8832\",\"00e066fd870d\",\"00e066fd8990\",\"00e066fd883a\",\"00e066fd882f\",\"00e066fd87b4\",\"00e066fd8989\",\"00e066fd8986\",\"00e066fd8797\",\"00e066fd898b\",\"00e066fd8931\"],\"ips\":[\"13.1.1.120\",\"13.1.1.119\",\"13.1.1.125\",\"13.1.1.124\",\"13.1.1.126\",\"13.1.1.131\",\"13.1.1.127\",\"13.1.1.129\",\"13.1.1.117\",\"13.1.1.180\",\"13.1.1.114\",\"13.1.1.116\",\"13.1.1.118\",\"13.1.1.104\",\"13.1.1.113\",\"13.1.1.139\",\"13.1.1.136\",\"13.1.1.111\",\"13.1.1.102\",\"13.1.1.108\",\"13.1.1.110\",\"13.1.1.132\",\"13.1.1.130\",\"13.1.1.122\",\"13.1.1.123\",\"13.1.1.112\",\"13.1.1.107\",\"13.1.1.109\",\"13.1.1.115\",\"13.1.1.105\",\"13.1.1.135\"]}"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [],
"source": [
"tem = []\n",
"for index,item in enumerate(data[\"macs\"]):\n",
" tem.append({\"mac\":item,\"ip\":data[\"ips\"][index]})"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[{'mac': '00e0b413c78b', 'ip': '13.1.1.120'},\n",
" {'mac': '00e066fd86c5', 'ip': '13.1.1.119'},\n",
" {'mac': '00e066fd8711', 'ip': '13.1.1.125'},\n",
" {'mac': '00e066fd8782', 'ip': '13.1.1.124'},\n",
" {'mac': '00e066fd87b1', 'ip': '13.1.1.126'},\n",
" {'mac': '00e066fd898d', 'ip': '13.1.1.131'},\n",
" {'mac': '00e066fd8710', 'ip': '13.1.1.127'},\n",
" {'mac': '00e066fd87ad', 'ip': '13.1.1.129'},\n",
" {'mac': '00e066fd890b', 'ip': '13.1.1.117'},\n",
" {'mac': '00e066fd882d', 'ip': '13.1.1.180'},\n",
" {'mac': '00e066fd61fc', 'ip': '13.1.1.114'},\n",
" {'mac': '00e066fd8927', 'ip': '13.1.1.116'},\n",
" {'mac': '00e066fd898c', 'ip': '13.1.1.118'},\n",
" {'mac': '00e066fd890c', 'ip': '13.1.1.104'},\n",
" {'mac': '00e066fd86c4', 'ip': '13.1.1.113'},\n",
" {'mac': '00e066fd8981', 'ip': '13.1.1.139'},\n",
" {'mac': '00e066fd87af', 'ip': '13.1.1.136'},\n",
" {'mac': '00e066fd891f', 'ip': '13.1.1.111'},\n",
" {'mac': '000ec6431230', 'ip': '13.1.1.102'},\n",
" {'mac': '00e066fd8988', 'ip': '13.1.1.108'},\n",
" {'mac': '00e066fd8832', 'ip': '13.1.1.110'},\n",
" {'mac': '00e066fd870d', 'ip': '13.1.1.132'},\n",
" {'mac': '00e066fd8990', 'ip': '13.1.1.130'},\n",
" {'mac': '00e066fd883a', 'ip': '13.1.1.122'},\n",
" {'mac': '00e066fd882f', 'ip': '13.1.1.123'},\n",
" {'mac': '00e066fd87b4', 'ip': '13.1.1.112'},\n",
" {'mac': '00e066fd8989', 'ip': '13.1.1.107'},\n",
" {'mac': '00e066fd8986', 'ip': '13.1.1.109'},\n",
" {'mac': '00e066fd8797', 'ip': '13.1.1.115'},\n",
" {'mac': '00e066fd898b', 'ip': '13.1.1.105'},\n",
" {'mac': '00e066fd8931', 'ip': '13.1.1.135'}]"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"tem"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "base",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.4"
}
},
"nbformat": 4,
"nbformat_minor": 2
}