You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

固定IP设备在线状态追踪工具开发需求问询

当然可以!完全能实现你想要的简易监控应用

我给你几个不同复杂度的方案,你可以根据自己的技术能力和实际需求来选择:

方案1:轻量级Python脚本(快速上手)

这个方案适合不想搞复杂界面的场景,能定时检测设备在线状态,状态变化时自动弹通知,几分钟就能跑起来。

  • 核心逻辑:用系统ping命令检测设备是否可达,定时循环检查,一旦设备在线/离线状态变化,就弹出系统通知。
  • 直接能用的代码:
import subprocess
import time
from plyer import notification

# 把你的设备信息填这里,格式是(设备名称, 固定IP)
DEVICES = [
    ("设备A", "192.168.1.10"),
    ("设备B", "192.168.1.11"),
    ("设备C", "192.168.1.12")
]
CHECK_INTERVAL = 10  # 每10秒检测一次,可调整
last_status = {device[0]: None for device in DEVICES}

def is_online(ip):
    # 适配Windows和Linux/macOS的ping命令参数
    param = '-n' if subprocess.os.name == 'nt' else '-c'
    command = ['ping', param, '1', '-W', '2', ip]
    # 执行ping命令,返回0代表能通
    return subprocess.call(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE) == 0

while True:
    for name, ip in DEVICES:
        current_status = is_online(ip)
        # 只有状态变化时才发通知,避免刷屏
        if current_status != last_status[name]:
            status_text = "在线" if current_status else "离线"
            notification.notify(
                title=f"设备状态更新",
                message=f"{name} ({ip}) 已{status_text}",
                timeout=5
            )
            last_status[name] = current_status
    time.sleep(CHECK_INTERVAL)
  • 准备工作:安装依赖pip install plyer,然后直接运行脚本就行。
  • 优缺点:好处是代码简单、零额外配置;缺点是没有可视化界面,只能靠通知了解状态。

方案2:简易Web监控页面(带可视化界面)

如果需要在同一局域网内的任意设备(手机、电脑)都能查看状态,这个方案很合适,用Flask搭个轻量Web服务,页面实时刷新展示所有设备状态。

后端代码(保存为app.py)

from flask import Flask, render_template
import subprocess
import threading
import time

app = Flask(__name__)
# 同样填你的设备信息
DEVICES = [
    ("设备A", "192.168.1.10"),
    ("设备B", "192.168.1.11"),
    ("设备C", "192.168.1.12")
]
device_status = {name: False for name, _ in DEVICES}

def update_status_loop():
    while True:
        for name, ip in DEVICES:
            param = '-n' if subprocess.os.name == 'nt' else '-c'
            command = ['ping', param, '1', '-W', '2', ip]
            device_status[name] = subprocess.call(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE) == 0
        time.sleep(10)

# 后台线程定时更新状态
threading.Thread(target=update_status_loop, daemon=True).start()

@app.route('/')
def index():
    return render_template('index.html', devices=DEVICES, status=device_status)

if __name__ == '__main__':
    # 允许局域网内其他设备访问
    app.run(host='0.0.0.0', port=5000, debug=False)

前端页面(新建templates文件夹,里面放index.html)

<!DOCTYPE html>
<html>
<head>
    <title>设备在线状态监控</title>
    <style>
        .device-card { margin: 15px 0; padding: 12px; border-radius: 8px; border: 1px solid #eee; }
        .online { color: #2ecc71; font-weight: bold; }
        .offline { color: #e74c3c; font-weight: bold; }
        h1 { text-align: center; }
    </style>
    <script>
        // 每10秒自动刷新页面,获取最新状态
        setInterval(() => window.location.reload(), 10000);
    </script>
</head>
<body>
    <h1>设备在线状态监控</h1>
    <div style="max-width: 600px; margin: 0 auto;">
        {% for name, ip in devices %}
            <div class="device-card">
                <span>{{ name }}</span>
                <span style="margin-left: 10px; color: #666;">({{ ip }})</span>
                <span style="float: right;" class="{{ 'online' if status[name] else 'offline' }}">
                    {{ '在线' if status[name] else '离线' }}
                </span>
            </div>
        {% endfor %}
    </div>
</body>
</html>
  • 准备工作:安装依赖pip install flask,运行python app.py,然后在浏览器输入http://你的电脑IP:5000就能查看了。
  • 优缺点:好处是跨设备访问、界面直观;缺点是需要一直运行Web服务,依赖浏览器查看。

方案3:桌面GUI小工具(独立应用)

如果想要一个不用开浏览器的独立桌面应用,可以用Tkinter(Python自带的GUI库)做个简单的窗口,实时显示状态,状态变化时还能弹窗提醒。

  • 代码示例:
import tkinter as tk
from tkinter import ttk
import subprocess
import threading
import time

class DeviceMonitor:
    def __init__(self, root):
        self.root = root
        self.root.title("设备在线监控")
        self.devices = [
            ("设备A", "192.168.1.10"),
            ("设备B", "192.168.1.11"),
            ("设备C", "192.168.1.12")
        ]
        self.status_labels = {}
        self._setup_ui()
        self._start_monitoring()

    def _setup_ui(self):
        # 逐个创建设备状态显示行
        for row, (name, ip) in enumerate(self.devices):
            frame = ttk.Frame(self.root, padding="8")
            frame.grid(row=row, column=0, sticky="we", padx=10, pady=5)
            
            ttk.Label(frame, text=f"{name} ({ip})", width=25).grid(row=0, column=0)
            status_label = ttk.Label(frame, text="离线", foreground="red")
            status_label.grid(row=0, column=1)
            self.status_labels[name] = status_label

    def _is_online(self, ip):
        param = '-n' if subprocess.os.name == 'nt' else '-c'
        command = ['ping', param, '1', '-W', '2', ip]
        return subprocess.call(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE) == 0

    def _monitor_loop(self):
        while True:
            for name, ip in self.devices:
                online = self._is_online(ip)
                new_status = "在线" if online else "离线"
                new_color = "green" if online else "red"
                
                # 状态变化时更新标签并弹窗提醒
                if self.status_labels[name].cget("text") != new_status:
                    self.status_labels[name].config(text=new_status, foreground=new_color)
                    self.root.deiconify()  # 把窗口调到前台
                    self.root.bell()  # 播放提示音
            time.sleep(10)

    def _start_monitoring(self):
        # 后台线程运行监控逻辑,不卡主界面
        monitor_thread = threading.Thread(target=self._monitor_loop, daemon=True)
        monitor_thread.start()

if __name__ == "__main__":
    root = tk.Tk()
    app = DeviceMonitor(root)
    root.mainloop()
  • 准备工作:Python自带Tkinter,直接运行就行;如果要发给别人用,可以用pyinstaller打包成exe/可执行文件:pyinstaller -w -F 你的脚本名.py。
  • 优缺点:好处是独立应用、界面直观;缺点是需要打包才能给其他设备用。

额外小技巧

  • 如果设备禁用了ping,可以改用端口检测(比如设备开放了80、22等端口),替换_is_online函数:
import socket
def _is_online(self, ip, port=80):
    try:
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        sock.settimeout(2)
        result = sock.connect_ex((ip, port))
        sock.close()
        return result == 0
    except Exception:
        return False
  • 通知扩展:除了系统通知,还可以加邮件、短信提醒(比如用SMTP发邮件,或者调用短信服务商的API)。
  • 长期运行:如果要让脚本24小时跑,可以做成系统服务(Windows用任务计划,Linux用systemd),开机自动启动。

内容的提问来源于stack exchange,提问作者JK V

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 03:41:30