-
Notifications
You must be signed in to change notification settings - Fork 72
/
_sss.py
249 lines (209 loc) · 6.2 KB
/
_sss.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
#!/usr/bin/env python3
# coding: utf-8
# Create by : https://github.com/lidalao/ServerStatus
# 版本:0.0.1, 支持Python版本:2.7 to 3.9
# 支持操作系统: Linux, OSX, FreeBSD, OpenBSD and NetBSD, both 32-bit and 64-bit architectures
import json
import sys
import os
import requests
import random,string
import subprocess
import uuid
CONFIG_FILE = "config.json"
GITHUB_RAW_URL = "https://raw.githubusercontent.com/lidalao/ServerStatus/master"
IP_URL = "https://api.ipify.org"
jjs = {}
ip = ""
def how2agent(user, passwd):
print('```')
print("\n")
print('curl -L {0}/sss-agent.sh -o sss-agent.sh && chmod +x sss-agent.sh && sudo ./sss-agent.sh {1} {2} {3}'.format(GITHUB_RAW_URL, getIP(), user, passwd))
print("\n")
print('```')
def getIP():
global ip
if ip == "":
ip = requests.get(IP_URL).content.decode('utf8')
return ip
def restartSSS():
cmd = ["docker-compose", "restart"]
p = subprocess.Popen(cmd, stdout=subprocess.PIPE)
for line in p.stdout:
print(line)
p.wait()
def getPasswd():
mima = []
sz = '123456789'
xzm = 'abcdefghijklmnopqrstuvwxyz'
dzm = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
# tzf = '~!#@$%^&*?'
# all = sz + xzm + dzm + tzf
all = sz + xzm + dzm
m1 = random.choice(sz)
m2 = random.choice(xzm)
m3 = random.choice(dzm)
# m4 = random.choice(tzf)
m5 = "".join(random.sample(all,12))
mima.append(m1)
mima.append(m2)
mima.append(m3)
# mima.append(m4)
mima.append(m5)
random.shuffle(mima)
a = "".join(mima)
return a
def saveJJs():
jjs['servers'] = sorted(jjs['servers'], key=lambda d: d['name'])
file = open(CONFIG_FILE,"w")
file.write(json.dumps(jjs))
file.close()
def _show():
print("---你的jjs如下---")
print("\n")
if len(jjs['servers']) == 0:
print('>>> 你好MJJ, 暂时没发现你有任何jj! <<<')
print("\n")
print("-----------------")
return
for idx, item in enumerate(jjs['servers']):
print(str(idx) + ". name: " + item['name'] + ", loc: "+ item['location'] + ", type: " + item['type'])
print("\n")
print("-----------------")
def show():
_show()
_back()
def _back():
print(">>>按任意键返回上级菜单")
input()
cmd()
def add():
print('>>>请输入jj名字:')
jjname =input()
if jjname == "":
print("输入有误")
_back()
return
print('>>>请输入{0}位置:[{1}]'.format(jjname, "us"))
jjloc =input()
if jjloc == "":
jjloc = "us"
print('>>>请输入{0}类型:[{1}]'.format(jjname, "kvm"))
jjtype =input()
if jjtype == "":
jjtype = "kvm"
item = {}
item['monthstart'] = "1"
item['location'] = jjloc
item['type'] = jjtype
item['name'] = jjname
item['username'] = uuid.uuid4().hex
item['host'] = jjname
item['password'] = getPasswd()
jjs['servers'].append(item)
saveJJs()
print("操作完成,等待服务重启")
restartSSS()
print("添加成功!")
_show()
print('>>>请复制以下命令在机器{0}安装agent服务'.format(item['name']))
how2agent(item['username'], item['password'])
_back()
def update():
print("请输入需要更新的jj标号:")
idx = input()
if not idx.isnumeric():
print('无效输入,退出')
_back()
return
if len(jjs['servers']) <= int(idx):
print('输入无效')
_back()
return
jj = jjs['servers'][int(idx)]
print('--- 面板更换ip时,请复制以下命令在机器{0}安装agent服务 ---'.format(jj['name']))
how2agent(jj['username'], jj['password'])
print('>>>请输入{0}新名字:[{1}] *中括号内为原值,按回车表示不做修改*'.format(jj['name'], jj['name']))
jjname = input()
if "" != jjname:
jjs['servers'][int(idx)]['name'] = jjname
print('>>>请输入{0}新位置:[{1}]'.format(jj['name'], jj['location']))
jjloc = input()
if "" != jjloc:
jjs['servers'][int(idx)]['location'] = jjloc
print('>>>请输入{0}新类型:[{1}]'.format(jj['name'], jj['type']))
jjtype = input()
if "" != jjtype:
jjs['servers'][int(idx)]['type'] = jjtype
print('>>>请输入{0}新的月流量起始日:[{1}]'.format(jj['name'], jj['monthstart']))
jjms = input()
if "" != jjms:
jjs['servers'][int(idx)]['monthstart'] = jjms
if "" == jjname and "" == jjloc and "" == jjtype and "" == jjms:
print('未做任何更新,直接返回')
_back()
return
saveJJs()
print("操作完成,等待服务重启")
restartSSS()
print("更新成功!")
_show()
_back()
def remove():
print(">>>请输入需要删除的jj标号:")
idx =input()
if not idx.isnumeric():
print('无效输入,退出')
_back()
return
if len(jjs['servers']) <= int(idx):
print('输入无效')
_back()
return
print('>>>请确认你需要删除的节点:{0}? [Y/n]'.format(jjs['servers'][int(idx)]['name']))
yesOrNo = input()
if yesOrNo == "n" or yesOrNo == "N":
print("取消删除")
_back()
return
del jjs['servers'][int(idx)]
saveJJs()
print("操作完成,等待服务重启")
restartSSS()
print("删除成功!")
_show()
_back()
def cmd():
print("\n")
print('- - - 欢迎使用最简洁的探针: Server Status - - -')
print('详细教程请参考:https://lidalao.com/archives/87')
print("\n")
_show()
print("\n")
print('>>>请输入操作标号:1.查看, 2.添加, 3.删除, 4.更新, 0.退出')
x = input()
if not x.isnumeric():
print('无效输入, 退出')
return
if 1 == int(x):
show()
elif 2 == int(x):
add()
elif 3 == int(x):
remove()
elif 4 == int(x):
update()
elif 0 == int(x):
return
else:
print('无效输入, 退出')
return
if __name__ == '__main__':
file_exists = os.path.exists(CONFIG_FILE)
if file_exists == False:
print("请在当前目录创建config.json!")
exit()
file = open(CONFIG_FILE,"r")
jjs = json.load(file)
file.close()
cmd()