import os
my_dict = {}
class changeNet:
def __init__(self):
print("执行开始....")
def getLocalIp(self):
# 执行命令并获取输出
output = os.popen("ipconfig").read()
# 使用正则表达式提取IP地址
# print("output:", output)
char =my_dict['ip']
if char in output:
return True
else:
return False
def getmap(self):
# 打开文件
with open('./config.txt', 'r') as file:
# 读取文件内容
content = file.read()
# 提取键值对
key_value_pairs = content.split('\n') # 按行分割
for pair in key_value_pairs:
key, value = pair.split('=') # 按等号分割
# 向字典中添加键值对
# print(f"Key: {key}, Value: {value}")
my_dict[f"{key}"]=f"{value}"
# 打印字典中的数据
# print(my_dict)
# return my_dict
def changeNet(self):
# 设置新的IP地址、子网掩码和默认网关
netname = my_dict['netname']
ip_address = my_dict['ip']
subnet_mask = my_dict['subnetmask']
gateway = my_dict['gateway']
# 设置DNS服务器地址
dns_servers = [my_dict['dns1'], my_dict['dns2']]
# 构建命令行参数
result = os.popen(f"netsh interface ip set address name={netname} static {ip_address} {subnet_mask} {gateway}")
res = result.read()
print(res)
return res
def reChangeNet(self):
# 设置新的IP地址、子网掩码和默认网关
netname = my_dict['netname']
# 构建命令行参数
result = os.popen(f"netsh interface ip set address name={netname} source=dhcp")
res = result.read()
print(res)
return res
import tkinter as tk
from tkinter import messagebox
window = tk.Tk()
window.withdraw() # 隐藏主窗口
def on_confirm():
window.destroy() # 关闭主窗口
def open_window(str,res):
messagebox.showinfo("执行提示", str+":"+res)
confirm_button = tk.Button(window, text="确认")
confirm_button.pack()
on_confirm()
window.mainloop()
if __name__ == "__main__":
ch = changeNet()
ch.getmap()
bo = ch.getLocalIp()
res =""
if(bo):
res = ch.reChangeNet()
open_window("默认ipv4配置还原",res)
else:
res = ch.changeNet()
open_window("自定义ipv4配置生效",res)
image.png
打包后 修改config配置信息,管理员启动changeNet.exe 自动生效
网友评论