Method for acquiring online duration of equipment, electronic equipment and storage medium
By obtaining the current system uptime time value of the WiFi router and the time value difference of the target hanging device in the single linked list, combined with the switching module port status and MAC address list, the problem that the WiFi router cannot accurately obtain the online duration of the hanging device in bridge mode or relay mode is solved, real-time and accurate online duration acquisition is achieved.
Patent Information
- Application Number
- CN202510492939.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-04-18
- Publication Date
- 2025-07-18
AI Technical Summary
When the WiFi router is configured in bridge mode or relay mode, it is impossible to accurately obtain the online duration of the downhook device, because the existing technology cannot distinguish the information of the downhook device and the uphook device in the same network segment based on the ARP table, resulting in inaccurate acquisition.
By obtaining the current system uptime time value of the WiFi router and the time value difference of the target downhill device in the single-linked list, combining the switching module port status and MAC address list, a single-linked list is created and maintained to ensure that the downhill device MAC address is added and deleted in a timely manner, and the accuracy of online time is improved.
It realizes accurate acquisition of the online duration of the WiFi router's hanging device in bridge mode or relay mode, avoids the impact of ARP table aging time, improves the acquisition accuracy, is suitable for routing, bridge and relay modes, and has real-time and accuracy.
Smart Images

Figure CN120343600A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of communication technology, and in particular to a method for obtaining online time of a device, an electronic device and a storage medium. Background Art
[0002] WiFi routers generally have three working modes: routing mode, bridge mode, and relay mode. When the WiFi router is configured in routing mode, the DHCP service is enabled simultaneously, and the IP address, MAC address, and host name information of the downstream device can be obtained according to the dhcp.leases table. When the WiFi router is configured in bridge mode or relay mode, the DHCP service function is usually not enabled, and the downstream device obtains the IP address through the DHCP service of the upstream gateway device. Since the data of the downstream device is directly forwarded to the upstream gateway through the second-layer bridge of the WiFi router, it will not enter the network layer for processing. When the IP address of the router is in a different network segment from the LAN, the WiFi router cannot directly obtain the relevant information of the downstream device, and thus cannot accurately obtain the online time of the online device of the WiFi router.
[0003] In the prior art, the information of the downstream device is often obtained through the ARP table. However, in addition to the information of the downstream device, the ARP table also includes information of other devices upstream of the network segment, and the online time of the downstream device cannot be accurately obtained. Summary of the invention
[0004] In view of the above technical problems, the technical solution adopted by the present invention is: a method for obtaining the online time of a device, the method is used to obtain the online time of a device connected to a WIFI router, and the method comprises the following steps:
[0005] S100, obtaining the current system uptime value of the WIFI router as the first uptime value;
[0006] S200, obtaining a corresponding node of the MAC address of the target downstream device of the WIFI router in the single linked list and a time value stored in the corresponding node, and using the time value stored in the corresponding node as a second uptime time value;
[0007] S300, obtaining the online duration of the target device connected to the WIFI router, where the online duration is the difference between the first uptime time value and the second uptime time value;
[0008] Get a singly linked list by following the steps below:
[0009] S001, creating a singly linked list, the singly linked list consisting of a plurality of nodes, the nodes storing the initial downstream device MAC address of the WIFI router and the current system uptime time value corresponding to the initial downstream device MAC address;
[0010] S002. Obtain the list of MAC addresses of the offline attached devices of the WIFI router. The list of MAC addresses of the offline attached devices includes m MAC addresses of the offline attached devices.
[0011] S003. Query the i-th MAC address of the offline attached device in the singly linked list, where the value range of i is from 1 to m.
[0012] S004. If the singly linked list contains the MAC address of the offline attached device, execute S006.
[0013] S005. If the singly linked list does not contain the MAC address of the offline attached device, obtain the current system uptime value corresponding to the MAC address of the offline attached device, and add the MAC address of the offline attached device and the current system uptime corresponding to the MAC address of the offline attached device as a node to the singly linked list, and then execute S006.
[0014] S006. Let i = i + 1, and execute S003 until i = m.
[0015] According to another aspect of the present invention, there is provided a non-transitory computer-readable storage medium, in which at least one instruction or at least one program segment is stored, and the at least one instruction or the at least one program segment is loaded and executed by a processor to implement the foregoing method.
[0016] According to still another aspect of the present invention, there is provided an electronic device, including a processor and the foregoing non-transitory computer-readable storage medium.
[0017] The present invention has at least the following beneficial effects: In summary, obtain the current system uptime value of the WIFI router as the first uptime value, obtain the corresponding node of the target attached device MAC address of the WIFI router in the singly linked list and the time value stored in the corresponding node, and use the time value stored in the corresponding node as the second uptime value. Obtain the online duration of the target attached device of the WIFI router, create a singly linked list, obtain the list of MAC addresses of the online attached devices of the WIFI router, and query the i-th online attached device MAC address in the list of MAC addresses of the online attached devices in the singly linked list. If the singly linked list contains the MAC address of the online attached device, execute the next step; if the singly linked list does not contain the MAC address of the online attached device, obtain the current system uptime value corresponding to the MAC address of the online attached device, and add the MAC address of the online attached device and the current system uptime corresponding to the MAC address of the online attached device as a node to the singly linked list, and execute the next step. By maintaining the singly linked list, the MAC addresses of the online attached devices can be added to the singly linked list in a timely manner, and the stored system uptime values in the singly linked list are as accurate as possible, thereby improving the accuracy of obtaining the online duration of the attached devices of the WIFI router. BRIEF DESCRIPTION OF THE DRAWINGS
[0018] In order to more clearly illustrate the technical solutions in the embodiments of the present invention, the following will briefly introduce the accompanying drawings required for the description of the embodiments. Obviously, the accompanying drawings in the following description are only some embodiments of the present invention. For those of ordinary skill in the art, without creative efforts, other drawings can be obtained based on these drawings.
[0019] Figure 1 It is a flowchart of a method for obtaining the online duration of a device provided by an embodiment of the present invention. DETAILED DESCRIPTION OF THE EMBODIMENTS
[0020] The following will clearly and completely describe the technical solutions in the embodiments of the present invention with reference to the accompanying drawings in the embodiments of the present invention. Obviously, the described embodiments are only some embodiments of the present invention, rather than all embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those of ordinary skill in the art without creative efforts belong to the scope of protection of the present invention.
[0021] An embodiment of the present invention provides a method for obtaining the online duration of a device, as Figure 1 shown. The method is used to obtain the online duration of the attached devices of the WIFI router, and the method includes the following steps:
[0022] S100, Obtain the current system uptime value of the WIFI router as the first uptime value.
[0023] Specifically, the WIFI router includes a main control unit, a WiFi module, and a switching module.
[0024] Specifically, the current system uptime value is the length of time that the WIFI router has been running continuously since its most recent startup, indicating the duration of continuous operation of the WIFI router without restarting or shutting down.
[0025] S200, Obtain the corresponding node of the target attached device MAC address of the WIFI router in the singly linked list and the time value stored in the corresponding node, and use the time value stored in the corresponding node as the second uptime value.
[0026] S300, Obtain the online duration of the target attached device of the WIFI router, where the online duration is the difference between the first uptime value and the second uptime value.
[0027] Among them, the singly linked list is obtained through the following steps:
[0028] S001, Create a singly linked list, which is composed of several nodes, and each node stores the initial attached device MAC address of the WIFI router and the storage system uptime time value corresponding to the initial attached device MAC address.
[0029] Specifically, the storage system uptime time value corresponding to the initial attached device MAC address is the system uptime time value when the initial attached device MAC address is stored in the node.
[0030] S002, Obtain the list of online attached device MAC addresses of the WIFI router, and the list of online attached device MAC addresses includes m online attached device MAC addresses.
[0031] S003, Query the i-th online attached device MAC address in the list of online attached device MAC addresses in the singly linked list, where the value range of i is from 1 to m.
[0032] S004, If the singly linked list contains the online attached device MAC address, execute S006.
[0033] S005, if the single linked list does not contain the MAC address of the offline attached device, obtain the current system uptime value corresponding to the MAC address of the offline attached device, and add the MAC address of the offline attached device and the current system uptime corresponding to the MAC address of the offline attached device as a node to the single linked list, then execute S006. It can be understood that when the MAC address of an offline attached device is not stored in the single linked list, the MAC address of the offline attached device and the current system uptime value are added as a node to the single linked list in a timely manner.
[0034] S006, i = i + 1, execute S003 until i = m.
[0035] Specifically, create a single linked list and maintain the single linked list through the steps of S002 - S006, so that all MAC addresses of offline attached devices are stored in the single linked list.
[0036] In summary, obtain the current system uptime value of the WIFI router as the first uptime value, obtain the corresponding node of the target attached device MAC address of the WIFI router in the single linked list and the time value stored in the corresponding node, and use the time value stored in the corresponding node as the second uptime value, obtain the online duration of the target attached device of the WIFI router, create a single linked list, obtain the list of MAC addresses of offline attached devices of the WIFI router, query the i-th MAC address of the offline attached device in the list of MAC addresses of offline attached devices in the single linked list. If the single linked list contains the MAC address of the offline attached device, execute the next one; if the single linked list does not contain the MAC address of the offline attached device, obtain the current system uptime value corresponding to the MAC address of the offline attached device, and add the MAC address of the offline attached device and the current system uptime corresponding to the MAC address of the offline attached device as a node to the single linked list, and execute the next one; through the way of maintaining the single linked list, the MAC addresses of offline attached devices can be added to the single linked list in a timely manner, and the stored system uptime values in the single linked list are as accurate as possible, thereby improving the accuracy of obtaining the online duration of the attached devices of the WIFI router.
[0037] Further, after S006, it also includes:
[0038] S007, traverse the single linked list, extract the MAC address of each node, so as to obtain a list of node MAC addresses, and the list of node MAC addresses includes n node MAC addresses.
[0039] S008, query the j-th node MAC address in the list of node MAC addresses in the list of MAC addresses of offline attached devices, and the value range of j is from 1 to n.
[0040] S009, if the MAC address of the j-th node exists in the list of MAC addresses of the subordinate devices, execute S0011.
[0041] S0010, if the MAC address of the j-th node does not exist in the list of MAC addresses of the subordinate devices, delete the node corresponding to this node's MAC address in the singly linked list, and execute S0011. When the MAC address of a node does not exist in the list of MAC addresses of the subordinate devices, it can be understood that the subordinate device corresponding to this node's MAC address is no longer a subordinate device for various reasons. Therefore, delete the node corresponding to this node's MAC address in the singly linked list.
[0042] S0011, j = j + 1, execute S062 until j = n.
[0043] Specifically, after S0011, it further includes: after an interval of a preset time period, execute S002 - S0011. Preferably, the preset time period is 5 seconds. It can be understood that after creating a singly linked list, based on S002 - S0011, the singly linked list is maintained. After an interval of the preset time period, S002 - S0011 is repeatedly executed. As time changes, the MAC addresses of the offline subordinate devices may change, and the singly linked list is continuously maintained.
[0044] In summary, traverse the singly linked list, extract the MAC address of each node, so as to obtain the list of node MAC addresses. For the MAC address of the j-th node in the list of node MAC addresses, query it in the list of online subordinate device MAC addresses. If the MAC address of the j-th node exists in the list of online subordinate device MAC addresses, execute the MAC address of the next node. If the MAC address of the j-th node does not exist in the list of online subordinate device MAC addresses, delete the node corresponding to this node's MAC address in the singly linked list, and execute the MAC address of the next node; through traversing the node MAC addresses, the present invention timely deletes the node MAC addresses that do not exist in the list of online subordinate device MAC addresses to maintain the singly linked list, thereby improving the accuracy of obtaining the online duration of the subordinate devices of the WIFI router.
[0045] Specifically, S002 further includes obtaining the list of online subordinate device MAC addresses of the WIFI router through the following steps:
[0046] S021. Based on a preset instruction, determine the connection status of each port in the switching module of the WIFI router; if the connection status of a port is the working state, obtain the MAC address of the first downstream device connected to this port, so as to obtain the list of MAC addresses of the first downstream devices connected to the WIFI router. The list of MAC addresses of the first downstream devices includes several MAC addresses of the first downstream devices. It can be understood that based on the connection status of the port, the MAC address of the first downstream device is determined, and the MAC address of the first downstream device is the MAC address of the downstream device connected to the WIFI router by wire.
[0047] S022. Obtain the list of MAC addresses of the second downstream devices connected through WIFI. The list of MAC addresses of the second downstream devices includes several MAC addresses of the second downstream devices.
[0048] Specifically, in S022, when obtaining the list of MAC addresses of the second downstream devices connected through WIFI, it also includes: obtaining the device association information corresponding to the MAC address of the second downstream device. The device association information includes the interface name and signal strength information.
[0049] Further, the application layer of the WIFI router sends a query command through the iwpriv tool. After receiving the command, the WiFi driver layer parses and processes it to obtain the list of MAC addresses of the second downstream devices. At the same time, the interface name, SSID information, sending and receiving statistical data information, signal strength information, etc. corresponding to the downstream devices corresponding to each MAC address of the second downstream devices are returned from the wifi driver, and then the collected information data of each downstream device is returned to the application layer in the form of a list, and then processed by the application layer.
[0050] S023. Based on the list of MAC addresses of the first downstream devices and the list of MAC addresses of the second downstream devices, obtain the list of MAC addresses of the online downstream devices.
[0051] Furthermore, after S023, it also includes: reading the ARP table of the WiFi router, and based on the list of MAC addresses of the online downstream devices, obtaining the IP address corresponding to the MAC address of the downstream device from the ARP table. When the WiFi router works in the bridge mode or the relay mode, and the IP address of the WiFi router and the IP address of the upstream router DHCP server are not in the same network segment, the relevant information of the downstream device cannot be obtained from the ARP table of the WiFi router. At this time, auxiliary processing is required to redirect the ARP information to the network layer at the layer-2 bridge.
[0052] Further, after S023, it further includes: reading the dhcp.leases table of the WiFi router, and based on the list of MAC addresses of the devices connected downstream offline, obtaining the host names corresponding to the MAC addresses of the devices connected downstream from the dhcp.leases table. When the WiFi router works in bridge mode or relay mode, since the IP addresses of the devices connected downstream are assigned by the DHCP server of the upstream router, the host name information cannot be obtained.
[0053] In summary, based on the preset instruction, determine the connection status of each port in the switching module of the WIFI router; if the connection status of a port is the working state, obtain the MAC address of the first device connected downstream of this port, so as to obtain the list of MAC addresses of the first devices connected downstream by the WIFI router, obtain the list of MAC addresses of the second devices connected downstream through WIFI, and based on the list of MAC addresses of the first devices connected downstream and the list of MAC addresses of the second devices connected downstream, obtain the list of MAC addresses of the devices connected downstream offline, and obtain all the MAC addresses of the devices connected downstream by the WIFI router by obtaining the MAC addresses of the devices connected downstream offline through wired connection and the MAC addresses of the devices connected downstream offline through WIFI.
[0054] Specifically, in the prior art, when the WiFi router works in bridge mode or relay mode, after some special processing, the IP information of the devices connected downstream is obtained through the ARP table. However, the upstream and downstream of the bridge are in the same broadcast domain, and the ARP table will contain information of other devices in the same network segment upstream in addition to the information of the devices connected downstream. Therefore, the information of the devices connected downstream cannot be accurately obtained, and DHCP and ARP have aging times. After the device connected downstream is offline, the corresponding table needs to wait until the aging time arrives before it will be updated. The information of the devices connected downstream obtained according to the dhcp.leases table and the ARP table cannot reflect the online situation of the devices in real time. It is also possible to use the ping tool to assist in detecting the online situation, but this will increase the burden on the system and network. However, in this application, the list of MAC addresses of the first devices connected downstream is obtained through the ports of the WIFI router, and the list of MAC addresses of the second devices connected downstream is obtained through WIFI, avoiding the situation of inaccurate acquisition of the MAC addresses of the devices connected downstream and improving the accuracy of obtaining the MAC addresses of the devices connected downstream.
[0055] The present invention is not limited by the aging times of ARP and DHCP, and the information can be updated in time after the device is offline; it can be used in routing mode, bridge mode, and relay mode; when working in bridge mode and relay mode, it can exclude the interference of information of other devices in the same network segment that are not connected to the Internet through this wifi router; it has the advantages of being real-time, accurate, rich in information, and wide in applicability.
[0056] Specifically, S021 further includes:
[0057] S0211, obtain the value of the specified register of the switching module of the WIFI router through a preset instruction.
[0058] S0212, determine the connection status of each port based on the value of the preset characteristic bit in the specified register.
[0059] Specifically, read the value of the specified register of the switching module through the preset instruction "switch reg r xx", and determine the connection status of each port after analyzing the value of the preset characteristic bit in the specified register.
[0060] S0213, obtain the intermediate MAC address table stored in the switching module of the WIFI router, where the intermediate MAC address table includes the mapping relationship between the MAC address and the port characteristic code, and the port characteristic code is the unique identifier of the port.
[0061] Specifically, obtain the intermediate MAC address table stored in the switching module through the "switch dump" command.
[0062] S0214, if the connection status of a port is the working state, match the port number of this port with the intermediate MAC address table to obtain the MAC address of the first downstream device connected to this port.
[0063] In summary, through the preset instruction, obtain the value of the specified register of the switching module of the WIFI router, determine the connection status of each port based on the value of the preset characteristic bit in the specified register, obtain the intermediate MAC address table stored in the switching module of the WIFI router, if the connection status of a port is the working state, match the port number of this port with the intermediate MAC address table to obtain the MAC address of the first downstream device connected to this port.
[0064] Further, after S023, it also includes: obtaining the transceiver statistical data rate of the first downstream device MAC address, specifically including the following steps:
[0065] S0310, based on the characteristic codes of each port of the switching module, obtain the first transceiver statistical data sampling information corresponding to the first downstream device MAC address.
[0066] S0320, obtain the intermediate time period, and after delaying the intermediate time period, obtain the second transceiver statistical data sampling information corresponding to the first downstream device MAC address.
[0067] Preferably, the intermediate time period is 1s.
[0068] S0330, based on the first transceiver statistical data sampling information and the second transceiver statistical data sampling information, obtain the transceiver statistical data rate of the first downstream device MAC address.
[0069] Further, the receiving and transmitting statistical data rate of the second subordinate device MAC address is obtained by the same method as that of S0310 - S0330.
[0070] In summary, based on the signature of each port of the switching module, the first receiving and transmitting statistical data sampling information corresponding to the MAC address of the first subordinate device is obtained, the intermediate time period is obtained, and after delaying the intermediate time period, the second receiving and transmitting statistical data sampling information corresponding to the MAC address of the first subordinate device is obtained. Based on the first receiving and transmitting statistical data sampling information and the second receiving and transmitting statistical data sampling information, the receiving and transmitting statistical data rate of the MAC address of the first subordinate device is obtained.
[0071] An embodiment of the present invention also provides a non - transitory computer - readable storage medium, which can be disposed in an electronic device to store at least one instruction or at least one program segment related to a method for implementing a method in the method embodiment. The at least one instruction or the at least one program segment is loaded and executed by the processor to implement the method provided in the above - mentioned embodiment.
[0072] An embodiment of the present invention also provides an electronic device, including a processor and the aforementioned non - transitory computer - readable storage medium.
[0073] Although some specific embodiments of the present invention have been described in detail by way of examples, those skilled in the art should understand that the above examples are for illustrative purposes only and not for limiting the scope of the present invention. Those skilled in the art should also understand that various modifications can be made to the embodiments without departing from the scope and spirit of the present invention.
Claims
1. A method for obtaining the online duration of a device, characterized in that, The method is used to obtain the online duration of the devices connected under a WIFI router, and the method includes the following steps: S100, obtain the current system uptime value of the WIFI router as the first uptime value; S200, obtain the corresponding node of the target device MAC address connected under the WIFI router in the single linked list and the time value stored in the corresponding node, and use the time value stored in the corresponding node as the second uptime value; S300, obtain the online duration of the target device connected under the WIFI router, where the online duration is the difference between the first uptime value and the second uptime value; Obtain the single linked list through the following steps: S001, create a single linked list, which is composed of several nodes, and each node stores the initial device MAC address connected under the WIFI router and the current system uptime value corresponding to the initial device MAC address; S002, obtain the list of MAC addresses of the devices connected online under the WIFI router, where the list of MAC addresses of the devices connected online includes m MAC addresses of the devices connected online; S003, query the i-th MAC address of the devices connected online in the list of MAC addresses of the devices connected online in the single linked list, where the value range of i is from 1 to m; S004, if the single linked list contains the MAC address of the device connected online, execute S006; S005, if the single linked list does not contain the MAC address of the device connected online, obtain the current system uptime value corresponding to the MAC address of the device connected online, and add the MAC address of the device connected online and the current system uptime corresponding to the MAC address of the device connected online as a node to the single linked list, and then execute S006; S006, i = i + 1, execute S003 until i = m.
2. The method for obtaining the online duration of a device according to claim 1, wherein After S006, it further includes: S007, traverse the single linked list, extract the MAC address of each node, so as to obtain the list of node MAC addresses, where the list of node MAC addresses includes n node MAC addresses; S008, query the j-th node MAC address in the list of node MAC addresses in the list of MAC addresses of the devices connected online, where the value range of j is from 1 to n; S009, if the j-th node MAC address exists in the list of MAC addresses of the devices connected online, execute S0011; S0010, if the j-th node MAC address does not exist in the list of MAC addresses of the devices connected online, delete the node corresponding to the node MAC address in the single linked list, and then execute S0011; S0011, j = j + 1, execute S062 until j = n.
3. The method for obtaining the online duration of a device according to claim 2, wherein After S0011, it further includes: at intervals of a preset time period, execute S002 - S0011.
4. The method for obtaining the online duration of a device according to claim 3, wherein The preset time period is 5 seconds.
5. The method for obtaining the online duration of a device according to claim 1, wherein S002 further includes obtaining the list of MAC addresses of the devices connected online under the WIFI router through the following steps: S021. Based on a preset instruction, determine the connection status of each port in the switching module of the WIFI router; if the connection status of a port is the working state, obtain the MAC address of the first downstream device connected to this port, so as to obtain the list of MAC addresses of the first downstream devices connected to the WIFI router, and the list of MAC addresses of the first downstream devices includes several MAC addresses of the first downstream devices. S022. Obtain the list of MAC addresses of the second downstream devices connected through WIFI, and the list of MAC addresses of the second downstream devices includes several MAC addresses of the second downstream devices. S023. Based on the list of MAC addresses of the first downstream devices and the list of MAC addresses of the second downstream devices, obtain the list of MAC addresses of the online downstream devices.
6. The method for obtaining the online duration of a device according to claim 5, wherein S021 further includes: S0211. Through a preset instruction, obtain the value of the specified register of the switching module of the WIFI router. S0212. Determine the connection status of each port based on the value of the preset characteristic bit in the specified register. S0213. Obtain the intermediate MAC address table stored in the switching module of the WIFI router, and the intermediate MAC address table includes the mapping relationship between the MAC address and the port characteristic code, and the port characteristic code is the unique identifier of the port. S0214. If the connection status of a port is the working state, match the port number of this port with the intermediate MAC address table to obtain the MAC address of the first downstream device connected to this port.
7. The method for obtaining the online duration of a device according to claim 6, characterized in that, After S023, it further includes: Obtain the transceiver statistical data rate of the MAC address of the first downstream device, and specifically includes the following steps: S0310. Based on the characteristic codes of each port of the switching module, obtain the first transceiver statistical data sampling information corresponding to the MAC address of the first downstream device. S0320. Obtain the intermediate time period, and after delaying the intermediate time period, obtain the second transceiver statistical data sampling information corresponding to the MAC address of the first downstream device. S0330. Based on the first transceiver statistical data sampling information and the second transceiver statistical data sampling information, obtain the transceiver statistical data rate of the MAC address of the first downstream device.
8. The method for obtaining the online duration of a device according to claim 5, wherein In S022, when obtaining the list of MAC addresses of the second downstream devices connected through WIFI, it further includes: Obtain the device association information corresponding to the MAC address of the second downstream device, and the device association information includes the interface name and the signal strength information.
9. A non-transitory computer-readable storage medium storing at least one instruction or at least one program segment, characterized in that, The at least one instruction or the at least one program is loaded and executed by a processor to implement the method for obtaining the online duration of a device as described in any one of claims 1-8.
10. An electronic device, characterized in that, It includes a processor and the non-transitory computer-readable storage medium described in claim 9.