CN110138890B - Method, device, computer equipment and storage medium for acquiring IP address - Google Patents

Method, device, computer equipment and storage medium for acquiring IP address Download PDF

Info

Publication number
CN110138890B
CN110138890B CN201910395026.3A CN201910395026A CN110138890B CN 110138890 B CN110138890 B CN 110138890B CN 201910395026 A CN201910395026 A CN 201910395026A CN 110138890 B CN110138890 B CN 110138890B
Authority
CN
China
Prior art keywords
network interface
information
address
target
priority
Prior art date
Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
Active
Application number
CN201910395026.3A
Other languages
Chinese (zh)
Other versions
CN110138890A (en
Inventor
郭庆伟
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
Guangzhou Shiyuan Electronics Thecnology Co Ltd
Guangzhou Shirui Electronics Co Ltd
Original Assignee
Guangzhou Shiyuan Electronics Thecnology Co Ltd
Guangzhou Shirui Electronics Co Ltd
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by Guangzhou Shiyuan Electronics Thecnology Co Ltd, Guangzhou Shirui Electronics Co Ltd filed Critical Guangzhou Shiyuan Electronics Thecnology Co Ltd
Priority to CN201910395026.3A priority Critical patent/CN110138890B/en
Publication of CN110138890A publication Critical patent/CN110138890A/en
Application granted granted Critical
Publication of CN110138890B publication Critical patent/CN110138890B/en
Active legal-status Critical Current
Anticipated expiration legal-status Critical

Links

Images

Classifications

    • HELECTRICITY
    • H04ELECTRIC COMMUNICATION TECHNIQUE
    • H04LTRANSMISSION OF DIGITAL INFORMATION, e.g. TELEGRAPHIC COMMUNICATION
    • H04L61/00Network arrangements, protocols or services for addressing or naming
    • H04L61/50Address allocation
    • H04L61/5007Internet protocol [IP] addresses

Abstract

The invention relates to a method, a device, a computer device and a storage medium for obtaining IP addresses, which are used for obtaining network interface information of each network interface of target equipment, determining the priority of each network interface according to the network interface information, obtaining the IP address information of the target equipment, and then sequentially obtaining the IP addresses corresponding to the network interfaces with corresponding priorities from the IP address information according to the sequence from high to low of the priority, on one hand, the proposal can completely inquire the IP addresses of each network interface of the target equipment, ensure that the inquired IP addresses are not missed, on the other hand, the IP addresses corresponding to each network interface can be sequentially obtained from the IP address information according to the priority determined by the network interface information, and is favorable for rapidly and preferentially screening effective IP addresses under the condition of complex network interfaces, the efficiency of obtaining the IP address is improved.

Description

Method, device, computer equipment and storage medium for acquiring IP address
Technical Field
The present invention relates to the field of network technologies, and in particular, to a method for acquiring an IP address, an apparatus for acquiring an IP address, a computer device, and a computer-readable storage medium.
Background
Under various application scenarios of the Internet, an IP Address (i.e., an Internet Protocol Address, such as an IPv4 Address or an IPv6 Address) of a device needs to be acquired. For example, when the IP address information of the client device is counted, the IP address of the client device needs to be acquired first. However, in practical situations, due to the complexity of the network interfaces of the devices, the respective network interfaces of the devices often correspond to different IP addresses. The device which is connected with the WiFi hotspot and the wired network simultaneously has a wireless IP address and a wired IP address, after the virtual machine is installed, the device can increase a plurality of virtual network interfaces, and can also be connected to the mobile phone through a USB data line or Bluetooth to share the cellular wireless network of the mobile phone, and at the moment, the device can increase a plurality of network interfaces, so that the complexity of the network interfaces of the device is higher, and the IP address of the device which is actually needed is difficult to directly obtain.
In the conventional technology, the IP address information of the device is usually directly acquired by using an ifconfig command, but the IP addresses of different network interfaces are difficult to distinguish by using the technology, and if the IP address of a wired network interface and the IP address of a wireless network interface cannot be distinguished, the acquired IP addresses need to be secondarily screened by using other parameters, so that the efficiency of acquiring the IP addresses of the device by using the technology is low.
Disclosure of Invention
In view of the above, it is necessary to provide a method for acquiring an IP address, an apparatus for acquiring an IP address, a computer device and a computer readable storage medium, aiming at the technical problem of low efficiency of acquiring an IP address in the conventional technology.
A method of obtaining an IP address, comprising the steps of:
acquiring network interface information of each network interface of target equipment;
determining the priority of each network interface according to the network interface information;
acquiring IP address information of the target equipment;
and sequentially acquiring the IP addresses corresponding to the network interfaces with the corresponding priorities from the IP address information according to the sequence of the priorities from high to low.
An apparatus for acquiring an IP address, comprising:
the interface information acquisition module is used for acquiring the network interface information of each network interface of the target equipment;
a priority determining module, configured to determine a priority of each network interface according to the network interface information;
the address information acquisition module is used for acquiring the IP address information of the target equipment;
and the address acquisition module is used for sequentially acquiring the IP addresses corresponding to the network interfaces with the corresponding priorities from the IP address information according to the sequence of the priorities from high to low.
A computer device comprising a processor and a memory, the memory storing a computer program that when executed by the processor performs the steps of:
acquiring network interface information of each network interface of target equipment; determining the priority of each network interface according to the network interface information; acquiring IP address information of the target equipment; and sequentially acquiring the IP addresses corresponding to the network interfaces with the corresponding priorities from the IP address information according to the sequence of the priorities from high to low.
A computer-readable storage medium, on which a computer program is stored which, when executed by a processor, carries out the steps of:
acquiring network interface information of each network interface of target equipment; determining the priority of each network interface according to the network interface information; acquiring IP address information of the target equipment; and sequentially acquiring the IP addresses corresponding to the network interfaces with the corresponding priorities from the IP address information according to the sequence of the priorities from high to low.
The method, the apparatus, the computer device and the storage medium for acquiring the IP address acquire the network interface information of each network interface of the target device, determining the priority of each network interface according to the network interface information, acquiring the IP address information of the target device, then sequentially acquiring the IP addresses corresponding to the network interfaces with the corresponding priorities from the IP address information according to the sequence of the priorities from high to low, according to the scheme, on one hand, the IP addresses of all the network interfaces of the target equipment can be completely inquired, the IP addresses obtained through inquiry are ensured not to be omitted, on the other hand, the IP addresses corresponding to all the network interfaces can be sequentially obtained from the IP address information according to the priority determined by the network interface information, the effective IP addresses can be rapidly and preferentially screened out under the condition of complex network interfaces, and the obtaining efficiency of the IP addresses is improved.
Drawings
FIG. 1 is a flow diagram illustrating a method for obtaining an IP address in one embodiment;
FIG. 2 is a schematic flow chart diagram illustrating a method for obtaining an IP address in another embodiment;
FIG. 3 is a block diagram of an apparatus for obtaining an IP address in one embodiment;
FIG. 4 is a diagram illustrating an internal structure of a computer device according to an embodiment.
Detailed Description
In order to make the objects, technical solutions and advantages of the present invention more apparent, the present invention is described in further detail below with reference to the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are merely illustrative of the invention and are not intended to limit the invention.
In an embodiment, a method for acquiring an IP address is provided, and referring to fig. 1, fig. 1 is a schematic flowchart of a method for acquiring an IP address in an embodiment, where the method may be applied to a computer device such as a terminal, a server, and the like, and the method for acquiring an IP address may include the following steps:
step S101, network interface information of each network interface of the target device is obtained.
The device may be provided with a plurality of network interfaces, the network interfaces may be used to connect the device to a network, different network interfaces may connect the device to different networks, for example, a wireless network interface (wireless network card) may connect the device to a WiFi network, a wired network interface (wired network card) may connect to a fiber optic network, and the like.
Different network interfaces of the device correspond to different IP addresses (i.e., Internet Protocol addresses, which are abbreviations of IP addresses), and the IP addresses are a uniform Address format provided by the IP Protocol and allocate a logical Address to each device on the Internet, so as to shield the difference of physical addresses. When the IP address of the network interface of a certain device needs to be acquired, the device is set as a target device. For example, when the IP address of the network interface of the terminal a needs to be acquired, the terminal a is taken as the target device, and when the IP address of the network interface of the server a needs to be acquired, the server a is taken as the target device.
The target device may be provided with a plurality of network interfaces including a wired network card, a wireless network card, and the like, each network interface has certain network interface information, and the network interface information refers to information for describing a network interface of the device itself, such as a display name (displayName) of the network interface, an interface type (interfaceType) of the network interface, a physical interface name (bsname) of the network interface, and the like.
Taking a terminal of a macOS system as an example of a target device, the scnetworklnterfacecopyall interface of the macOS system may be called to obtain all network interface information of each network interface of the terminal, and then the network interface information of each network interface may be expressed by a ternary data set: the display name of the network interface, the interface type of the network interface and the physical interface name of the network interface are variable in the order of the ternary data set, and it can be understood that besides the three parameters of the display name of the network interface, the interface type of the network interface and the physical interface name of the network interface, other parameters of the network interface can be added into the ternary data set according to actual needs, and the network interface information is expressed in the form of the multivariate data set.
And step S102, determining the priority of each network interface according to the network interface information.
The step can mainly determine the priority of each network interface according to the network interface information of each network interface of the terminal. The priority is mainly used for identifying the query sequence of the IP addresses of all the network interfaces, and the network interfaces with high priority are queried preferentially when the IP addresses are queried.
In this step, the priority may be flexibly established according to the network interface information in combination with the actual application scenario, for example, for a terminal used as a client, the usage state of the terminal is usually a wireless or wireless wired network connection manner, and for a server, only one wired network is usually connected. Therefore, for a terminal used by a client, the priority of the wireless network interface can be set to be higher than that of the wired network interface according to the network interface information of each network interface of the terminal, i.e., the IP address of the wireless network interface is firstly queried and the IP address of the wired network interface is secondly queried, and for a server, the priority of the wired network interface can be set to be the highest priority and the IP address of the wired network interface is firstly queried according to the network interface information of each network interface of the server. By determining the priority of each network interface, a query sequence can be provided for querying the IP address, the IP address of the network interface with higher priority can be queried preferentially, the priority can be flexibly established according to the network interface information of each network interface of the target equipment by combining the actual situation, the IP address of the wired network interface can be queried preferentially, the IP address of the wireless network interface can be queried preferentially, and the IP address of the virtual network interface can be queried preferentially after the virtual machine is installed on the target equipment.
Step S103, obtaining the IP address information of the target device.
The method mainly comprises the step of obtaining IP address information of target equipment, wherein the IP address information is usually expressed in a key value pair mode, a key is a physical interface name + IP address type, and a value is a specific numerical value corresponding to the interface name + IP address type.
Taking a terminal of a macOS system as an example, the getifaddrs interface of the system can be called to obtain the IP address information of the network interface of the terminal, and the obtained IP address information is expressed in a key value pair form as follows: "en 0/ipv 4" ═ 192.168.1.101 "," en4/ipv4 "═ 172.18.145.13", and the like. Wherein, if the network interface a is not connected to the network and the network interfaces B and C are connected to the network, the IP address information obtained here includes the IP address information of the network interfaces B and C, but does not include the IP address information of the network interface a.
And step S104, sequentially acquiring the IP addresses corresponding to the network interfaces with the corresponding priorities from the IP address information according to the sequence of the priorities from high to low.
In this step, the IP addresses corresponding to the respective network interfaces may be sequentially queried among the IP address information acquired in step S103 in order from the high priority determined in step S102 to the low priority. Specifically, assuming that the network interfaces of the terminal include a network interface a, a network interface B and a network interface C, in step S102, a priority may be determined according to interface display names, interface types and physical interface names of the network interfaces, for example, the priority of the network interface B is greater than the priority of the network interface C, and the priority of the network interface C is greater than the priority of the network interface a, in this step, the IP address of the network interface B may be preferentially queried in the obtained IP address information, in a specific query process, the IP address information is usually expressed in a form of key value pairs, and the key is usually expressed by "physical interface name + IP address type", so that the physical interface name of each network interface may be used as a key for IP address query, and an IP address corresponding to the physical interface name is queried in the IP address information, the IP address corresponding to the network interface can be obtained from the IP address information.
Considering that the IP address information obtained in step S103 is the IP address information of the network interface connected to the network on the target device, and the target device may be provided with a plurality of network interfaces, there may be network interfaces not connected to the network, and the priorities of the network interfaces determined in step S102 are the priorities of all the network interfaces of the target device, so that, when the IP address is queried according to the priorities in this step, the network interface with higher priority may not be connected to the network and cannot query the corresponding IP address, and this step may sequentially query the IP addresses of the corresponding network interfaces from the network interface with higher priority in order of priority from high to low until an IP address is queried, i.e., if the IP address query is performed in the IP address information in order of network interface B, network interface C and network interface a, and under the condition that the network interface B is not connected to the network and the network interfaces A and C are both connected to the network, the inquired first IP address is the IP address of the network interface C, the IP address of the network interface C is taken as the effective IP address of the target device, the effective IP address corresponds to the inquired IP address corresponding to the first network interface, the effective IP address is actually associated with the priority of each network interface and the network connection condition of each network interface, and the priority can be set according to the actual application scene.
The method for acquiring the IP address acquires the network interface information of each network interface of the target equipment, determines the priority of each network interface according to the network interface information, acquires the IP address information of the target equipment, and then sequentially acquires the IP addresses corresponding to the network interfaces with the corresponding priorities from the IP address information according to the sequence of the priorities from high to low.
In one embodiment, the determining the priority of each network interface according to the network interface information may include:
determining a target network interface; acquiring target network interface information of a target network interface; and determining the priority according to the target network interface information and the network interface information.
In this embodiment, the number of the target network interfaces may be one or more, and the target network interfaces are preset, may be network interfaces that the target device has, or may not have. For example, the wireless network interface may be set as the target network interface, the wired network interface may be set as the target network interface, and interfaces such as USB and bluetooth may be used as the target network interface. The target network interface information refers to network interface information of the target network interface, and may include a display name of the network interface, an interface type of the network interface, a physical interface name of the network interface, and the like, where the target network interface information is mainly used to determine a priority of each network interface of the target device, and specifically, the target network interface information of the target network interface may be compared with the network interface information of each network interface of the target device, for example, the interface type of the target network interface is compared with the interface type of each network interface of the target device, and a network interface having the same interface type as the interface type of the target network interface is set as the highest priority, and the like.
Through the scheme of this embodiment, a target network interface may be preset according to the requirements of an actual application scenario, so as to set the priority of each network interface of a target device according to the network interface information of the target network interface, for example, a wireless network interface is preset as the target network interface, then the target network interface information of the target network interface is obtained, for example, the interface type is IEEE80211 and the display name is Wi-Fi as the target network interface information, and finally the interface type and the display name of the target network interface are respectively compared with the interface type and the display name of each network interface of the target device, and a network interface with the same interface type and the same display name as the target network interface may be set as a higher priority, so if the target device includes a wired network interface and a WiFi interface, the determined priority is that the WiFi interface has a higher priority than the wired network interface.
Further, in an embodiment, the step of determining the priority according to the target network interface information and the network interface information may include:
acquiring the information matching degree of target network interface information and network interface information; and determining the priority of each network interface according to the information matching degree.
In this embodiment, the information matching degree is a matching degree between the target network interface information and the network interface information, and the higher the matching degree is, the higher the priority of the corresponding network interface is. Therefore, the evaluation conditions of the priority of each network interface can be quantized, and when the information matching degree between the target network interface information and the network interface information is higher, the priority of the corresponding network interface is set to be higher, so that the priority of each network interface is accurately set.
Specifically, the priority of each network interface may be determined by using the network interface type and the network interface display name in the network interface information as an evaluation condition of the priority, and in an embodiment, the step of obtaining the information matching degree between the target network interface information and the network interface information may include:
extracting the target network interface type and the target network interface display name of the target network interface from the target network interface information; extracting the network interface type and the network interface display name of the network interface from the network interface information; and acquiring the information matching degree according to the matching degree of the target network interface type, the target network interface display name and the network interface type and the network interface display name.
In this embodiment, the target network interface information of the target network interface may include a target network interface type (interfaceType) and a target network interface display name (displayName), and correspondingly, in order to accurately evaluate the priority of the network interface, the network interface information of the network interface of the target device also needs to include a network interface type (interfaceType) and a network interface display name (displayName), after the target network interface type and the target network interface display name of the target network interface are extracted and the network interface type and the network interface display name of the network interface are extracted, the target network interface type and the network interface type may be compared, and the target network interface display name and the network interface display name may be compared, so that a network interface with the same network interface type as the target network interface type and the same network interface display name as the target network interface display name may be screened as a network interface with the highest information matching degree, the network interface with the highest matching degree of the information is the network interface with the highest priority. Assuming that the target network interface types are Ethernet and IEEE80211, and the display name of the target network interface is Wi-Fi, the information matching degree of the network interface with the network interface type being IEEE80211 and the display name of the network interface being Wi-Fi is highest among the network interfaces of the target device, the information matching degree of the network interface with the network interface type being IEEE80211 or Ethernet and the display name of the network interface being not Wi-Fi is inferior, and the information matching degree of the network interface type being not Ethernet or IEEE80211 is lowest, and then the priority order of each network interface can be obtained accordingly according to the order of the information matching degree.
In an embodiment, before the step of obtaining the information matching degree according to the target network interface type and the target network interface display name, and the matching degree between the target network interface type and the network interface display name, the network interface may be filtered, and the specific steps include:
acquiring a filtering keyword; and if the network display interface display name of the network interface contains the filtering keyword, filtering the network interface.
In this embodiment, the filtering keyword refers to data such as characters and numbers used for filtering the network interface, the filtering keyword may be preset, and the number of the filtering keyword may be one or more. When the network interface information of a certain network interface carries these keywords, for example, "USB", "Bluetooth", "Thunderbolt", and "Bridge" may be used as filtering keywords, and when the network interface display names of each network interface of the target device include the above four keywords, the corresponding network interfaces are filtered, so that there is no need to set priorities for the network interfaces, the number of network interfaces for which priorities need to be set is reduced, and the efficiency of obtaining and improving IP addresses is facilitated.
After the information matching degree of each network interface is obtained, the priority of the network interface may be determined based on the information matching degree, and in an embodiment, the step of determining the priority of each network interface according to the information matching degree may specifically include:
if the information matching degree is larger than the threshold value, setting the network interface as a candidate network interface; and determining the priority of each candidate network interface according to the relative size of the information matching degree of each network interface.
In this embodiment, a threshold may be set for the information matching degree for screening the network interfaces, when the information matching degree of the network interface is greater than the set threshold, the network interface is set as a candidate network interface, and when a network interface is set as a candidate network interface, the priorities of the candidate network interfaces are evaluated according to the relative size of the information matching degree of the candidate network port; if the information matching degree of the network interface is smaller than the threshold value, the network interface can be filtered without evaluating the network interfaces with the information matching degrees smaller than the threshold value so as to reduce data processing amount and accelerate the efficiency of obtaining the IP address, and the network interfaces with lower matching degrees are filtered, so that invalid network interfaces for an application layer can be filtered, priority ranking can be conveniently carried out on candidate network interfaces, and the accuracy of obtaining the IP address is improved.
After acquiring the IP address information of the target device and determining the priority of each network interface, the IP address of the network interface may be acquired according to the following steps, and in an embodiment, the step of sequentially acquiring the IP addresses corresponding to the network interfaces with corresponding priorities from the IP address information according to the order of the priorities from top to bottom may include:
setting an IP address query sequence of each network interface according to the priority sequence; and sequentially inquiring the IP addresses corresponding to the network interfaces from the IP address information according to the IP address inquiry sequence.
In this embodiment, the query order of the IP addresses of each network interface may be determined by the order of the priorities of each network interface, where the query order of the network interface with higher priority is prior to the query order of the network interface with lower priority. That is, if the priority of network interface a is higher than that of network interface B, the IP address query order of network interface a is better than that of network interface B. After the IP address query sequence of each network interface is set, sequentially querying the IP addresses corresponding to each network interface in the IP address information according to the IP address query sequence.
In the query process of the IP address, the IP address information is usually expressed in a form of key value pairs, and the key is usually expressed by "physical interface name + IP address type", so that the IP address corresponding to the physical interface name can be sequentially queried from the IP address information according to the set query sequence of the IP address of each network interface by the physical interface name (bsdma) of each network interface, thereby obtaining the IP address corresponding to each network interface.
When inquiring the IP address, considering that in an actual application scenario, the IP addresses of all network interfaces of the device do not need to be acquired each time, and only the effective IP address of the target device needs to be acquired, where the effective IP address refers to the IP address corresponding to the first network interface inquired in the IP address information, in an embodiment, the step of inquiring the IP address corresponding to the network interface from the IP address information according to the IP address inquiry sequence may include:
extracting physical interface names from the network interface information of each network interface; and according to the IP address query sequence, sequentially querying the IP addresses in the IP address information until finding an IP address matched with the physical interface name.
In this embodiment, the effective IP address of the target device is mainly queried from the IP address information according to the physical interface name of each network interface. Wherein, the effective IP address refers to the IP address corresponding to the first network interface queried in the IP address information, and the effective IP address is actually associated with the priority of each network interface and the network connection condition of each network interface, that is, after determining the IP address query sequence of each network interface of the target device and obtaining the IP address information of the target device, the IP address query can be performed in the IP address information according to the IP address query sequence, and since each network interface of the target device is not necessarily connected to the network, for the network interface a with the optimal IP address query sequence, the corresponding IP address may not be obtained in the IP address query, because the network interface a may not be connected to the network, the network interface a has the optimal IP address query sequence, but is not connected to the network, therefore, the IP address corresponding to the network interface a cannot be used as the IP address of the target device, and for the network interface B with the second IP address query sequence, assuming that the network interface B is already connected to the network, if the IP address of the network interface a cannot be queried, the IP address of the network interface B is queried, and the queried IP address of the network interface B is the effective IP address of the target device.
Through the technical scheme of the embodiment, the correct and effective IP address (IPv4 or IPv6) can be acquired under the condition of a complex network interface, for example, under the condition that a wired network interface and a wireless network interface are simultaneously connected, the IP address of the wireless network interface can be accurately and preferentially acquired as the effective IP address by setting the priority of the network interface and the corresponding IP address query sequence.
In order to better understand the method for acquiring an IP address in the foregoing embodiment, the method for acquiring an IP address is applied to a terminal of a macOS system as an example, in one embodiment, a method for acquiring an IP address is provided, and referring to fig. 2, fig. 2 is a schematic flow chart of the method for acquiring an IP address in another embodiment, where the method may include the following steps:
step S201, network interface information of each network interface of the terminal is obtained.
This step can call the scnetworklnterfacecopyall interface of the macOS system to obtain all the network interface information of the terminal, each network interface can be represented by a ternary data set < display name (displayName), interface Type (interface Type), physical interface name (bsname) > with a variable sequence, and all the obtained network interface information can be assumed to include: < displayName: AX8872A, interface Type: ethernet, badName: en4>, < displayName: Wi-Fi, interface Type: IEEE80211, badName: en0>, < displayName: bluetooth PAN, interface Type: ethernet, badName: en3>, < displayName: thunderbolt 1, interface Type: ethernet, badName: en1>, < displayName: thunderbolt 2, interface Type: ethernet, badName: en2> and < displayName: thunderbolt Bridge, interface Type: bridge, badName: bridge0 >.
Step S202, determining a target network interface, and acquiring target network interface information of the target network interface.
In this step, a target network interface may be preset, and network interface information of the target network interface may be obtained as the target network interface information, for example, an interface Type (interface Type) may be IEEE80211 or Ethernet, and a display name (displayName) may be Wi-Fi, which is used as the target network interface information of the target network interface.
Step S203, obtaining the information matching degree between the target network interface information and the network interface information.
In this step, the target network interface information may be compared with the network interface information of each network interface of the terminal, so as to obtain the information matching degree of each network interface.
And step S204, if the information matching degree is greater than the threshold value, setting the corresponding network interface as a candidate network interface, and determining the priority of each candidate network interface according to the information matching degree of each candidate network interface.
In this step, assuming that the target network interface information is an interface Type (interface Type) of IEEE80211 or Ethernet, and the display name (displayName) is Wi-Fi, the interface Type is equal to the display name: Wi-Fi, interface Type: IEEE80211, badName: en0> the information match for the network interface corresponding to the network interface is highest, and for a network interface corresponding to < displayName: AX8872A, interface Type: ethernet, badName: en4>, < displayName: bluetooth PAN, interface Type: ethernet, badName: en3>, < displayName: thunderbolt 1, interface Type: ethernet, badName: en1> and < displayName: thunderbolt 2, interface Type: ethernet, badName: en2>, the information matching degree of the corresponding network interface is the second time, and at this time, the network interfaces may be filtered by using "USB", "Bluetooth", "Thunderbolt", and "Bridge" as filtering keywords, so as to finally obtain the information matching degree with the < displayName: Wi-Fi, interface Type: IEEE80211, badName: en0> (denoted as network interface en0), and a network interface corresponding to < displayName: AX8872A, interface Type: ethernet, badName: en4> corresponding network interface (denoted as network interface en4), then network interface en0 and network interface en4 may be candidate network interfaces, and since the information matching degree of network interface en0 is higher than that of network interface en4, the priority may be set as: the priority of network interface en0 is set higher than the priority of network interface en 4.
Step S205, acquiring IP address information of the terminal.
In this step, getifaddrs interface of macOS system can be called to obtain all IP address information of the terminal. Wherein getifaddrs is an interface provided in a unix-like system for acquiring address information, linux and macOS systems support, and windows system has an API similar to getaddres. The IP address information is usually represented in the form of key value pairs, for example, the acquired IP address information of the terminal may include: "awdl 0/ipv 0" ═ fe 0: "68 b 0:" caff: fe4 0: fa 00 "," en0/ipv 0 "═ 192.168.1.101", "en 0/ipv 0": 184e:962d:73b 0: 469 "," en0/ipv 0 "═ 0", "en 0/ipv 0": 1800:9f 0: c9a 0: e931 "," 0/v 0 ": 127.0.0.1", "en 0/ipv 0": "utfe 0:" 1 "," utun0/ipv 0: "fe 0:: 495: 3602: 1d 36184: 0/ipc:" 753672: "757: 157: 3685".
Step S206, according to the IP address query sequence of the corresponding network interface set by the priority, extracting the physical interface name from the network interface information of the network interface, and according to the IP address query sequence, sequentially carrying out IP address query in the IP address information until an IP address matched with the physical interface name of the network interface is searched, and setting the IP address as the effective IP address of the terminal.
In this step, the IP address query sequence of each network interface may be determined according to the priority obtained in step S204, and since the priority of the network interface en0 is set to be higher than the priority of the network interface en4, the IP address query sequence is: inquiring the IP address of the network interface en0, then inquiring the IP address of the network interface en4, extracting the physical interface name (badName: en0) from the network interface information of the network interface en0, then inquiring the corresponding IP address in the IP address information obtained in step S205 according to the physical interface name (badName: en0), and inquiring that the IP address of the physical interface name (badName: en0) includes the IPv4 address: 192.168.1.101, and IPv6 address: "fe 80::184e:962d:73b7: 469", the IPv4 address or IPv6 address of the network interface en0 can be used as the effective IP address of the terminal.
The method for acquiring the IP address can be applied to a terminal of a macOS system, and can acquire the correct and effective IP address (IPv4 or IPv6) of the terminal under the condition of a complex network interface, for example, under the condition that a wired network interface and a wireless network interface are simultaneously connected, the IP address of the wireless network interface of the terminal can be accurately and preferentially acquired as the effective IP address by setting the priority of the network interface and the corresponding IP address query sequence.
In an embodiment, an apparatus for acquiring an IP address is provided, and referring to fig. 3, fig. 3 is a block diagram illustrating a structure of the apparatus for acquiring an IP address in an embodiment, where the apparatus for acquiring an IP address may include:
an interface information obtaining module 101, configured to obtain network interface information of each network interface of a target device;
a priority determining module 102, configured to determine a priority of each network interface according to the network interface information;
an address information obtaining module 103, configured to obtain IP address information of a target device;
and the address obtaining module 104 is configured to sequentially obtain, from the IP address information, IP addresses corresponding to network interfaces with corresponding priorities in an order from high priority to low priority.
In one embodiment, the priority determination module 102 is further configured to: determining a target network interface; acquiring target network interface information of a target network interface; and determining the priority according to the target network interface information and the network interface information.
In one embodiment, the priority determination module 102 is further configured to: acquiring the information matching degree of target network interface information and network interface information; and determining the priority of each network interface according to the information matching degree.
In one embodiment, the priority determination module 102 is further configured to: extracting the target network interface type and the target network interface display name of the target network interface from the target network interface information; extracting the network interface type and the network interface display name of the network interface from the network interface information; and acquiring the information matching degree according to the matching degree of the target network interface type, the target network interface display name and the network interface type and the network interface display name.
In one embodiment, the method may further include:
the first interface filtering module is used for acquiring filtering keywords; and if the network interface display name of the network interface contains the filtering keyword, filtering the network interface.
In one embodiment, the priority determination module 102 is further configured to: if the information matching degree is larger than the threshold value, setting the network interface as a candidate network interface; and determining the priority according to the relative size of the information matching degree of each candidate network interface.
In one embodiment, the method may further include:
and the second interface filtering module is used for filtering the network interface if the information matching degree is smaller than the threshold value.
In one embodiment, the address acquisition module 104 is further configured to: setting an IP address query sequence of each network interface according to the priority sequence; the query sequence of the network interface with higher priority is superior to that of the network interface with lower priority; and sequentially inquiring the IP addresses corresponding to the network interfaces from the IP address information according to the IP address inquiry sequence.
In one embodiment, the address acquisition module 104 is further configured to: extracting physical interface names from the network interface information of each network interface; and according to the IP address query sequence, sequentially querying the IP addresses in the IP address information until finding an IP address matched with the physical interface name.
The device for acquiring an IP address of the present invention corresponds to the method for acquiring an IP address of the present invention one to one, and for specific limitations of the device for acquiring an IP address, reference may be made to the above limitations on the method for acquiring an IP address, and technical features and advantages thereof described in the above embodiments of the method for acquiring an IP address are applicable to the embodiments of the device for acquiring an IP address, and are not described herein again. The modules in the apparatus for acquiring an IP address may be implemented wholly or partially by software, hardware, or a combination thereof. The modules can be embedded in a hardware form or independent from a processor in the computer device, and can also be stored in a memory in the computer device in a software form, so that the processor can call and execute operations corresponding to the modules.
In one embodiment, a computer device is provided, where the computer device may be a server or a terminal, and its internal structure diagram may be as shown in fig. 4, and fig. 4 is an internal structure diagram of the computer device in one embodiment. The computer device includes a processor, a memory, and a network interface connected by a system bus. Wherein the processor of the computer device is configured to provide computing and control capabilities. The memory of the computer device comprises a nonvolatile storage medium and an internal memory. The non-volatile storage medium stores an operating system, a computer program, and a database. The internal memory provides an environment for the operation of an operating system and computer programs in the non-volatile storage medium. The network interface of the computer device is used to connect to an external network. The computer program is executed by a processor to implement a method of obtaining an IP address.
Those skilled in the art will appreciate that the configuration shown in fig. 4 is a block diagram of only a portion of the configuration associated with aspects of the present invention and is not intended to limit the computing devices to which aspects of the present invention may be applied, and that a particular computing device may include more or less components than those shown, or may combine certain components, or have a different arrangement of components.
In one embodiment, a computer device is provided, comprising a processor and a memory, the memory storing a computer program, the processor implementing the method of acquiring an IP address as described in any one of the above embodiments when executing the computer program.
In one embodiment, there is provided a computer device comprising a processor and a memory, the memory storing a computer program which when executed by the processor performs the steps of:
acquiring network interface information of each network interface of target equipment; determining the priority of each network interface according to the network interface information; acquiring IP address information of target equipment; and sequentially acquiring the IP addresses corresponding to the network interfaces with the corresponding priorities from the IP address information according to the sequence of the priorities from high to low.
In one embodiment, the processor, when executing the computer program, further performs the steps of:
determining a target network interface; acquiring target network interface information of a target network interface; and determining the priority according to the target network interface information and the network interface information.
In one embodiment, the processor, when executing the computer program, further performs the steps of:
acquiring the information matching degree of target network interface information and network interface information; and determining the priority of each network interface according to the information matching degree.
In one embodiment, the processor, when executing the computer program, further performs the steps of:
extracting the target network interface type and the target network interface display name of the target network interface from the target network interface information; extracting the network interface type and the network interface display name of the network interface from the network interface information; and acquiring the information matching degree according to the matching degree of the target network interface type, the target network interface display name and the network interface type and the network interface display name.
In one embodiment, the processor, when executing the computer program, further performs the steps of:
acquiring a filtering keyword; and if the network interface display name of the network interface contains the filtering keyword, filtering the network interface.
In one embodiment, the processor, when executing the computer program, further performs the steps of:
if the information matching degree is larger than the threshold value, setting the network interface as a candidate network interface; and determining the priority according to the relative size of the information matching degree of each candidate network interface.
In one embodiment, the processor, when executing the computer program, further performs the steps of:
and if the information matching degree is smaller than the threshold value, filtering the network interface.
In one embodiment, the processor, when executing the computer program, further performs the steps of:
setting an IP address query sequence of each network interface according to the priority sequence; the query sequence of the network interface with higher priority is superior to that of the network interface with lower priority; and sequentially inquiring the IP addresses corresponding to the network interfaces from the IP address information according to the IP address inquiry sequence.
In one embodiment, the processor, when executing the computer program, further performs the steps of:
extracting physical interface names from the network interface information of each network interface; and according to the IP address query sequence, sequentially querying the IP addresses in the IP address information until finding an IP address matched with the physical interface name.
According to the computer equipment, through the computer program running on the processor, on one hand, the IP addresses of all the network interfaces of the target equipment can be completely inquired, the IP addresses obtained through inquiry are ensured not to be omitted, on the other hand, the IP addresses corresponding to all the network interfaces can be sequentially obtained from the IP address information according to the priority determined by the network interface information, so that the effective IP addresses can be rapidly and preferentially screened out under the condition of complex network interfaces, and the obtaining efficiency of the IP addresses is improved.
It will be understood by those skilled in the art that all or part of the processes of implementing the method for acquiring an IP address according to any of the above embodiments may be implemented by a computer program, which may be stored in a non-volatile computer-readable storage medium, and when executed, may include the processes of the above embodiments of the methods. Any reference to memory, storage, databases, or other media used in embodiments provided herein may include non-volatile and/or volatile memory. Non-volatile memory can include read-only memory (ROM), Programmable ROM (PROM), Electrically Programmable ROM (EPROM), Electrically Erasable Programmable ROM (EEPROM), or flash memory. Volatile memory can include Random Access Memory (RAM) or external cache memory. By way of illustration and not limitation, RAM is available in a variety of forms such as Static RAM (SRAM), Dynamic RAM (DRAM), Synchronous DRAM (SDRAM), Double Data Rate SDRAM (DDRSDRAM), Enhanced SDRAM (ESDRAM), Synchronous Link DRAM (SLDRAM), Rambus Direct RAM (RDRAM), direct bus dynamic RAM (DRDRAM), and memory bus dynamic RAM (RDRAM).
Accordingly, in an embodiment, a computer readable storage medium is also provided, on which a computer program is stored, wherein the program, when executed by a processor, implements the method for acquiring an IP address as described in any one of the above embodiments.
In one embodiment, a computer-readable storage medium is provided, having a computer program stored thereon, which when executed by a processor, performs the steps of:
acquiring network interface information of each network interface of target equipment; determining the priority of each network interface according to the network interface information; acquiring IP address information of target equipment; and sequentially acquiring the IP addresses corresponding to the network interfaces with the corresponding priorities from the IP address information according to the sequence of the priorities from high to low.
In one embodiment, the computer program when executed by the processor further performs the steps of:
determining a target network interface; acquiring target network interface information of a target network interface; and determining the priority according to the target network interface information and the network interface information.
In one embodiment, the computer program when executed by the processor further performs the steps of:
acquiring the information matching degree of target network interface information and network interface information; and determining the priority of each network interface according to the information matching degree.
In one embodiment, the computer program when executed by the processor further performs the steps of:
extracting the target network interface type and the target network interface display name of the target network interface from the target network interface information; extracting the network interface type and the network interface display name of the network interface from the network interface information; and acquiring the information matching degree according to the matching degree of the target network interface type, the target network interface display name and the network interface type and the network interface display name.
In one embodiment, the computer program when executed by the processor further performs the steps of:
acquiring a filtering keyword; and if the network interface display name of the network interface contains the filtering keyword, filtering the network interface.
In one embodiment, the computer program when executed by the processor further performs the steps of:
if the information matching degree is larger than the threshold value, setting the network interface as a candidate network interface; and determining the priority according to the relative size of the information matching degree of each candidate network interface.
In one embodiment, the computer program when executed by the processor further performs the steps of:
and if the information matching degree is smaller than the threshold value, filtering the network interface.
In one embodiment, the computer program when executed by the processor further performs the steps of:
setting an IP address query sequence of each network interface according to the priority sequence; the query sequence of the network interface with higher priority is superior to that of the network interface with lower priority; and sequentially inquiring the IP addresses corresponding to the network interfaces from the IP address information according to the IP address inquiry sequence.
In one embodiment, the computer program when executed by the processor further performs the steps of:
extracting physical interface names from the network interface information of each network interface; and according to the IP address query sequence, sequentially querying the IP addresses in the IP address information until finding an IP address matched with the physical interface name.
According to the computer readable storage medium, the stored computer program can completely inquire the IP addresses of all the network interfaces of the target equipment on one hand and ensure that the inquired IP addresses are not missed, and on the other hand, the IP addresses corresponding to all the network interfaces can be sequentially acquired from the IP address information according to the priority determined by the network interface information, so that the effective IP addresses can be rapidly and preferentially screened out under the condition of complex network interfaces, and the acquisition efficiency of the IP addresses is improved.
The technical features of the above embodiments can be arbitrarily combined, and for the sake of brevity, all possible combinations of the technical features in the above embodiments are not described, but should be considered as the scope of the present specification as long as there is no contradiction between the combinations of the technical features.
The above-mentioned embodiments only express several embodiments of the present invention, and the description thereof is more specific and detailed, but not construed as limiting the scope of the invention. It should be noted that, for a person skilled in the art, several variations and modifications can be made without departing from the inventive concept, which falls within the scope of the present invention. Therefore, the protection scope of the present patent shall be subject to the appended claims.

Claims (10)

1. A method for obtaining an IP address, comprising the steps of:
acquiring network interface information of each network interface of target equipment;
determining a target network interface;
acquiring target network interface information of the target network interface;
acquiring the information matching degree of the target network interface information and the network interface information;
determining the priority of each network interface according to the information matching degree;
acquiring IP address information of the target equipment;
and sequentially acquiring the IP addresses corresponding to the network interfaces with the corresponding priorities from the IP address information according to the sequence of the priorities from high to low.
2. The method according to claim 1, wherein the step of obtaining the information matching degree between the target network interface information and the network interface information comprises:
extracting the target network interface type and the target network interface display name of the target network interface from the target network interface information;
extracting the network interface type and the network interface display name of the network interface from the network interface information;
and acquiring the information matching degree according to the matching degree of the target network interface type, the target network interface display name and the network interface type and the network interface display name.
3. The method according to claim 2, wherein before the step of obtaining the matching degree of the information according to the matching degree between the target network interface type and the target network interface display name and the network interface type and the network interface display name, the method further comprises:
acquiring a filtering keyword;
and if the network interface display name of the network interface contains the filtering keyword, filtering the network interface.
4. The method of claim 1, wherein determining the priority of each network interface according to the information matching degree comprises:
if the information matching degree is larger than a threshold value, setting the network interface as a candidate network interface;
and determining the priority according to the relative size of the information matching degree of each candidate network interface.
5. The method of claim 4, further comprising the step of:
and if the information matching degree is smaller than the threshold value, filtering the network interface.
6. The method according to claim 1, wherein the step of sequentially acquiring the IP addresses corresponding to the network interfaces of the corresponding priorities from the IP address information in the order from the highest priority to the lowest priority comprises:
setting the IP address query sequence of each network interface according to the priority sequence; wherein the query order of the network interfaces with higher priority is better than the query order of the network interfaces with lower priority;
and sequentially inquiring the IP addresses corresponding to the network interfaces from the IP address information according to the IP address inquiry sequence.
7. The method according to claim 6, wherein the step of querying the IP address corresponding to the network interface from the IP address information according to the IP address query sequence comprises:
extracting a physical interface name from the network interface information of each network interface;
and sequentially inquiring the IP addresses in the IP address information according to the IP address inquiry sequence until an IP address matched with the physical interface name is found.
8. An apparatus for obtaining an IP address, comprising:
the interface information acquisition module is used for acquiring the network interface information of each network interface of the target equipment;
the priority determining module is used for determining a target network interface, acquiring target network interface information of the target network interface, acquiring information matching degree of the target network interface information and the network interface information, and determining the priority of each network interface according to the information matching degree;
the address information acquisition module is used for acquiring the IP address information of the target equipment;
and the address acquisition module is used for sequentially acquiring the IP addresses corresponding to the network interfaces with the corresponding priorities from the IP address information according to the sequence of the priorities from high to low.
9. A computer device comprising a processor and a memory, said memory storing a computer program, characterized in that said processor, when executing said computer program, implements the steps of the method according to any one of claims 1 to 7.
10. A computer-readable storage medium, on which a computer program is stored, which, when being executed by a processor, carries out the steps of the method of any one of claims 1 to 7.
CN201910395026.3A 2019-05-13 2019-05-13 Method, device, computer equipment and storage medium for acquiring IP address Active CN110138890B (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN201910395026.3A CN110138890B (en) 2019-05-13 2019-05-13 Method, device, computer equipment and storage medium for acquiring IP address

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN201910395026.3A CN110138890B (en) 2019-05-13 2019-05-13 Method, device, computer equipment and storage medium for acquiring IP address

Publications (2)

Publication Number Publication Date
CN110138890A CN110138890A (en) 2019-08-16
CN110138890B true CN110138890B (en) 2022-04-05

Family

ID=67573590

Family Applications (1)

Application Number Title Priority Date Filing Date
CN201910395026.3A Active CN110138890B (en) 2019-05-13 2019-05-13 Method, device, computer equipment and storage medium for acquiring IP address

Country Status (1)

Country Link
CN (1) CN110138890B (en)

Families Citing this family (1)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN111314146B (en) * 2020-02-25 2023-04-07 杭州涂鸦信息技术有限公司 IOT equipment service network switching method, system and equipment thereof

Citations (3)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN1988553A (en) * 2006-12-14 2007-06-27 杭州华为三康技术有限公司 Method and device for realizing field name registration
CN104468861A (en) * 2014-12-15 2015-03-25 北京奇虎科技有限公司 Terminal recognition method, device and system
CN106953795A (en) * 2016-01-07 2017-07-14 中兴通讯股份有限公司 Configure the method and device of many network interface cards

Family Cites Families (1)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CA2496939A1 (en) * 2005-02-08 2006-08-08 Cirond Networks, Inc. Network security method and apparatus

Patent Citations (3)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN1988553A (en) * 2006-12-14 2007-06-27 杭州华为三康技术有限公司 Method and device for realizing field name registration
CN104468861A (en) * 2014-12-15 2015-03-25 北京奇虎科技有限公司 Terminal recognition method, device and system
CN106953795A (en) * 2016-01-07 2017-07-14 中兴通讯股份有限公司 Configure the method and device of many network interface cards

Also Published As

Publication number Publication date
CN110138890A (en) 2019-08-16

Similar Documents

Publication Publication Date Title
CN108491450B (en) Data caching method, device, server and storage medium
CN109492019B (en) Service request response method, device, computer equipment and storage medium
CN109308305B (en) Monitoring data query method and device and computer equipment
CN111062013B (en) Account filtering method and device, electronic equipment and machine-readable storage medium
CN106210155B (en) Method and device for connecting application server
CN108717426B (en) Enterprise data updating method and device, computer equipment and storage medium
CN109246235B (en) Method and device for receiving monitoring data and data monitoring system
CN110750267A (en) Service information updating method and device, computer equipment and storage medium
US20210321358A1 (en) Sim card registration method and apparatus, computer device, and storage medium
CN110868361A (en) Gateway load balancing method, device and equipment
CN104767839A (en) IP positioning method and device
CN110138890B (en) Method, device, computer equipment and storage medium for acquiring IP address
US20170118625A1 (en) Method and Device for Controlling Subscriber Identity Module Card of Mobile Terminal
US20220039001A1 (en) Network search method and apparatus, device and storage medium
CN112541102B (en) Abnormal data filtering method, device, equipment and storage medium
CN113065887A (en) Resource processing method, resource processing device, computer equipment and storage medium
US11200204B2 (en) Method, device and computer program product for searching a file
CN112887451A (en) Domain name resolution method and device and computer equipment
CN109614399B (en) Bitmap data query method and device, computer equipment and storage medium
CN106302848B (en) Resource allocation method and device
CN108076450B (en) Method and device for identifying same user
CN112291365B (en) Access balance processing method, device, computer equipment and storage medium
CN115688099A (en) Computer virus retrieval method and device, computer equipment and storage medium
CN110572285B (en) Device code writing method, device code writing device and readable storage medium
CN113761043A (en) Data extraction method and device, computer equipment and storage medium

Legal Events

Date Code Title Description
PB01 Publication
PB01 Publication
SE01 Entry into force of request for substantive examination
SE01 Entry into force of request for substantive examination
GR01 Patent grant
GR01 Patent grant