生活中的python-实现自动更换ip

在办公室有两个网络需要随时切换使用,而且需要静态获取,每次手动太麻烦,所以写了个脚本用来自动切换ip。

源码已经上传:https://github.com/juventusryp/python-in-life

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138

# -*- coding: utf-8 -*-

import wmi

import random

print ('正在修改IP,请稍候...')

wmiService = wmi.WMI()

colNicConfigs = wmiService.Win32_NetworkAdapterConfiguration(IPEnabled = True)

if len(colNicConfigs) < 1:

print ('没有找到可用的网络适配器')

exit()

print ("-------------------------------------------------------\n")

for i in range(len(colNicConfigs)):

print (str(i+1)+" : ",colNicConfigs[i].IPAddress)

print ("-------------------------------------------------------\n")

i=int(input("选择以太网卡:\n"))

objNicConfig = colNicConfigs[i-1]

i=int(input("---------------------------\n1、切换成校园网\n2、切换成联通网\n---------------------------\n"))

if(i==1):

arrIPAddresses = ['192.168.2.11']

arrSubnetMasks = ['255.255.255.0']

arrDefaultGateways = ['192.168.2.1']

arrGatewayCostMetrics = [1]

arrDNSServers = ['114.114.114.114', '8.8.8.8']

intReboot = 0

returnValue = objNicConfig.EnableStatic(IPAddress = arrIPAddresses, SubnetMask =arrSubnetMasks)

if returnValue[0] == 0 or returnValue[0] == 1:

print ('设置IP成功')

intReboot += returnValue[0]

else:

print ('修改失败: IP或子网掩码设置发生错误')

returnValue = objNicConfig.SetGateways(DefaultIPGateway = arrDefaultGateways, GatewayCostMetric = arrGatewayCostMetrics)

if returnValue[0] == 0 or returnValue[0] == 1:

print ('设置网关成功')

intReboot += returnValue[0]

else:

print ('修改失败: 网关设置发生错误')

returnValue = objNicConfig.SetDNSServerSearchOrder(DNSServerSearchOrder = arrDNSServers)

if returnValue[0] == 0 or returnValue[0] == 1:

print ('设置DNS成功')

intReboot += returnValue[0]

else:

print (str(returnValue)+'修改失败: DNS设置发生错误')

else:

arrIPAddresses = ['10.253.201.100']

arrSubnetMasks = ['255.255.224.0']

arrDefaultGateways = ['10.253.223.254']

arrGatewayCostMetrics = [1]

arrDNSServers = ['114.114.114.114', '8.8.8.8']

intReboot = 0

returnValue = objNicConfig.EnableStatic(IPAddress = arrIPAddresses, SubnetMask =arrSubnetMasks)

if returnValue[0] == 0 or returnValue[0] == 1:

print ('设置IP成功')

intReboot += returnValue[0]

else:

print ('修改失败: IP或子网掩码设置发生错误')

returnValue = objNicConfig.SetGateways(DefaultIPGateway = arrDefaultGateways, GatewayCostMetric = arrGatewayCostMetrics)

if returnValue[0] == 0 or returnValue[0] == 1:

print ('设置网关成功')

intReboot += returnValue[0]

else:

print ('修改失败: 网关设置发生错误')

returnValue = objNicConfig.SetDNSServerSearchOrder(DNSServerSearchOrder = arrDNSServers)

if returnValue[0] == 0 or returnValue[0] == 1:

print ('设置DNS成功')

intReboot += returnValue[0]

else:

print (str(returnValue)+'修改失败: DNS设置发生错误')

if intReboot > 0:

print ('需要重新启动计算机')

print ('修改结束')

坚持原创技术分享,您的支持将鼓励我继续创作!