用Python更改IP地址(转)
大多数用笔记本电脑的朋友都有一个烦恼,那就是在家里和公司的IP地址不一样,上班和回家后都得来回切换IP地址,两个字“麻烦”。最近在写Python,于是在网上查找是否Python能够胜任这份造福的工作,很幸运,找到了,这是网络上的一个实现方法,经测试通过,大家可以使用。
注:需要下面两个模块的支持,请朋友们下载自行安装。
Python for Windows extensions(pywin32)
源码如下:
# -*- coding: cp936 -*-
#
# FileName: ModifyIP.py
# Date : 2008-01-15
#
import wmi
print '正在修改IP,请稍候'
wmiService = wmi.WMI()
colNicConfigs = wmiService.Win32_NetworkAdapterConfiguration(IPEnabled = True)
#for objNicConfig in colNicConfigs:
# print objNicConfig.Index
# print objNicConfig.SettingID
# print objNicConfig.Description.encode("cp936")
# print objNicConfig.IPAddress
# print objNicConfig.IPSubnet
# print objNicConfig.DefaultIPGateway
# print objNicConfig.DNSServerSearchOrder
if len(colNicConfigs) < 1:
print '没有找到可用的网络适配器'
exit()
objNicConfig = colNicConfigs[0]
#for method_name in objNicConfig.methods:
# method = getattr(objNicConfig, method_name)
# print method
arrIPAddresses = ['172.16.151.147']
arrSubnetMasks = ['255.255.255.0']
arrDefaultGateways = ['172.16.151.1']
arrGatewayCostMetrics = [1]
arrDNSServers = ['172.16.151.10']
intReboot = 0
returnValue = objNicConfig.EnableStatic(IPAddress = arrIPAddresses, SubnetMask = arrSubnetMasks)
if returnValue[0] == 0:
print '设置IP成功'
elif returnValue[0] == 1:
print '设置IP成功'
intReboot += 1
else:
print '修改IP失败: IP设置发生错误'
exit()
returnValue = objNicConfig.SetGateways(DefaultIPGateway = arrDefaultGateways, GatewayCostMetric = arrGatewayCostMetrics)
if returnValue[0] == 0:
print '设置网关成功'
elif returnValue[0] == 1:
print '设置网关成功'
intReboot += 1
else:
print '修改IP失败: 网关设置发生错误'
exit()
returnValue = objNicConfig.SetDNSServerSearchOrder(DNSServerSearchOrder = arrDNSServers)
if returnValue[0] == 0:
print '设置DNS成功'
elif returnValue[0] == 1:
print '设置DNS成功'
intReboot += 1
else:
print '修改IP失败: DNS设置发生错误'
exit()
if intReboot > 0:
print '需要重新启动计算机'
else:
print ''
print '修改后的配置为:'
print 'IP: ', ', '.join(objNicConfig.IPAddress)
print '掩码: ', ', '.join(objNicConfig.IPSubnet)
print '网关: ', ', '.join(objNicConfig.DefaultIPGateway)
print 'DNS: ', ', '.join(objNicConfig.DNSServerSearchOrder)
print '修改IP结束'
#
# FileName: ModifyIP.py
# Date : 2008-01-15
#
import wmi
print '正在修改IP,请稍候'
wmiService = wmi.WMI()
colNicConfigs = wmiService.Win32_NetworkAdapterConfiguration(IPEnabled = True)
#for objNicConfig in colNicConfigs:
# print objNicConfig.Index
# print objNicConfig.SettingID
# print objNicConfig.Description.encode("cp936")
# print objNicConfig.IPAddress
# print objNicConfig.IPSubnet
# print objNicConfig.DefaultIPGateway
# print objNicConfig.DNSServerSearchOrder
if len(colNicConfigs) < 1:
print '没有找到可用的网络适配器'
exit()
objNicConfig = colNicConfigs[0]
#for method_name in objNicConfig.methods:
# method = getattr(objNicConfig, method_name)
# print method
arrIPAddresses = ['172.16.151.147']
arrSubnetMasks = ['255.255.255.0']
arrDefaultGateways = ['172.16.151.1']
arrGatewayCostMetrics = [1]
arrDNSServers = ['172.16.151.10']
intReboot = 0
returnValue = objNicConfig.EnableStatic(IPAddress = arrIPAddresses, SubnetMask = arrSubnetMasks)
if returnValue[0] == 0:
print '设置IP成功'
elif returnValue[0] == 1:
print '设置IP成功'
intReboot += 1
else:
print '修改IP失败: IP设置发生错误'
exit()
returnValue = objNicConfig.SetGateways(DefaultIPGateway = arrDefaultGateways, GatewayCostMetric = arrGatewayCostMetrics)
if returnValue[0] == 0:
print '设置网关成功'
elif returnValue[0] == 1:
print '设置网关成功'
intReboot += 1
else:
print '修改IP失败: 网关设置发生错误'
exit()
returnValue = objNicConfig.SetDNSServerSearchOrder(DNSServerSearchOrder = arrDNSServers)
if returnValue[0] == 0:
print '设置DNS成功'
elif returnValue[0] == 1:
print '设置DNS成功'
intReboot += 1
else:
print '修改IP失败: DNS设置发生错误'
exit()
if intReboot > 0:
print '需要重新启动计算机'
else:
print ''
print '修改后的配置为:'
print 'IP: ', ', '.join(objNicConfig.IPAddress)
print '掩码: ', ', '.join(objNicConfig.IPSubnet)
print '网关: ', ', '.join(objNicConfig.DefaultIPGateway)
print 'DNS: ', ', '.join(objNicConfig.DNSServerSearchOrder)
print '修改IP结束'
功能上述已经实现了,现在我们再把它做成EXE执行文件,毕竟在Windows下,使用起来方便,一劳永逸呀!:)
1、建立编译文件setup.py
代码如下:
from distutils.core import setup
import py2exe
setup(console=['ModifyIP.py'])
import py2exe
setup(console=['ModifyIP.py'])
2、进行编译
python setup.py py2exe
如果朋友们没有安装py2exe,请点击这里下载py2exe.exe并安装。注意要找对应于你所安装的python版本。
3、执行exe文件
编译以后会自动生成build和dist两个文件夹,生成的exe文件在dist文件夹中,我们只要保留dist文件夹下的所有文件(必须全部保留),build文件夹可以删除。
好了,我们用Python更改IP的小软件就做好了,以后来回切换就轻松多了,嘿嘿!!!
转自:
http://zhulinywj.spaces.live.com/Blog/cns!BB862E2AA454A862!357.entry