Python3.10 dynamically modifies the local IP address (static IP) of the Windows system (win10/win11)

Under normal circumstances, the advantage of setting a static IP for a terminal in a LAN such as a local server is that it can effectively reduce the network connection time. The reason is that the process of obtaining an IP address from the DHCP server after each networking is omitted in the process. The disadvantage is that it is easy to cause the IP address to be lost. Conflicts, of course, are cumbersome at the operational level. If you want to switch the static IP address, you have to manually operate it in the network connection settings. This time we use Python3.10 to dynamically modify the static IP address of the computer.

Get multi-NIC configuration

A network card corresponds to a static IP address, but there may not be only one network card on the machine, so if you want to switch dynamically, you must specify the name of the network card. In the Win system, use the ipconfig command to obtain the network card information of the current system:

ipconfig

The system returns:

PS C:\Users\liuyue\h2102-a\videopro> ipconfig  
  
Windows IP 配置  
  
  
以太网适配器 以太网:  
  
   连接特定的 DNS 后缀 . . . . . . . :  
   本地链接 IPv6 地址. . . . . . . . : fe80::a216:f22a:52a:3388%4  
   IPv4 地址 . . . . . . . . . . . . : 192.168.1.104  
   子网掩码  . . . . . . . . . . . . : 255.255.255.0  
   默认网关. . . . . . . . . . . . . : 192.168.1.1  
  
以太网适配器 以太网 2:  
  
   连接特定的 DNS 后缀 . . . . . . . :  
   IPv6 地址 . . . . . . . . . . . . : fdb2:2c26:f4e4:0:7703:1e08:e622:2f0  
   临时 IPv6 地址. . . . . . . . . . : fdb2:2c26:f4e4:0:717c:b59e:b6cd:51b2  
   本地链接 IPv6 地址. . . . . . . . : fe80::2645:f265:ad72:c751%16  
   IPv4 地址 . . . . . . . . . . . . : 192.168.0.118  
   子网掩码  . . . . . . . . . . . . : 255.255.255.0  
   默认网关. . . . . . . . . . . . . :  
  
以太网适配器 vEthernet (Default Switch):  
  
   连接特定的 DNS 后缀 . . . . . . . :  
   本地链接 IPv6 地址. . . . . . . . : fe80::3ece:9b38:2572:4e33%18  
   IPv4 地址 . . . . . . . . . . . . : 172.31.16.1  
   子网掩码  . . . . . . . . . . . . : 255.255.240.0  
   默认网关. . . . . . . . . . . . . :

If you want to obtain network card information through Python, you need to run the ipconfig command in the script to build the change_ip.py script:

import os,re  
  
class IpManage:  
  
    def __init__(self):  
  
        self.ip_list = self.get_ip()  
  
  
    def get_ip(self):  
  
  
        result = os.popen('ipconfig')  
        res = result.read()  
  
        resultlist = re.findall('''(?<=以太网适配器 ).*?(?=:)|(?<=无线局域网适配器 ).*?(?=:)''', res)  
  
        print(resultlist)  
  
        return resultlist  
  
if __name__ == '__main__':  
      
    IpManage()

Here, the ipconfig command is run through the popen method of the os module, and then the regular pattern is used to match the name of the network card. Finally, the list of matched network cards is assigned to the instance attribute, and the program returns:

['以太网', '以太网 2', 'vEthernet (Default Switch)']  
[Finished in 394ms]

So far, the names of the three network cards have been obtained.

Dynamically switch static IP

The next step is to dynamically switch the static IP address of the specified network card through the Python script. The Windows system specifies the IP address through the netsh command:

netsh interface ip set address name=以太网 static 192.168.201.137 255.255.248.0 192.168.200.1

Here the name parameter is the name of the network card, and the following three addresses represent the static IP address, subnet mask and gateway address respectively.

Here, the static IP address of the first network card is set to 192.168.201.137, the subnet mask is 255.255.248.0, and the gateway address is 192.168.200.1.

Then check it in the network connection settings of Windows:

It is found that it has been set up, and then manually modify it to obtain an IP address automatically.

The following is set up through a Python script:

def set_ip(self,name,ip="192.168.201.137",mask="255.255.248.0",gateway="192.168.200.1"):  
  
        result = os.popen(f"netsh interface ip set address name={name} static {ip} {mask} {gateway}")  
        res = result.read()  
  
        print(res)

Add an instance method here to set the ip address, also use the popen method to run the command, and then call it:

if __name__ == '__main__':  
      
    im = IpManage()  
  
    im.set_ip(im.ip_list[0])

Here, specify the IP address of the first network card.

Full code:

import os,re  
  
class IpManage:  
  
    def __init__(self):  
  
        self.ip_list = self.get_ip()  
  
  
    def set_ip(self,name,ip="192.168.201.137",mask="255.255.248.0",gateway="192.168.200.1"):  
  
        result = os.popen(f"netsh interface ip set address name={name} static {ip} {mask} {gateway}")  
        res = result.read()  
  
  
  
    def get_ip(self):  
  
  
        result = os.popen('ipconfig')  
        res = result.read()  
  
        resultlist = re.findall('''(?<=以太网适配器 ).*?(?=:)|(?<=无线局域网适配器 ).*?(?=:)''', res)  
  
        print(resultlist)  
  
        return resultlist  
  
if __name__ == '__main__':  
      
    im = IpManage()  
  
    im.set_ip(im.ip_list[0])

epilogue

In this way, we can dynamically configure the static IP address of the local network card through Python3.10, which can also be understood as a Python automation process. The static IP address can make the IP address semantic, and it is suitable for data centers, websites, and bank settlements. Static IP is often required for ports, etc. At the same time, it also saves the tedious process of manually configuring static IP.

Guess you like

Origin blog.csdn.net/zcxey2911/article/details/130571444