【问题标题】:Python script for changing ip, subnet and gateway用于更改 ip、子网和网关的 Python 脚本
【发布时间】:2023-01-04 22:59:24
【问题描述】:

您好我正在尝试编写一个脚本来更改我的 ip、子网掩码和网关。

脚本运行,不给出任何错误但不改变任何东西。连打印,新的ip还是和现在的一样。

我究竟做错了什么?

# importing os module 
import os
import ipaddress
import socket 
      
# Get the current working 
# directory (CWD) 
cwd = os.getcwd() 
      
# Print the current working 
# directory (CWD) 
print("Current working directory:", cwd) 

# Print current IP
hn = socket.gethostname()
ipa = socket.gethostbyname(hn)
print("Current IP: ",ipa)

os.system('netsh interface ip set address name="FirstSetup" static 192.168.0.20 255.255.255.0 192.168.0.1')
print("IP ADRESS CHANGED!")

# Print newIP
hn = socket.gethostname()
ipa = socket.gethostbyname(hn)
print("New IP: ",ipa)

【问题讨论】:

    标签: python networking wmi python-ipaddress


    【解决方案1】:

    更改所有这些需要 Windows 重新启动。 尝试重新启动,您将看到您的 IP 已更改。

    【讨论】:

    • 我试过了但没有改变。可能是什么问题?实际上打印出来的新ip还是旧ip,好像没变一样
    • 会不会是因为电脑有多个nw接口?
    【解决方案2】:

    name="FirstSetup"

    该名称应该是分配给您要更改的接口的名称,通常类似于“以太网适配器以太网 1”或“无线 LAN 适配器本地连接 1”。除非您的界面实际上以某种方式命名为“FirstSetup”,否则您不会更改任何内容。

    运行“ipconfig /all”以查看您的接口名称并选择您要更改的 IP 地址所在的接口。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-01-13
      • 2012-06-28
      • 1970-01-01
      • 2012-09-26
      • 2015-01-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多