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