-
Notifications
You must be signed in to change notification settings - Fork 2
/
tools.py
executable file
·259 lines (227 loc) · 8.47 KB
/
tools.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
250
251
252
253
254
255
256
257
258
259
# coding: utf-8
# ---------------------------------------------------------------------------------
# 江湖面板
# ---------------------------------------------------------------------------------
# copyright (c) 2018-∞(https://github.com/jianghujs/jh-panel) All rights reserved.
# ---------------------------------------------------------------------------------
# Author: midoks <[email protected]>
# ---------------------------------------------------------------------------------
# ---------------------------------------------------------------------------------
# 工具箱
# ---------------------------------------------------------------------------------
import sys
import os
import json
import time
import socket
sys.path.append(os.getcwd() + "/class/core")
import mw
import db
import system_api
# cmd = 'ls /usr/local/lib/ | grep python | cut -d \\ -f 1 | awk \'END {print}\''
# info = mw.execShell(cmd)
# p = "/usr/local/lib/" + info[0].strip() + "/site-packages"
# sys.path.append(p)
INIT_DIR = "/etc/rc.d/init.d"
if mw.isAppleSystem():
INIT_DIR = mw.getRunDir() + "/scripts/init.d"
INIT_CMD = INIT_DIR + "/mw"
def mw_input_cmd(msg):
if sys.version_info[0] == 2:
in_val = raw_input(msg)
else:
in_val = input(msg)
return in_val
def mw_input_default_cmd(msg, default):
if sys.version_info[0] == 2:
in_val = raw_input(msg)
else:
in_val = input(msg)
in_val = in_val if in_val else default
return in_val
def mwcli(mw_input=0, confirm=False):
raw_tip = "======================================================"
if not mw_input:
print("===============jh-panel cli tools=================")
print("(1) 重启面板服务")
print("(2) 停止面板服务")
print("(3) 启动面板服务")
print("(4) 重载面板服务")
print("(5) 修改面板端口")
print("(10) 查看面板默认信息")
print("(11) 修改面板密码")
print("(12) 修改面板用户名")
print("(13) 显示面板错误日志")
print("(20) 关闭BasicAuth认证")
print("(21) 解除域名绑定")
print("(22) 面板脚本工具")
print("(0) 取消")
print(raw_tip)
try:
mw_input = input("请输入命令编号:")
if sys.version_info[0] == 3:
mw_input = int(mw_input)
except:
mw_input = 0
nums = [1, 2, 3, 4, 5, 10, 11, 12, 13, 20, 21, 22]
if not mw_input in nums:
print(raw_tip)
print("已取消!")
exit()
if mw_input == 1:
if not confirm:
confirm = mw_input_default_cmd("提示:在面板终端执行此操作可能会失败,请勿在面板终端执行此操作\n确定要重启面板吗?(默认y)[y/n]:", 'y')
if confirm != 'y':
print("已取消")
exit()
os.system(INIT_CMD + " restart")
elif mw_input == 2:
if not confirm:
confirm = mw_input_default_cmd("提示:在面板终端执行此操作可能会失败,请勿在面板终端执行此操作\n确定要停止面板吗?(默认n)[y/n]:", 'n')
if confirm != 'y':
print("已取消")
exit()
os.system(INIT_CMD + " stop")
elif mw_input == 3:
os.system(INIT_CMD + " start")
elif mw_input == 4:
os.system(INIT_CMD + " reload")
elif mw_input == 5:
in_port = mw_input_cmd("请输入新的面板端口:")
in_port_int = int(in_port.strip())
if in_port_int < 65536 and in_port_int > 0:
import firewall_api
firewall_api.firewall_api().addAcceptPortArgs(
in_port, 'tcp', 'WEB面板[TOOLS修改]', 'port')
mw.writeFile('data/port.pl', in_port)
else:
print("|-端口范围在0-65536之间")
return
elif mw_input == 10:
os.system(INIT_CMD + " default")
elif mw_input == 11:
input_pwd = mw_input_cmd("请输入新的面板密码:")
if len(input_pwd.strip()) < 5:
print("|-错误,密码长度不能小于5位")
return
set_panel_pwd(input_pwd.strip(), True)
elif mw_input == 12:
input_user = mw_input_cmd("请输入新的面板用户名(>=5位):")
set_panel_username(input_user.strip())
elif mw_input == 13:
os.system('tail -100 ' + mw.getRunDir() + '/logs/error.log')
elif mw_input == 20:
basic_auth = 'data/basic_auth.json'
if os.path.exists(basic_auth):
os.remove(basic_auth)
os.system(INIT_CMD + " restart")
print("|-关闭basic_auth成功")
elif mw_input == 21:
bind_domain = 'data/bind_domain.pl'
if os.path.exists(bind_domain):
os.remove(bind_domain)
os.system(INIT_CMD + " unbind_domain")
print("|-解除域名绑定成功")
elif mw_input == 22:
os.system('bash /www/server/jh-panel/scripts/os_tool/index.sh vm "" "true"')
def set_panel_pwd(password, ncli=False):
# 设置面板密码
import db
sql = db.Sql()
result = sql.table('users').where('id=?', (1,)).setField(
'password', mw.md5(password))
username = sql.table('users').where('id=?', (1,)).getField('username')
if ncli:
print("|-用户名: " + username)
print("|-新密码: " + password)
else:
print(username)
def show_panel_pwd():
# 设置面板密码
import db
sql = db.Sql()
password = sql.table('users').where('id=?', (1,)).getField('password')
file_pwd = ''
if os.path.exists('data/default.pl'):
file_pwd = mw.readFile('data/default.pl').strip()
if mw.md5(file_pwd) == password:
print('password: ' + file_pwd)
return
print("the password has been changed!")
def set_panel_username(username=None):
# 随机面板用户名
import db
sql = db.Sql()
if username:
if len(username) < 5:
print("|-错误,用户名长度不能少于5位")
return
if username in ['admin', 'root']:
print("|-错误,不能使用过于简单的用户名")
return
sql.table('users').where('id=?', (1,)).setField('username', username)
print("|-新用户名: %s" % username)
return
username = sql.table('users').where('id=?', (1,)).getField('username')
if username == 'admin':
username = mw.getRandomString(8).lower()
sql.table('users').where('id=?', (1,)).setField('username', username)
print('username: ' + username)
def getServerIp():
version = sys.argv[2]
ip = mw.execShell(
"curl -{} -sS --connect-timeout 5 -m 60 https://v6r.ipip.net/?format=text".format(version))
print(ip[0] if ip[2] == 0 else "")
def getLocalIp():
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
try:
# The IP address isn't really important here, we just need to select a valid IP address
# to allow the socket to be bound correctly
sock.connect(("8.8.8.8", 80))
local_ip = sock.getsockname()[0]
finally:
sock.close()
print(local_ip)
def getPanelIp():
print(mw.getLocalIpBack())
def getStandbyIp():
standby_ip = ""
try:
slave_ssh_list_result = mw.execShell("python3 /www/server/jh-panel/plugins/mysql-apt/index.py get_slave_ssh_list {page:1,page_size:5,tojs:getSlaveSSHPage}")
slave_ssh_list_result = json.loads(slave_ssh_list_result[0])
slave_ssh_list = slave_ssh_list_result.get("data")
if len(slave_ssh_list) > 0:
standby_ip = slave_ssh_list[0].get("ip")
except:
standby_ip = ""
print(standby_ip)
if __name__ == "__main__":
method = sys.argv[1]
confirm = True if len(sys.argv) > 3 and sys.argv[3] == '-y' else False
if method == 'panel':
set_panel_pwd(sys.argv[2])
elif method == 'username':
if len(sys.argv) > 2:
set_panel_username(sys.argv[2])
else:
set_panel_username()
elif method == 'password':
show_panel_pwd()
elif method == 'getServerIp':
getServerIp()
elif method == 'getLocalIp':
getLocalIp()
elif method == 'getPanelIp':
getPanelIp()
elif method == 'getStandbyIp':
getStandbyIp()
elif method == "cli":
clinum = 0
try:
if len(sys.argv) > 2:
clinum = int(sys.argv[2]) if sys.argv[2][:6] else sys.argv[2]
except:
clinum = sys.argv[2]
mwcli(clinum, confirm)
else:
print('ERROR: Parameter error')