{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "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": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "broker = '100.64.0.6'\n",
    "# broker = '192.168.0.111'\n",
    "ip = \"http://100.64.0.6:9008\"\n",
    "port = 1883\n",
    "topic = \"/python/mqtt\"\n",
    "client_id = f'python-mqtt-demo'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 3,
     "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": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<paho.mqtt.client.MQTTMessageInfo at 0x201f147c7c0>"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "client.publish(\"demo/python\", \"Hello World!\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "token = post(f\"{ip}/login\",json={\"username\":\"admin\",\"password\":\"admin123\"}).json()[\"token\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "headers = {\"Authorization\":f\"Bearer {token}\"}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "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": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "macs =[]\n",
    "for item in records:\n",
    "    macs.append(item.get(\"machineCode\", \"\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "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": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(onlines)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(offlines)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'machineCode': '20254c7802bd',\n",
       "  'displayName': '七步洗手法',\n",
       "  'ip': '192.168.8.10',\n",
       "  'groupId': 1,\n",
       "  'contentId': 86,\n",
       "  'updateTime': '2025-10-28T09:15:36',\n",
       "  'version': '1.0.50',\n",
       "  'deviceName': '食堂门口',\n",
       "  'groupName': '食堂',\n",
       "  'createTime': '2025-10-26T08:58:36',\n",
       "  'teamId': 19,\n",
       "  'online': 0,\n",
       "  'location': '食堂门口',\n",
       "  'id': 'cade19b0-1f2d-4c9c-8289-c7741d56bb0b'},\n",
       " {'machineCode': '20254c9856bf',\n",
       "  'displayName': '七步洗手法',\n",
       "  'ip': '192.168.8.3',\n",
       "  'groupId': 30,\n",
       "  'contentId': 86,\n",
       "  'updateTime': '2025-10-28T10:34:34',\n",
       "  'version': '1.0.50',\n",
       "  'deviceName': '3楼一体机',\n",
       "  'groupName': '教学楼',\n",
       "  'createTime': '2025-10-12T17:58:33',\n",
       "  'teamId': 19,\n",
       "  'online': 1,\n",
       "  'location': '3楼',\n",
       "  'id': '9b876939-9258-4c87-a2f9-d4a61459e8ab'},\n",
       " {'machineCode': '20254c823347',\n",
       "  'displayName': '七步洗手法',\n",
       "  'ip': '192.168.8.2',\n",
       "  'groupId': 30,\n",
       "  'contentId': 86,\n",
       "  'updateTime': '2025-10-28T10:22:24',\n",
       "  'version': '1.0.50',\n",
       "  'deviceName': '2楼一体机',\n",
       "  'groupName': '教学楼',\n",
       "  'createTime': '2025-10-12T17:51:30',\n",
       "  'teamId': 19,\n",
       "  'online': 0,\n",
       "  'location': '2楼',\n",
       "  'id': '1c25761c-5917-4c2b-af42-f3cc258e6957'}]"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "records"
   ]
  },
  {
   "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": 45,
   "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/watchDog.exe\",\"type\":\"down\"}\n",
    "    client.publish(topic,json.dumps(message,ensure_ascii=False))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<paho.mqtt.client.MQTTMessageInfo at 0x2015292bfb0>"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "topic = f\"cmd/publicizeTelevision/000c290a0fcb/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": 20,
   "metadata": {},
   "outputs": [],
   "source": [
    "for item in macs:\n",
    "    topic = f\"cmd/publicizeTelevision/{item}/cmd\"\n",
    "    message = '''systeminfo'''\n",
    "    client.publish(topic,message)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# cmd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "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": 90,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "for item in macs:\n",
    "    topic = f\"cmd/televisionDog/{item.upper()}/common\"\n",
    "    message = {\n",
    " \"common\":[\"tasklist | findstr \\\"校宣机程序\\\"\"]\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": "markdown",
   "metadata": {},
   "source": [
    "#调整轮播间隔"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "for item in macs:\n",
    "    topic = f\"cmd/publicizeTelevision/{item}/setting\"\n",
    "    message = {\n",
    "        \"data\":60000\n",
    "    }\n",
    "\n",
    "    client.publish(topic,json.dumps(message, ensure_ascii=False))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['00e0220fa0e4', '20254c7802bd', '20254c9856bf', '20254c823347']"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "macs"
   ]
  }
 ],
 "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
}
