-
Notifications
You must be signed in to change notification settings - Fork 0
/
ipv6.py
127 lines (110 loc) · 4.36 KB
/
ipv6.py
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
import os
from glob import glob
import subprocess as sp
import ctypes, sys,time
class PowerShell:
# from scapy
def __init__(self, coding, ):
cmd = [self._where('PowerShell.exe'),
"-NoLogo", "-NonInteractive", # Do not print headers
"-Command", "-"] # Listen commands from stdin
startupinfo = sp.STARTUPINFO()
startupinfo.dwFlags |= sp.STARTF_USESHOWWINDOW
self.popen = sp.Popen(cmd, stdout=sp.PIPE, stdin=sp.PIPE, stderr=sp.STDOUT, startupinfo=startupinfo)
self.coding = coding
def __enter__(self):
return self
def __exit__(self, a, b, c):
self.popen.kill()
def run(self, cmd, timeout=15):
b_cmd = cmd.encode(encoding=self.coding)
try:
b_outs, errs = self.popen.communicate(b_cmd, timeout=timeout)
except sp.TimeoutExpired:
self.popen.kill()
b_outs, errs = self.popen.communicate()
outs = b_outs.decode(encoding=self.coding)
return outs, errs
@staticmethod
def _where(filename, dirs=None, env="PATH"):
"""Find file in current dir, in deep_lookup cache or in system path"""
if dirs is None:
dirs = []
if not isinstance(dirs, list):
dirs = [dirs]
if glob(filename):
return filename
paths = [os.curdir] + os.environ[env].split(os.path.pathsep) + dirs
try:
return next(os.path.normpath(match)
for path in paths
for match in glob(os.path.join(path, filename))
if match)
except (StopIteration, RuntimeError):
raise IOError("File not found: %s" % filename)
def is_admin():
try:
return ctypes.windll.shell32.IsUserAnAdmin()
except:
return False
def count_time(t):
while(t>0):
t_out = ' '+str(t) if t<10 else str(t)
print('\r倒计时: {:>}'.format(t_out), end='',flush=True)
time.sleep(1)
t-=1
if is_admin():
s = 999
while s !='9':
s = input(
'''
=====================================
请选择要进行的操作:
0---->查看ipv6状态
1---->永久关闭ipv6
2---->定时关闭ipv6
3---->开启ipv6
9---->退出
=====================================\n
'''
)
if s=='0':
print('查看ipv6状态')
with PowerShell('GBK') as ps:
outs, errs = ps.run('Get-NetAdapterBinding -Name * -ComponentID ms_tcpip6')
# print('error:', os.linesep, errs)
print('output:', os.linesep, outs)
# print('ipv6已关闭\n\n')
if s=='1':
print('关闭ipv6')
with PowerShell('GBK') as ps:
outs, errs = ps.run('Disable-NetAdapterBinding -Name * -ComponentID ms_tcpip6 -PassThru')
print('error:', os.linesep, errs)
print('output:', os.linesep, outs)
print('ipv6已关闭\n\n')
if s=='2':
t = int(input('临时关闭多少秒?'))
with PowerShell('GBK') as ps:
outs, errs = ps.run('Disable-NetAdapterBinding -Name * -ComponentID ms_tcpip6 -PassThru')
print('error:', os.linesep, errs)
print('output:', os.linesep, outs)
print('ipv6已关闭\n\n')
count_time(t)
with PowerShell('GBK') as ps:
outs, errs = ps.run('Enable-NetAdapterBinding -Name * -ComponentID ms_tcpip6 -PassThru')
print('error:', os.linesep, errs)
print('output:', os.linesep, outs)
print('ipv6已开启\n\n')
time.sleep(2)
s = '0'
if s=='3':
with PowerShell('GBK') as ps:
outs, errs = ps.run('Enable-NetAdapterBinding -Name * -ComponentID ms_tcpip6 -PassThru')
print('error:', os.linesep, errs)
print('output:', os.linesep, outs)
print('ipv6已开启\n\n')
else:
if sys.version_info[0] == 3:
ctypes.windll.shell32.ShellExecuteW(None, "runas", sys.executable, __file__, None, 1)
else:#in python2.x
ctypes.windll.shell32.ShellExecuteW(None, u"runas", unicode(sys.executable), unicode(__file__), None, 1)