Method for dynamically obtaining ip under non-dhcp

By listening to port 67 of the DHCP protocol and resolving the Client MAC address, specific devices are allowed to automatically obtain IP addresses and enable Telnet or SSH servers, solving the problem of devices being unable to connect when the DHCP server is not enabled, and achieving both automation and security in device debugging.

CN116668409BActive Publication Date: 2025-12-19TAICANG T&W ELECTRONICS CO LTD
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202310613091.5
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2023-05-29
Publication Date
2025-12-19
Estimated Expiration
2043-05-29

AI Technical Summary

Technical Problem

Without a DHCP server enabled, the device cannot automatically obtain an IP address, making it impossible to connect via Telnet or SSH for debugging, which complicates troubleshooting and maintenance.

Method used

By listening on port 67 of the DHCP protocol, resolving the Client MAC address in the DHCP discover message, allowing only devices with specific MAC addresses to connect, automatically setting IP addresses and enabling Telnet or SSH servers, while simultaneously shutting down the debug_dhcp process, dynamic IP address acquisition is achieved.

Benefits of technology

Without compromising network security, specific devices are allowed to automatically obtain IP addresses and perform debugging. After debugging, the devices are restored to their original state, thus resolving the issue of devices being unable to connect.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN116668409B_ABST
    Figure CN116668409B_ABST
Patent Text Reader

Abstract

The application provides a method for dynamically obtaining IP under unopened DHCP, comprising the following steps: S1, adding a socket process for monitoring the 67 port; S2, determining whether the ClientMACaddress in the dhcpdiscover message is 0005ca112233, if yes, then starting the relevant action, otherwise discarding; S3, if the ClientMACaddress is determined as 0005ca112233, then automatically setting the IP address of the LAN port and starting the dhcp and telnet or SSH server, and simultaneously closing the debug_dhcp process; S4, through the execution of steps S1-S3, the device normally interacts through the DHCP protocol; S5, after the PC obtains the IP, the telnet / SSH is performed to the device for debugging; S6, after the debugging is completed, the corresponding function is closed, and the original state is restored. Beneficial effects: the method can connect the device to perform the telnet debugging under the condition that the DHCP is not opened on the device and no IP is configured, the original intention of the customer for the network security is considered, the relevant function is automatically started only during the debugging, and the function is automatically closed after the debugging is completed.
Need to check novelty before this filing date? Find Prior Art

Description

TECHNICAL FIELD

[0001] The present application belongs to the field of network security, and particularly relates to a method for dynamically obtaining IP under non-starting DHCP. BACKGROUND

[0002] In network devices, the DHCP server is a commonly used way to allocate IP addresses and other network configuration information. However, in some cases where network security is taken into account, some clients can not start the DHCP server and can not allocate IP addresses to devices. In this case, if the device has a problem, the developer needs to log in to the device to view information and cannot connect to the device, which greatly disturbs troubleshooting and maintenance work. SUMMARY

[0003] Therefore, the present application aims to provide a method for dynamically obtaining IP under non-starting DHCP to at least solve at least one problem in the background art.

[0004] To achieve the above-mentioned purpose, the technical scheme of the present application is implemented as follows:

[0005] The method for dynamically obtaining IP under non-starting DHCP comprises the following steps:

[0006] S1, adding a socket process for listening to port 67;

[0007] S2, parsing whether the ClientMACaddress in the dhcpdiscover packet is 0005ca112233, if yes, starting the relevant action, otherwise discarding;

[0008] S3, if the ClientMACaddress is determined as 0005ca112233, automatically setting the IP address of the LAN port and starting the dhcp and telnet or SSH server, and closing the debug_dhcp process;

[0009] S4, through the execution of steps S1-S3, the device normally interacts through the DHCP protocol;

[0010] S5, after the PC gets the IP, telnet / SSH to the device for debugging;

[0011] S6, after the debugging is finished, closing the corresponding function and returning to the original state.

[0012] Further, in step S1, specifically comprising:

[0013] A1, create a socket process in the device software that listens to port 67, when a DHCP message arrives at the port, use the process to accept and process the DHCP message;

[0014] A2, manually modify the MAC address of the PC to a fixed MAC, predefine a variable to store the MAC address of the device allowed to connect, then perform verification and judgment before processing the received DHCP message, only the device allowed to connect can be accepted by the system;

[0015] A3, when processing the DHCP discover message sent by the client, use the process to parse the message and discard the message with other MAC addresses;

[0016] A4, process according to the confirmed matching client.

[0017] Further, in step A3, the specific method is as follows:

[0018] Parse the ClientMACaddress from the option field of the DHCPDiscover message, and match it with the MAC address of the device allowed to connect, if not matched, ignore the message.

[0019] Further, in step S2, specifically includes:

[0020] B1, configure the DHCP server: configure the relevant policy on the DHCP server to respond to the DHCPDiscover request initiated by the client;

[0021] B2, packet analysis ClientMACaddress: use network packet capture tool to capture message data when DHCPDiscover request, analyze whether the ClientMACaddress is 0005ca112233;

[0022] B3, automatic processing: if the ClientMACaddress meets the requirements, start the relevant action, such as automatically assigning IP address and replying DHCPOffer message, otherwise discard the request and do not respond.

[0023] Further, in step S3, specifically includes:

[0024] C1, packet analysis ClientMACaddress: use network packet capture tool to capture message data when DHCPDiscover request, analyze whether the ClientMACaddress is "0005ca112233";

[0025] C2, automatically configure LAN port IP address: automatically implemented through script;

[0026] C3, starting DHCP server: ISCDHCP Server can be used in Linux system;

[0027] C4, starting SSH or Telnet server: OpenSSH or telnetd software can be used in Linux system;

[0028] C5, closing debug_dhcp process: automatic implementation through script.

[0029] Further, in step C2, specifically comprising:

[0030] using ifconfig command to obtain LAN port name;

[0031] using ifconfig command to set IP address;

[0032] saving the set IP address to corresponding configuration file, so as to still take effect after restart.

[0033] Further, in step C3, specifically comprising:

[0034] D1, installing ISCDHCP Server software;

[0035] D2, editing / etc / dhcp / dhcpd.conf file, setting subnet, netmask, option subnet-mask parameters;

[0036] D3, starting dhcpd service.

[0037] Further, the scheme discloses an electronic device, comprising a processor and a memory connected with the processor and used for storing executable instructions of the processor, and the processor is used for executing the method for dynamically obtaining IP under non-starting DHCP.

[0038] Further, the scheme discloses a server, comprising at least one processor and a memory connected with the processor, wherein the memory stores executable instructions of the at least one processor, and the instructions are executed by the processor to make the at least one processor execute the method for dynamically obtaining IP under non-starting DHCP.

[0039] Further, the scheme discloses a computer readable storage medium, which stores a computer program, and the computer program is executed by a processor to implement the method for dynamically obtaining IP under non-starting DHCP.

[0040] Compared with the prior art, the method for dynamically obtaining an IP under the condition that DHCP is not started has the following advantages:

[0041] The method for dynamically obtaining an IP under the condition that DHCP is not started has the following advantages: BRIEF DESCRIPTION OF DRAWINGS

[0042] The accompanying drawings, which form a part of the specification, illustrate the present application and, together with the description, serve to explain the principles of the application. In the drawings:

[0043] Figure 1 The method for dynamically obtaining an IP under the condition that DHCP is not started has the following advantages: DETAILED DESCRIPTION

[0044] It should be noted that the embodiments and features of the embodiments in the present application can be combined with each other without conflict.

[0045] In the description of the present application, it should be understood that the terms "center", "longitudinal", "transverse", "upper", "lower", "front", "rear", "left", "right", "vertical", "horizontal", "top", "bottom", "inner", "outer" and the like indicate the orientation or positional relationship based on the orientation or positional relationship shown in the drawings, and are only for the purpose of facilitating the description of the present application and simplifying the description, and do not indicate or imply that the device or element referred to must have a particular orientation, be constructed and operated in a particular orientation, and therefore cannot be understood as a limitation of the present application. In addition, the terms "first", "second" and the like are only for the purpose of description and cannot be understood as indicating or implying relative importance or implicitly indicating the number of the technical features indicated. Therefore, the features with "first", "second" and the like can explicitly or implicitly include one or more of the features. In the description of the present application, unless otherwise specified, the meaning of "a plurality of" is two or more.

[0046] In the description of the present application, it should be noted that unless otherwise specified and limited, the terms "mounting", "connecting", "connecting" should be understood broadly, for example, it can be fixedly connected, or it can be detachably connected, or integrally connected; it can be mechanically connected, or it can be electrically connected; it can be directly connected, or it can be indirectly connected through an intermediate medium; it can be the communication between two elements inside. For those skilled in the art, the specific meaning of the above terms in the present application can be understood through specific circumstances.

[0047] The application will be described in detail below with reference to the accompanying drawings and in conjunction with the embodiments.

[0048] The method and condition of connecting normal LAN port PC device to layer 2 device:

[0049] Layer 2 device LAN port needs to set an IP address;

[0050] Layer 2 device needs to open DHCP server;

[0051] 3. Connect LAN port with PC to obtain IP;

[0052] 4. Telnet / SSH to device.

[0053] This method cannot obtain IP in case of removing steps 1 and 2 as required by the customer, so it cannot Telnet / SSH to the device.

[0054] How to obtain IP without steps 1 and 2 is the main consideration direction of the present application.

[0055] DHCP interaction process cannot be avoided, and the present application completes the process of obtaining IP around DHCP interaction message.

[0056] When PC is connected to the LAN port of the device through the network port, the PC will certainly send DHCP discover message to attempt to automatically obtain IP. The discover message is used to optimize the whole interaction process.

[0057] The specific implementation steps are as follows:

[0058] I. Add a socket process (debug_dhcp) for listening to port 67 in the device software;

[0059] In order to prevent any device from connecting to start the hidden debug mode, the MAC address of PC needs to be manually modified to a fixed MAC (for example, 0005ca112233);

[0060] Whether to discard the message is judged by analyzing the Client MAC address in the received dhcp discover message;

[0061] II. Analyze whether the Client MAC address in the dhcp discover message is 0005cal12233, if yes, start the relevant action, if not, discard;

[0062] III. If the result is 0005cal12233, the IP address of the LAN port is automatically set, the DHCP and telnet or SSH server is started, and the debug_dhcp process is closed.

[0063] IV. At this time, conditions #1 and #2 are met, and the device can normally interact through the DHCP protocol.

[0064] V. After the PC obtains the IP, telnet / SSH is used to debug the device.

[0065] VI. After the debugging is completed, the corresponding functions are closed, and the original state is restored.

[0066] In the specific implementation process, the method is as follows:

[0067] I. Add a socket process (debug_dhcp) that listens to port 67 in the device software

[0068] 1. Create a socket process (debug_dhcp) that listens to port 67

[0069] First, create a thread to handle DHCP requests. You can use the Python built-in library threading.Thread to create a new thread and pass the function you want to execute as an argument.

[0070] In this function, create a UDP socket object by calling the `socket.socket()` method. ICMP is not commonly used, and UDP is usually used.

[0071] import socket

[0072] def serve():

[0073] # Create an IPv4 reliable connection-oriented (socket.SOCK_DGRAM) socket object

[0074] sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

[0075] # Bind the IP and port address

[0076] server_address = ('localhost', 67) # Listen to the local address, 67 is the default port of the DHCP server

[0077] sock.bind(server_address)

[0078] print('server start listening on port 67')

[0079] # Loop through and process client requests

[0080] while True:

[0081] data, address=sock.recvfrom(2048)

[0082] #Data Processing

[0083] print(f″Received:{data}from{address}″)

[0084] 2. Verify MAC address:

[0085] The following is how to extract the Client MAC Address from a received DHCP Discover message:

[0086] def get_client_mac_address(packet):

[0087] mac_hex = packet[28:34] # Get the binary sequence of mac

[0088] client_mac = ':'.join([f'{x:x2}'for x in mac_hex]) # Convert to MAC address string format

[0089] return client_mac

[0090] In networks that support the DHCP protocol, each network interface card (NIC) has a unique MAC address, which is configured by the network equipment manufacturer at the factory and cannot be changed. The MAC address of the local interface can be obtained by calling the `get_mac_address()` function.

[0091] import uuid

[0092] def get_mac_address():

[0093] mac=hex(uuid.getnode()>>48)[2:]

[0094] return″:″.join([mac[i:i+2]for i in range(0,len(mac),2)])

[0095] Then, we can easily complete the verification operation by defining a pre-configured MAC address list (allowing devices to connect to the system) in the program, and then comparing the MAC addresses extracted from the DHCP Discover packet.

[0096] 3. Process DHCP request

[0097] Use the recv() method of the socket.socket object to read all data, where server_socket is the socket process you created to listen to port 67.

[0098] data, address = server_socket.recvfrom(1024)

[0099] packet = Packet(data) # Packet is a python class I encapsulated, which can help parse the received DHCP packet

[0100] client_mac = get_client_mac_address(packet)

[0101] if client_mac not in allow_mac_list:

[0102] print(f"[Dropped]: MAC Address {client_mac} not in allowlist")

[0103] else:

[0104] handle_dhcp_request(packet, address)

[0105] Here, allow_mac_list is the list of MAC addresses allowed to connect as described above. If the MAC address of the received request is not in the allowed list, we will reject the request.

[0106] If the MAC address of the client is in the list of allowed connections, we can call the custom function handle_dhcp_request() to process the request. This function can allocate IP addresses, set the lease period, send response packets, etc. as needed.

[0107] def handle_dhcp_request(packet, address):

[0108] your ip='192.168.1.100'

[0109] lease time=86400 # set lease to one day

[0110] response_packet=Packet()

[0111] response_packet.set_op(BOOTREPLY)

[0112] response_packet.set_h_type(packet.h_type)

[0113] response_packet.set_h_len(packet.h_len)

[0114] response_packet.set_xid(packet.xid)

[0115] response_packet.set_giaddr('0.0.0.0') # set gateway addresses to 0.0.0.0

[0116] response_packet.set_ciaddr('0.0.0.0') # set last used IP addresses to 0.0.0.0

[0117] response_packet.set_yiaddr(your_ip) # assign a new IP address to the client

[0118] response_packet.set_siaddr('0.0.0.0') # set boot file path to 0.0.0.0

[0119] response_packet.set_chaddr(packet.chaddr)

[0120] # set option field

[0121] message_type=Option(DHCP_MESSAGE_TYPE)

[0122] message_type.add_value(DHCP_OFFER)

[0123] response_packet.add_option(message_type)

[0124] # Set IP address broadcast flag

[0125] response_packet.set_broadcast_flags()

[0126] # Send DHCP offor message back to client

[0127] II. Analyze whether the Client MAC address in the dhcp discover message is 0005ca112233. If yes, start relevant actions. If not, discard

[0128] 1. Configure DHCP server: Configure relevant policies on the DHCP server to respond to the DHCP Discover request initiated by the client. Specifically, you can set relevant parameters in the configuration file of the DHCP server, such as subnet, netmask, option subnet-mask, etc. For example, in ISC DHCP Server, you can configure it by editing the / etc / dhcp / dhcpd.conf file.

[0129] 2. Capture and analyze Client MAC address: Use network capture tools such as Wireshark to capture packet data when DHCP Discover request is sent, and analyze whether the Client MAC address is "0005cal12233". The specific steps are as follows:

[0130] - Open Wireshark and select the network interface you need to listen to

[0131] - Enter "bootp" in the filter bar and press Enter

[0132] - Start capturing, wait for the client to send DHCP Discover request

[0133] - Select a DHCP Discover request Packet Details, find the "Client MAC Address" field, and check if its value is "00:05:ca:11:22:33"

[0134] 3. Automation: If the Client MAC address meets the requirements, start relevant actions such as automatic IP address allocation and reply DHCP Offer message. Otherwise, discard the request and do not respond. Specific scripts can be written to achieve automation. For example, in ISC DHCP Server, you can use dhcp-script tool to associate scripts with dhcpd.conf file. Scripts can read Client MAC address in DHCP Discover request and perform corresponding processing according to requirements.

[0135] Please note that in actual use, the following factors also need to be considered:

[0136] - There may be cases of fake MAC addresses for clients, and corresponding authentication measures need to be taken to ensure system security and data integrity.

[0137] - DHCP server needs to work with other network devices such as routers, switches, etc., and needs to configure the corresponding network topology.

[0138] - For large-scale network deployment, consider the reliability and fault tolerance of DHCP server, and recommend using multiple DHCP servers for load balancing and failover.

[0139] Three, determine if 0005ca112233, automatically set the IP address of the LAN port and start dhcp and telnet or SSH server, while closing debug_dhcp process

[0140] 1. Capture packet analysis ClientMACaddress: Use network packet capture tools such as Wireshark, etc. to capture packet data when DHCP Discover request, analyze ClientMACaddress whether it is "0005ca112233". Specific steps please refer to the method above.

[0141] 2. Automatically configure LAN port IP address: Achieve automation through scripts. For example, you can write a bash shell script in Linux system to set the LAN port IP address. The specific implementation steps are as follows:

[0142] - Use ifconfig command to get LAN port name (such as eth0)

[0143] - Use ifconfig command to set IP address (such as ifconfig eth0 192.168.1.1 netmask 255.255.255.0)

[0144] - Save the set IP address to the corresponding configuration file so that it still takes effect after reboot

[0145] 3. Start the DHCP server: In Linux systems, you can use ISC DHCP Server. The specific implementation steps are as follows:

[0146] - Install ISC DHCP Server software

[0147] - Edit the / etc / dhcp / dhcpd.conf file and set subnet, netmask, option subnet-mask, etc.

[0148] - Start the dhcpd service (such as service dhcpd start)

[0149] 4. Start the SSH or Telnet server: In Linux systems, you can use OpenSSH or telnetd software. The specific implementation steps are as follows:

[0150] - Install the corresponding software

[0151] - Configure SSH or Telnet service (such as edit / etc / ssh / sshd_config file)

[0152] - Start the SSH or Telnet service (such as service sshd start)

[0153] 5. Close the debug_dhcp process: Automate it through scripts. For example, in Linux systems, you can use the kill command to close the process. The specific implementation steps are as follows:

[0154] - Use the ps command to find the PID of the debug_dhcp process

[0155] - Use the kill command to close the process (such as kill -9 PID)

[0156] Please note that in actual use, the following factors also need to be considered:

[0157] - Take appropriate authentication measures to ensure system security and data integrity.

[0158] - The DHCP server needs to work with other network devices such as routers and switches, and the corresponding network topology needs to be configured.

[0159] - For large-scale network deployment, consider the reliability and fault tolerance of the DHCP server. It is recommended to use multiple DHCP servers for load balancing and failover.

[0160] - When starting the SSH or Telnet service, the corresponding security measures need to be strengthened, such as disabling the root user login, setting the password complexity, etc.

[0161] The above only describes the preferred embodiments of the present application and is not intended to limit the present application. Any modification, equivalent replacement, improvement, etc. made within the spirit and principle of the present application shall be included in the protection scope of the present application.

Claims

1. A method for dynamically obtaining an IP under a non-activated DHCP, characterized in that, Comprise: S1, add socket process of listening 67 port; S2, analyze whether the Client MAC address in the dhcp discover message is 0005ca112233, if yes, start relevant action, otherwise discard; S3, if the Client MAC address is determined as 0005ca112233, automatically set the IP address of LAN port and start dhcp and telnet or SSH server, and close debug_dhcp process at the same time; S4, through the execution of steps S1-S3, the device normally interacts through the DHCP protocol; S5, after the PC gets the IP, the telnet / SSH server is connected to the device for debugging; S6, after the debugging is finished, the corresponding function is closed, and the original state is restored.

2. The method for dynamically acquiring IP without starting DHCP according to claim 1, characterized in that, In step S1, specifically comprising: A1, create a socket process of listening 67 port in the device software, when a DHCP message arrives at the port, use the process to accept and process the DHCP message; A2, manually modify the MAC address of the PC to a fixed MAC, predefine a variable to store the allowed device MAC address, and then verify and judge before processing the received DHCP message, only the allowed device can be accepted by the system; A3, when processing the DHCP discover message sent by the client, use the process to parse the message and discard the message except the allowed device MAC address; A4, process according to the confirmed client.

3. The method for dynamically acquiring IP without starting DHCP according to claim 2, characterized in that, In step A3, the specific method is as follows: Parse the Client MAC address from the option field of the DHCP Discover message, and match it with the allowed device MAC address, if not matched, ignore the message.

4. The method for dynamically acquiring IP without starting DHCP according to claim 1, wherein, In step S2, specifically comprising: B1, configure the DHCP server: configure relevant policies on the DHCP server to respond to the DHCP Discover request initiated by the client; B2, capture and analyze Client MAC address: use network packet capture tool to capture message data when DHCP Discover request, analyze whether the Client MAC address is 0005ca112233; B3, automatic processing: if the Client MAC address meets the requirements, start relevant action, automatically allocate IP address and reply DHCP Offer message, otherwise discard the request and do not respond.

5. The method for dynamically acquiring IP without starting DHCP according to claim 1, wherein, In step S3, specifically comprising: C1, capture and analyze Client MAC address: use network packet capture tool to capture message data when DHCP Discover request, analyze whether the Client MAC address is "0005ca112233"; C2, automatically configure LAN port IP address: automatically realized through script; C3, open DHCP server: use ISC DHCP Server in Linux system; C4, open SSH or Telnet server: use OpenSSH or telnetd software in Linux system; C5, close debug_dhcp process: achieve automation through script.

6. The method for dynamically acquiring IP without starting DHCP according to claim 5, wherein, In step C2, specifically comprising: Using ifconfig command to obtain LAN port name; Using ifconfig command to set IP address; Saving the set IP address to the corresponding configuration file, so as to still take effect after restart.

7. The method for dynamically acquiring IP without starting DHCP according to claim 5, wherein, In step C3, specifically comprising: D1, install ISC DHCP Server software; D2, edit / etc / dhcp / dhcpd.conf file, set subnet, netmask, option subnet-mask parameters; D3, start dhcpd service.

8. An electronic device, comprising a processor and a memory connected to the processor in communication, and configured to store executable instructions of the processor, characterized in that: The processor is configured to execute the method for dynamically obtaining IP under unopened DHCP in any one of claims 1-7.

9. A server, characterized by: The computer program is executed by the processor to implement the method for dynamically obtaining IP under unopened DHCP in any one of claims 1-7.

10. A computer readable storage medium storing a computer program, characterized in that: ​

Citation Information

Patent Citations

  • Method of guiding ISO and WIM mirror files through PXE protocol-based network boot manner

    CN108769267A

  • Automatic production debugging method and system for network equipment

    CN116032914A