Python 實現 修改 本機IP地址,并自動投票的一段代碼,僅供個人學習研究。
1 import sys
2 import os,tempfile,webbrowser,random
3 import wmi,time
4 from twisted.web import client
5 from twisted.internet import reactor
6
7 def ModifyIP(ip, mask, gateway):
8 wmiService = wmi.WMI()
9 colNicConfigs = wmiService.Win32_NetworkAdapterConfiguration(IPEnabled = True)
10
11 if len(colNicConfigs) < 1:
12 print '沒有找到可用的網絡適配器'
13 exit()
14
15 #選擇要修改的網卡
16 objNicConfig = colNicConfigs[0]
17
18 print '目前配置為:'
19 print 'IP: %s' % objNicConfig.IPAddress
20 print '掩碼: %s' % objNicConfig.IPSubnet
21 print '網關: %s' % objNicConfig.DefaultIPGateway
22
23 arrIPAddresses = [ip]
24 arrSubnetMasks = [mask]
25 arrDefaultGateways = [gateway]
26 arrGatewayCostMetrics = [1]
27
28 intReboot = 0
29
30 returnValue = objNicConfig.EnableStatic(IPAddress = arrIPAddresses, SubnetMask = arrSubnetMasks)
31 print returnValue
32 if returnValue[0] == 0:
33 print '設置IP成功'
34 elif returnValue[0] == 1:
35 print '設置IP成功'
36 intReboot += 1
37 else:
38 print '修改IP失敗: IP設置發生錯誤'
39 exit()
40
41 returnValue = objNicConfig.SetGateways(DefaultIPGateway = arrDefaultGateways, GatewayCostMetric = arrGatewayCostMetrics)
42 if returnValue[0] == 0:
43 print '設置網關成功'
44 elif returnValue[0] == 1:
45 print '設置網關成功'
46 intReboot += 1
47 else:
48 print '修改網關失敗: 網關設置發生錯誤'
49 exit()
50
51 if intReboot > 0:
52 print '需要重新啟動計算機'
53 else:
54 print ''
55 print '修改后的配置為:'
56 print 'IP: %s' % objNicConfig.IPAddress
57 print '掩碼: %s' % objNicConfig.IPSubnet
58 print '網關: %s' % objNicConfig.DefaultIPGateway
59 print ''
60
61
62
63 def handleError(failure):
64 print "Error:",failure.getErrorMessage()
65 reactor.stop()
66 print "--------------Vote failed one time---------------"
67
68 def showPage(pageData):
69 print pageData
70 reactor.stop()
71 print "--------------Voted success one time---------------"
72
73 def vote():
74 voteURL="http://10.14.4.9:8081/plus/d3vote/new/vote/dovote.jsp?VoteIds=4&type=0"
75
76 postRequest=client.getPage(voteURL,method="POST")
77 postRequest.addCallback(showPage).addErrback(handleError)
78 reactor.run()
79
80 def main():
81 sysargv = sys.argv
82 prefix = sys.argv[1]
83 start = sys.argv[2]
84 end = sys.argv[3]
85 mask = sys.argv[4]
86 gateway = sys.argv[5]
87
88 ipList = []
89 for i in range(int(start), int(end) + 1):
90 ipList.append(prefix + str(i))
91
92 for ip in ipList:
93 ModifyIP(ip, mask, gateway)
94 vote()
95 time.sleep(3)
96
97
98 if __name__=="__main__":
99 main()
100