11
05
Do you know the IP address of the machine you are currently using? Or do you know how to find out? To find out the IP address, you can use the ipconfig.exe command, "Network and Sharing Center" in Control Panel, or "Settings"-> "Network and Internet"-> "Network Status" in Windows 10.
There are several ways to find out the IP address in Windows. One is ipconfig.exe, the other is the Network and Sharing Center in Control Panel, and the "Status" of "Network and Internet" in the settings.
The ipconfig command displays multiple network interfaces. At this time, the user selects an appropriate interface from his or her own knowledge and "sees" the IP address displayed there. For example, if you set up your own network router, you know the network address used for your LAN and recognize the matching one as the IP address of the machine.
In the Network and Sharing Center in Control Panel, Windows is a little more "intelligent" to determine if each network interface has an internet connection and only show network adapters that are connected to the internet. It has become.
The Network and Sharing Center in Control Panel can eliminate network adapters that are not connected to the Internet, such as Bluetooth, but will display network devices related to virtual network switches that are not connected to the Internet.
So fewer network adapters are displayed than the ipconfig command. Even so, it displays adapters made with network switches for virtual machines.
In Windows 10, "Settings"-> "Network and Internet"-> "Network status", only the physical network connected to the Internet is displayed. It seems that it is determining whether it is a virtual network interface created by a network switch.
So how are these decisions made? How can I find out my correct IP address used to connect to the Internet without knowledge of router settings or LAN network addresses?
Being connected to the Internet requires that you be able to communicate with a "gateway" (Gateway, router). This is called a "default route", and the router that is the destination is called a default gateway. The network adapter that connects to the default route communicates with the Internet, and its IP address can be said to be the "correct IP address."
Some PCs have two network interfaces, Ethernet and Wi-Fi, but by default, Windows Connection Manager prefers Ethernet (wired). When both wired and wireless are available when Windows starts, Windows first checks if the Internet can be connected via Ethernet (this checks if it can connect to a site specified by Microsoft). Check the wireless LAN side when Ethernet is not available or you are not connected to the Internet. However, it is possible for the user to force the two network interfaces, wired and wireless, to be enabled at the same time.
Also, when Hyper-V is enabled, a virtual network switch is created, and a virtual network interface for connecting the running Windows on the host side to the virtual machine side is displayed. This is also assigned an IP address. In addition, Terodo, which converts IPv4 and IPv6 via a server, is also realized using a network interface.
Also, although it is not displayed in ipconfig unless a compatible device is connected, it is possible to connect to the Internet from Bluetooth. This is displayed in "Control Panel"-> "Network and Sharing Center"-> "Change adapter settings"-> "Network connection". Windows has a number of network adapters, some of which have IP addresses. You need to choose the "correct IP address" from these without any knowledge of routers for connecting to the Internet.
There are some clues you can use. One is the default route mentioned above. At least network adapters that aren't connected to the default route aren't directly connected to the Internet. The other is to have an IP address. Since you are looking for the correct IP address in the first place, you can ignore the network adapter that is not assigned an IP address.
There are several ways to get information about network adapters. One is to use the .NET Framework to list network adapters and examine each one. However, using the .NET Framework can be quite tedious. Because it is a fairly low-level API for versatility, it is necessary to combine multiple APIs and follow the information. For example, since the default route is managed as routing information, it cannot be obtained directly from the API of the network adapter, and it is necessary to check the correspondence between the routing information and the network adapter information.
When it comes to PowerShell, it's a lot easier. PowerShell is implemented using the .NET Framework, but because it is for system management, it can handle IP addresses and network adapters in a relatively abstract format. However, this method only works with PowerShell.
The other is to use the system management function provided by Windows. Assuming that a large number of PCs will operate in a company, recent OSs have become able to retrieve various information in a certain procedure. Windows provides a mechanism called CIM / WIM for managing the status and information of such devices.
CIM is an abbreviation of "Common Information Model" and is an object model of the management target in IT created by an organization called DMTF (Distributed Management Task Force). For example, storage devices and network interfaces and the management information they should have are defined as objects (actually XML).
By making the management target an object as an industry group in this way, it is possible to make the communication between the system management tool and the platform a certain procedure. In CIM, network interfaces have names and information such as physical media type. By doing this, although the connection part with the platform is different, it is possible to support a large number of platforms by keeping the information flowing on it constant. However, since CIM abstracts physical devices that differ from one to another, sometimes only ambiguous information can be obtained from the viewpoint of actual management work.
WMI (Windows Managiment Interface) is one of the implementations of this CIM on Windows. It's complicated, so I'll explain it briefly, but in Windows, this function was named WMI, but when I changed the access protocol to a secure one, I added an API and named it CIM. WMI is one of the CIMs in the first place, only the access method is different. In order to distinguish between the original CIM and that of Windows, the one of Windows is referred to as WMI / CIM.
On Windows, you can use WMI / CIM objects to capture various management information. Some of them are related to network interfaces and IP addresses. Windows has an API to handle this WMI / CIM. In addition, the above-mentioned PowerShell also has a command corresponding to this API. This time, I will use this to see it. This method is the standard method for WMI / CIM, so you can program with the same idea in other languages. This function is "Get-CimInstace" that was also used when searching for Win32 applications (how to check the installation status of Win32 applications on Windows).
Using this, this time we will use a class called "Win32_NetworkAdapterConfiguration". This class has information such as the name and IP address of the network adapter, gateway address, and subnet. For this reason, it's a convenient class to find out which network is connected to the Windows network interface. Specifically, it is as follows.
The Get-CimInstance command gives you information about the network interface. By adding conditions such as IP address assignment and the existence of a default route, you can extract only the network adapter connected to the Internet.
Get-CimInstance -Class Win32_NetworkAdapterConfiguration | Where-Object {$ _. IPEnabled -eq "TRUE" -and $ _. Defaultipgateway}
The part before the pipe symbol "|" is the acquisition of network adapter information, and the part after it is the part that selects the one that has the IP address and the default route (default gateway) is set.
You can get the "correct IP address" by fetching the IPAddress property of the object output by this command. Please note that the results include not only IPv4 but also IPv6 addresses, and multiple IP addresses can be obtained.
(Get-CimInstance -Class Win32_NetworkAdapterConfiguration | Where-Object {$ _. IPEnabled -eq "TRUE" -and $ _. Defaultipgateway}). IPAddress
In fact, I've found that this works well in most cases to get an IP address for an internet connection. However, if you connect to Ethernet while connecting via wireless LAN, both may be temporarily connected to the Internet.
After waiting for a while, the Windows connection manager will disconnect the wireless LAN, but for about 1 minute both will be connected. At this time, both have "correct IP addresses", so it cannot be determined. If you wait for a while, the wireless LAN will be disconnected and you will have only one internet connection. At the moment, the above method seems to be the easiest way to get the correct IP address.