本文实例讲述了python实现自动更换ip的方法。分享给大家供大家参考。具体实现方法如下:
#!/usr/bin/env python
#-*- encoding:gb2312 -*-
# filename: ip.py
import sitecustomize
import _winreg
import configparser
from ctypes import *
print ‘正在进行网络适配器检测,请稍候…’
print
netcfginstanceid = none
hkey = _winreg.openkey(_winreg.hkey_local_machine, \
r’system\currentcontrolset\control\class\{4d36e972-e325-11ce-bfc1-08002be10318}’)
keyinfo = _winreg.queryinfokey(hkey)
# 寻找网卡对应的适配器名称 netcfginstanceid
for index in range(keyinfo[0]):
hsubkeyname = _winreg.enumkey(hkey, index)
hsubkey = _winreg.openkey(hkey, hsubkeyname)
try:
hndiinfkey = _winreg.openkey(hsubkey, r’ndi\interfaces’)
lowerrange = _winreg.queryvalueex(hndiinfkey, ‘lowerrange’)
# 检查是否是以太网
if lowerrange[0] == ‘ethernet’:
driverdesc = _winreg.queryvalueex(hsubkey, ‘driverdesc’)[0]
print ‘检测到网络适配器名:’, driverdesc
netcfginstanceid = _winreg.queryvalueex(hsubkey, ‘netcfginstanceid’)[0]
print ‘检测到网络适配器id:’, netcfginstanceid
if netcfginstanceid == none:
print ‘没有找到网络适配器,程序退出’
exit()
break
_winreg.closekey(hndiinfkey)
except windowserror:
print r’message: no ndi\interfaces key’
# 循环结束,目前只提供修改一个网卡ip的功能
_winreg.closekey(hsubkey)
_winreg.closekey(hkey)
# 通过修改注册表设置ip
strkeyname = ‘system\\currentcontrolset\\services\\tcpip\\parameters\\interfaces\\’ + netcfginstanceid
print ‘网络适配器的注册表地址是:\n’, strkeyname
hkey = _winreg.openkey(_winreg.hkey_local_machine, \
strkeyname, \
0, \
_winreg.key_write)
config = configparser.configparser()
print
print ‘正在打开ip.ini配置文件…’
config.readfp(open(‘ip.ini’))
ipaddress = config.get(“school”,”ipaddress”)
subnetmask = config.get(“school”,”subnetmask”)
gateway = config.get(“school”,”gateway”)
dnsserver1 = config.get(“school”,”dnsserver1″)
dnsserver2 = config.get(“school”,”dnsserver2″)
dnsserver = [dnsserver1,dnsserver2]
print ‘配置文件内设定的信息如下,请核对:’
print
print ‘ip地址:’, ipaddress
print ‘子关掩码:’, subnetmask
print ‘默认网关:’, gateway
print ‘主dns服务器:’, dnsserver1
print ‘次dns服务器:’, dnsserver2
print
res = raw_input(‘现在,请您决定:输入1,则将配置文件写入系统;输入2,则将现有的系统设定还原为全部自动获取;否则程序退出:’)
if str(res) == ‘1’:
try:
_winreg.setvalueex(hkey, ‘enabledhcp’, none, _winreg.reg_dword, 0x00000000)
_winreg.setvalueex(hkey, ‘ipaddress’, none, _winreg.reg_multi_sz, [ipaddress])
_winreg.setvalueex(hkey, ‘subnetmask’, none, _winreg.reg_multi_sz, [subnetmask])
_winreg.setvalueex(hkey, ‘defaultgateway’, none, _winreg.reg_multi_sz, [gateway])
_winreg.setvalueex(hkey, ‘nameserver’, none, _winreg.reg_sz, ‘,’.join(dnsserver))
except windowserror:
print ‘set ip error’
exit()
_winreg.closekey(hkey)
print ‘切换成功!重置网络后即可生效’
elif str(res) == ‘2’:
try:
_winreg.setvalueex(hkey, ‘enabledhcp’, none, _winreg.reg_dword, 0x00000001)
_winreg.setvalueex(hkey, ‘t1’, none, _winreg.reg_dword, 0x00000000)
_winreg.setvalueex(hkey, ‘t2’, none, _winreg.reg_dword, 0x00000000)
_winreg.setvalueex(hkey, ‘nameserver’, none, _winreg.reg_sz, none)
_winreg.setvalueex(hkey, ‘dhcpconnforcebroadcastflag’, none, _winreg.reg_dword, 0x00000000)
_winreg.setvalueex(hkey, ‘lease’, none, _winreg.reg_dword, 0x00000000)
_winreg.setvalueex(hkey, ‘leaseobtainedtime’, none, _winreg.reg_dword, 0x00000000)
_winreg.setvalueex(hkey, ‘leaseterminatestime’, none, _winreg.reg_dword, 0x00000000)
except windowserror:
print ‘set ip error’
exit()
_winreg.closekey(hkey)
print ‘切换成功!重置网络后即可生效’
else:
print ‘用户手动取消,程序退出’
exit(”)
希望本文所述对大家的python程序设计有所帮助。