forked from kun945/weixind
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ssc.py
79 lines (57 loc) · 1.73 KB
/
ssc.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
#!/usr/bin/env python
# encoding: utf-8
import os
from shutil import copy
from filecmp import cmp as fcmp
from commands import getstatusoutput
_CONIFG_PATH = '/etc/shadowsocks/'
_CONIFG_NAME = sorted(os.listdir(_CONIFG_PATH))
_SHELL_PATH = '/home/pi/temp/shadowsocks/all.sh'
_SSC_LOCK = '/tmp/ssc.lock'
def ssp():
bufs = []
for i in range(0, len(_CONIFG_NAME)):
bufs.append('[%d] %s' %(i, _CONIFG_NAME[i]))
return '\n'.join(bufs)
def ssc(i, p='restart'):
index = int(i)
if os.path.exists(_SSC_LOCK):
return 'is changing'
if not index in range(0, len(_CONIFG_NAME)):
return '%d beyond the scope of the file list.' %(index)
o = os.path.join(_CONIFG_PATH, _CONIFG_NAME[index])
n = os.path.join(_CONIFG_PATH, 'default.json')
if fcmp(o, n): return '%s like %s' %(o, n)
fd = open(_SSC_LOCK, 'w'); fd.close()
copy(o, n)
status, output = getstatusoutput('%s %s' %(_SHELL_PATH, p))
os.remove(_SSC_LOCK)
return output
def ssu():
d = os.path.join(_CONIFG_PATH, 'default.json')
for f in _CONIFG_NAME:
if f == 'default.json':
continue
n = os.path.join(_CONIFG_PATH, f)
if fcmp(d, n):
fd = open(n, 'r')
s = 'using %s\n%s' %(f, fd.read())
fd.close()
return s.translate(None, '\'\"<>&')
return 'not in the folder'
def ssd():
if os.path.exists(_SSC_LOCK):
os.remove(_SSC_LOCK)
def sscmd(cmd):
status, output = getstatusoutput(cmd)
return output
def ssi():
return sscmd('iptables -L -t nat')
def ssr():
return sscmd('reboot')
if __name__ == '__main__':
print ssu()
print ssp()
s = raw_input('Enter index: ')
print ssc(*(int(s), 'restart'))
exit(0)