本文主要說一下怎么使用Python來修改本地的ip和dns等,因?yàn)橛斜镜氐膇p和dns都是隨機(jī)獲取的,有些時(shí)候不是很方便,需要修改,我就稍微的封裝了一下,但是隨機(jī)ip和網(wǎng)關(guān)、子網(wǎng)掩碼等我都沒有設(shè)置為參數(shù),因?yàn)榻?jīng)常用也懶得改了,可以自己去修改一下。
測(cè)試的時(shí)候,在win8.1上面需要用管理員身份才能執(zhí)行,win7似乎是不需要管理員身份的。
使用的Python庫(kù)是WMI,這個(gè)是默認(rèn)安裝了的。如果沒有去網(wǎng)上下載即可。
該說的都在注釋里,就直接上代碼了。
# -*- coding: utf-8 -*-
import os
import random
import re
from time import sleep
from wmi import WMI
#隨機(jī)修改指定ip段的本機(jī)ip
class updateIP:
def __init__(self):
self.wmiService = WMI()
#獲取到本地有網(wǎng)卡信息
self.colNicConfigs = self.wmiService.Win32_NetworkAdapterConfiguration(IPEnabled = True)
#print self.colNicConfigs[0]
def getAdapter(self):
flag = 0
#遍歷所有網(wǎng)卡,找到要修改的那個(gè),這里我是用原ip的第一段正則出來的
for obj in self.colNicConfigs:
ip = re.findall("10.\d+.\d+.\d+", obj.IPAddress[0])
if len(ip) > 0:
return flag
else:
flag = flag+1
def runSet(self):
adapter = self.colNicConfigs[self.getAdapter()]
'''
#檢測(cè)ip是否在線,不可用,需登錄
while True:
ip2 = random.choice(['216', '217'])
ip3 = random.randint(1, 254)
ip4 = random.randint(1, 254)
newIP = '10.%s.%s.%s' % (ip2, ip3, ip4)
if self.pingIP(newIP):
break
'''
#隨機(jī)選擇了ip的第二段
ip2 = random.choice(['216', '217'])
ip3 = random.randint(1, 254) #隨機(jī)生成第三段和第二段的值
ip4 = random.randint(1, 254)
newIP = '10.%s.%s.%s' % (ip2, ip3, ip4)
arrIPAddresses = [newIP] #設(shè)置新的ip
arrSubnetMasks = ['255.248.0.0'] #子網(wǎng)掩碼
arrDefaultGateways = ['10.223.255.254'] #網(wǎng)關(guān)
arrGatewayCostMetrics = [1] #這里要設(shè)置成1,代表非自動(dòng)選擇
arrDNSServers = ['211.137.191.26'] #dns服務(wù)器
#開始執(zhí)行修改ip、子網(wǎng)掩碼、網(wǎng)關(guān)
ipRes = adapter.EnableStatic(IPAddress = arrIPAddresses, SubnetMask = arrSubnetMasks)
if ipRes[0] == 0:
print u'\ttip:設(shè)置IP成功'
print u'\t當(dāng)前ip:%s' % newIP
else:
if ipRes[0] == 1:
print u'\ttip:設(shè)置IP成功,需要重啟計(jì)算機(jī)!'
else:
print u'\ttip:修改IP失敗: IP設(shè)置發(fā)生錯(cuò)誤'
return False
#開始執(zhí)行修改dns
wayRes=adapter.SetGateways(DefaultIPGateway = arrDefaultGateways, GatewayCostMetric=arrGatewayCostMetrics)
if wayRes[0] == 0:
print u'\ttip:設(shè)置網(wǎng)關(guān)成功'
else:
print u'\ttip:修改網(wǎng)關(guān)失敗: 網(wǎng)關(guān)設(shè)置發(fā)生錯(cuò)誤'
return False
dnsRes = adapter.SetDNSServerSearchOrder(DNSServerSearchOrder=arrDNSServers)
if dnsRes[0] == 0:
print u'\ttip:設(shè)置DNS成功,等待3秒刷新緩存'
sleep(3)
#刷新DNS緩存使DNS生效
os.system('ipconfig /flushdns')
else:
print u'\ttip:修改DNS失敗: DNS設(shè)置發(fā)生錯(cuò)誤'
return False
'''
//ping某ip看是否可以通
def pingIP(self, ip):
res = os.popen('ping -n 2 -w 1 %s' % ip).read() #內(nèi)容返回到res
res = res.decode('gbk')
if u'請(qǐng)求超時(shí)' in res: #注意亂碼編碼問題
return False
else:
return True
'''
if __name__ == '__main__':
update = updateIP()
update.runSet()
input()
以上這篇python 修改本地網(wǎng)絡(luò)配置的方法就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元

