Initial YakPanel commit

This commit is contained in:
Niranjan
2026-04-07 02:04:22 +05:30
commit 2826d3e7f3
5359 changed files with 1390724 additions and 0 deletions

View File

@@ -0,0 +1,7 @@
from .process import RealProcess
from .user import RealUser
from .server import RealServer
from .process import Process
from .user import User
from .server import Server
__all__ = ['RealProcess', 'Process', 'RealUser', 'User', 'RealServer', 'Server']

889
mod/base/process/process.py Normal file
View File

@@ -0,0 +1,889 @@
# coding: utf-8
# -------------------------------------------------------------------
# yakpanel
# -------------------------------------------------------------------
# Copyright (c) 2015-2099 yakpanel(http://www.yakpanel.com) All rights reserved.
# -------------------------------------------------------------------
# Author: sww <sww@yakpanel.com>
# -------------------------------------------------------------------
import json
import os
# ------------------------------
# 进程模型
# ------------------------------
import sys
import time
import traceback
if "/www/server/panel/class" not in sys.path:
sys.path.insert(0, "/www/server/panel/class")
os.chdir("/www/server/panel")
import public
import psutil
from typing import Any
try:
from YakPanel import cache
except:
import cachelib
cache = cachelib.SimpleCache()
class RealProcess:
process_path = '/proc'
ps = json.loads(public.readFile('/www/server/panel/mod/base/process/process_ps.json'))
__isUfw = False
__isFirewalld = False
old_info = {}
new_info = {}
old_path = '/tmp/bt_task_old1.json'
__cpu_time = None
__process_net_list = {}
last_net_process = None
last_net_process_time = 0
old_net_path = '/tmp/bt_network_old1.json'
old_net_info = {}
new_net_info = {}
def __init__(self):
if os.path.exists('/usr/sbin/firewalld'): self.__isFirewalld = True
if os.path.exists('/usr/sbin/ufw'): self.__isUfw = True
def object_to_dict(self, obj):
result = {}
for name in dir(obj):
value = getattr(obj, name)
if not name.startswith('__') and not callable(value) and not name.startswith('_'): result[name] = value
return result
def get_computers_use(self):
result = {}
cpu_usage = psutil.cpu_percent(interval=1, percpu=True)
result['cpu'] = round(sum(cpu_usage) / len(cpu_usage), 2)
memory = psutil.virtual_memory()
print(memory.total)
result['memory_usage'] = memory.percent
disk = psutil.disk_usage('/')
result['disk_usage'] = round(((disk.used / disk.total) * 100), 0)
network_io = psutil.net_io_counters()
result['network_io_bytes_sent'] = network_io.bytes_sent
result['network_io_bytes_recv'] = network_io.bytes_recv
return result
# ------------------------------ 获取进程列表 start ------------------------------
def get_process_list(self):
"""
获取进程列表
:return:
"""
try:
process_list = []
if type(self.new_info) != dict: self.new_info = {}
self.new_info['cpu_time'] = self.get_cpu_time()
self.new_info['time'] = time.time()
self.get_process_net_list()
for proc in psutil.process_iter(
['pid', 'ppid', 'name', 'username', 'create_time', 'memory_info', 'io_counters', 'num_threads', 'create_time', 'connections', 'open_files', 'status', 'cmdline']):
try:
proc_info = proc.as_dict(
attrs=['pid', 'ppid', 'name', 'username', 'create_time', 'memory_info', 'io_counters', 'num_threads', 'create_time', 'connections', 'open_files', 'status',
'cmdline'])
p_cpus = proc.cpu_times()
process_list.append({
'pid': proc_info['pid'],
'ppid': proc_info['ppid'],
'name': proc_info['name'],
'username': proc_info['username'],
'cpu_percent': self.get_cpu_percent(str(proc_info['pid']), p_cpus, self.new_info['cpu_time']),
'running_time': time.time() - proc_info['create_time'],
'memory_info': proc_info['memory_info'],
'io_info': proc_info['io_counters'],
'num_threads': proc_info['num_threads'],
'create_time': proc_info['create_time'],
'connections_info': proc_info['connections'],
'open_files': proc_info['open_files'],
'ps': self.get_process_ps(proc.name())['data'],
'status': proc_info['status'],
'cmdline': proc_info['cmdline'],
'net_info': self.get_process_network(proc_info['pid'])
})
cache.set(self.old_path, self.new_info, 600)
except:
pass
return public.returnResult(code=1, msg='success', status=True, data=process_list)
except Exception as e:
return public.returnResult(code=0, msg='获取进程列表失败' + str(e), status=False)
# ------------------------------ 获取进程列表 end ------------------------------
# ------------------------------ 获取进程信息 start ------------------------------
@staticmethod
def _format_connections(connects):
result = []
for i in connects:
r_addr = i.raddr
if not i.raddr:
r_addr = ('', 0)
l_addr = i.laddr
if not i.laddr:
l_addr = ('', 0)
result.append({
"fd": i.fd,
"family": i.family,
"local_addr": l_addr[0],
"local_port": l_addr[1],
"client_addr": r_addr[0],
"client_rport": r_addr[1],
"status": i.status
})
return result
@staticmethod
def get_connects(pid: str):
'''
@name 获取进程连接信息
@author hwliang<2021-08-09>
@param pid<int>
@return dict
'''
connects = 0
try:
if pid == 1:
return connects
tp = '/proc/' + str(pid) + '/fd/'
if not os.path.exists(tp):
return connects
for d in os.listdir(tp):
f_name = tp + d
if os.path.islink(f_name):
l = os.readlink(f_name)
if l.find('socket:') != -1:
connects += 1
except:
pass
return connects
def get_process_info_by_pid(self, pid: int) -> dict:
"""
获取进程信息
:param pid:
:return:
"""
try:
status_ps = {'sleeping': '睡眠', 'running': '活动'}
process = psutil.Process(int(pid))
if type(self.new_info) != dict: self.new_info = {}
self.new_info['cpu_time'] = self.get_cpu_time()
self.new_info['time'] = time.time()
self.get_process_net_list()
p_cpus = process.cpu_times()
# 获取连接信息
connections = process.connections()
p_mem = process.memory_full_info()
io_info = process.io_counters()
info = {
'pid': process.pid,
'ppid': process.ppid(),
'name': process.name(),
'threads': process.num_threads(),
'user': process.username(),
'username': process.username(),
'cpu_percent': self.get_cpu_percent(process.pid, p_cpus, self.new_info['cpu_time']),
'memory_info': self.object_to_dict(p_mem),
'memory_used': p_mem.uss,
'io_info': self.object_to_dict(io_info),
"io_write_bytes": io_info.write_bytes,
"io_read_bytes": io_info.read_bytes,
'connections': self._format_connections(connections),
"connects": self.get_connects(str(process.pid)),
'status': status_ps[process.status()] if process.status() in status_ps else process.status(),
'create_time': process.create_time(),
'running_time': process.cpu_times().user + process.cpu_times().system,
'cmdline': process.cmdline(),
'open_files': [self.object_to_dict(i) for i in process.open_files()],
'ps': self.get_process_ps(process.name())['data'],
'net_info': self.get_process_network(process.pid),
"exe": ' '.join(process.cmdline()),
}
cache.set(self.old_path, self.new_info, 600)
return public.returnResult(code=1, msg='success', status=True, data=info)
except Exception as e:
return public.returnResult(code=0, msg='获取进程信息失败' + str(e), status=False)
# 通过name获取进程信息
def get_process_info_by_name(self, name: str) -> dict:
"""
通过name获取进程信息
:param name:
:return:
"""
try:
pids = [i.pid for i in psutil.process_iter(['pid', 'name', 'cmdline']) if i.name() == name]
infos = []
for pid in pids:
try:
info = self.get_process_info_by_pid(pid)
if info['status']:
infos.append(info['data'])
except:
pass
return public.returnResult(code=1, msg='success', status=True, data=infos)
except Exception as e:
return public.returnResult(code=0, msg='获取进程信息失败' + str(e), status=False)
# 通过启动命令获取进程信息
def get_process_info_by_exec(self, cli: str) -> dict:
"""
通过启动命令获取进程信息
:param cli:启动命令
:return:
"""
try:
pids = [i.pid for i in psutil.process_iter(['pid', 'cmdline']) if cli in ' '.join(i.cmdline())]
infos = []
for pid in pids:
try:
info = self.get_process_info_by_pid(pid)
if info['status']:
infos.append(info['data'])
except:
pass
return public.returnResult(code=1, msg='success', status=True, data=infos)
except Exception as e:
return public.returnResult(code=0, msg='获取进程信息失败' + str(e), status=False)
def get_process_info_by_port(self, port: int) -> dict:
"""
通过端口获取进程信息
:param port:
:return:
"""
try:
infos = []
for i in psutil.process_iter(['pid', 'connections']):
for conn in i.connections():
try:
if conn.laddr.port == int(port):
info = self.get_process_info_by_pid(i.pid)
if info['status']:
infos.append(info['data'])
except:
pass
return public.returnResult(code=1, msg='success', status=True, data=infos)
except Exception as e:
return public.returnResult(code=0, msg='获取进程信息失败' + str(e), status=False)
def get_process_info_by_ip(self, ip: str) -> dict:
"""
通过远程ip获取进程信息
:param ip:
:return:
"""
infos = []
try:
for i in psutil.process_iter(['pid', 'connections']):
for conn in i.connections():
try:
if conn.raddr:
if conn.raddr.ip == ip:
info = self.get_process_info_by_pid(i.pid)['data']
if info:
infos.append(info)
except:
pass
return public.returnResult(code=1, msg='success', status=True, data=infos)
except:
return public.returnResult(code=0, msg='获取进程信息失败', status=False, data=infos)
def get_process_info_by_openfile(self, file_path: str) -> dict:
"""
通过打开文件获取进程信息
:param file_path:
:return:
"""
infos = []
try:
for i in psutil.process_iter(['pid', 'open_files']):
try:
for file in i.open_files():
if file.path == file_path:
info = self.get_process_info_by_pid(i.pid)['data']
if info:
infos.append(info)
except:
pass
return public.returnResult(code=1, msg='success', status=True, data=infos)
except:
return public.returnResult(code=0, msg='获取进程信息失败', status=False, data=infos)
# ------------------------------ 获取进程信息 end ------------------------------
# ------------------------------ 获取进程ps start ------------------------------
def get_process_ps(self, name: str) -> dict:
"""
获取进程ps
:param name:
:return:
"""
return public.returnResult(code=1, msg='success', status=True, data=self.ps.get(name, '未知进程'))
# ------------------------------ 获取进程ps end ------------------------------
# ------------------------------ 获取进程树 start ------------------------------
def get_process_tree(self, pid: int) -> dict:
"""
获取进程树
:param pid:
:return:
"""
try:
pid = int(pid)
process = psutil.Process(pid)
process_tree = process.children(recursive=True)
infos = []
info = self.get_process_info_by_pid(pid)
if info['status']:
infos.append(info['data'])
for prc in process_tree:
info = self.get_process_info_by_pid(prc.pid)
if info['status']:
infos.append(info['data'])
return public.returnResult(code=1, msg='success', status=True, data=infos)
except Exception as e:
return public.returnResult(code=0, msg='获取进程树失败' + str(e), status=False)
# ------------------------------ 获取进程树 end ------------------------------
# ------------------------------ 结束进程 start ------------------------------
# 结束进程pid
def kill_pid(self, pid: int) -> dict:
"""
通过关闭进程
:param pid:
:return:
"""
try:
os.kill(pid, 9)
return public.returnResult(code=1, msg='success', status=True, data='')
except Exception as e:
public.ExecShell('kill -9 ' + str(pid))
return public.returnResult(code=1, msg='结束进程失败' + str(e), status=True)
# 结束进程名
def kill_name(self, name: str) -> dict:
"""
通过name关闭进程
:param name:
:return:
"""
try:
os.system('killall ' + name)
return public.returnResult(code=1, msg='success', status=True, data='')
except Exception as e:
return public.returnResult(code=0, msg='结束进程失败' + str(e), status=False)
# 结束进程树
def kill_tree(self, pid: int) -> dict:
"""
通过关闭进程树
:param pid:
:return:
"""
try:
p = psutil.Process(pid)
p.kill()
for i in p.children(recursive=True):
i.kill()
return public.returnResult(code=1, msg='success', status=True, data='')
except Exception as e:
public.ExecShell('kill -9 ' + str(pid))
return public.returnResult(code=1, msg='success', status=True)
# 结束所有进程 pid,进程名,进程树
def kill_proc_all(self, pid: int) -> dict:
"""
结束所有进程
:return:
"""
try:
proc = psutil.Process(pid)
name = proc.name()
self.kill_pid(pid)
self.kill_name(name)
self.kill_tree(pid)
return public.returnResult(code=1, msg='success', status=True, data='')
except Exception as e:
return public.returnResult(code=0, msg='结束进程失败' + str(e), status=False)
def kill_port(self, port: str) -> dict:
"""
结束端口进程
:param port:
:return:
"""
for process in psutil.process_iter(['pid', 'name', 'connections']):
try:
for conn in process.connections():
if conn.laddr.port == int(port):
self.kill_pid(process.pid)
except:
pass
return public.returnResult(code=1, msg='success', status=True, data='')
# ------------------------------ 结束进程 end ------------------------------
# ------------------------------ 拉黑ip start ------------------------------
def add_black_ip(self, ips: list, ) -> dict:
"""
拉黑ip
:param ip:
:return:
"""
try:
if not public.get_firewall_status() == 1: return public.returnMsg(False, '当前系统防火墙未开启')
if [ip for ip in ips if ip in ['0.0.0.0', '127.0.0.0', "::1"]]: return {'status': False, 'msg': '禁止拉黑本机ip', 'data': ''}
for ip in ips:
if not public.check_ip(ip): continue
if public.M('firewall_ip').where("port=?", (ip,)).count() > 0: continue
if self.__isUfw:
if public.is_ipv6(ip):
public.ExecShell('ufw deny from ' + ip + ' to any')
else:
public.ExecShell('ufw insert 1 deny from ' + ip + ' to any')
else:
if self.__isFirewalld:
if public.is_ipv6(ip):
public.ExecShell('firewall-cmd --permanent --add-rich-rule=\'rule family=ipv6 source address="' + ip + '" drop\'')
else:
public.ExecShell('firewall-cmd --permanent --add-rich-rule=\'rule family=ipv4 source address="' + ip + '" drop\'')
else:
if public.is_ipv6(ip): return public.returnMsg(False, 'FIREWALL_IP_FORMAT')
public.ExecShell('iptables -I INPUT -s ' + ip + ' -j DROP')
addtime = time.strftime('%Y-%m-%d %X', time.localtime())
public.M('firewall_ip').add('address,addtime,types', (ip, addtime, 'drop'))
self.firewall_reload()
return public.returnResult(code=1, msg='success', status=True, data='')
except Exception as e:
return public.returnResult(code=0, msg='拉黑失败' + str(e), status=False)
# ------------------------------ 拉黑ip end ------------------------------
# ------------------------------ 取消拉黑ip start ------------------------------
# 删除IP屏蔽
def del_black_ip(self, ips: list) -> dict:
try:
if not public.get_firewall_status() == 1: return public.returnMsg(False, '当前系统防火墙未开启')
for ip in ips:
if not public.check_ip(ip): continue
if self.__isUfw:
public.ExecShell('ufw delete deny from ' + ip + ' to any')
else:
if self.__isFirewalld:
if public.is_ipv6(ip):
public.ExecShell('firewall-cmd --permanent --remove-rich-rule=\'rule family=ipv6 source address="' + ip + '" drop\'')
else:
public.ExecShell('firewall-cmd --permanent --remove-rich-rule=\'rule family=ipv4 source address="' + ip + '" drop\'')
else:
public.ExecShell('iptables -D INPUT -s ' + ip + ' -j DROP')
public.WriteLog("TYPE_FIREWALL", 'FIREWALL_ACCEPT_IP', (ip,))
public.M('firewall_ip').where("address=?", (ip,)).delete()
self.firewall_reload()
return public.returnResult(code=1, msg='success', status=True)
except Exception as e:
return public.returnResult(code=0, msg='删除失败' + str(e), status=False)
# 重载防火墙配置
def firewall_reload(self):
try:
if self.__isUfw:
public.ExecShell('/usr/sbin/ufw reload &')
return public.returnResult(code=1, msg='success', status=True)
if self.__isFirewalld:
public.ExecShell('firewall-cmd --reload &')
else:
public.ExecShell('/etc/init.d/iptables save &')
public.ExecShell('/etc/init.d/iptables restart &')
return public.returnResult(code=1, msg='success', status=True)
except:
return public.returnResult(code=0, msg='重载防火墙失败', status=False)
# ------------------------------ 取消拉黑ip end ------------------------------
# ------------------------------ 获取进程cpu start ------------------------------
# 获取cpu使用率
def get_cpu_percent(self, pid, cpu_times, cpu_time):
self.get_old()
percent = 0.00
process_cpu_time = self.get_process_cpu_time(cpu_times)
if not self.old_info: self.old_info = {}
if not pid in self.old_info:
self.new_info[pid] = {}
self.new_info[pid]['cpu_time'] = process_cpu_time
return percent
try:
percent = round(
100.00 * (process_cpu_time - self.old_info[pid]['cpu_time']) / (cpu_time - self.old_info['cpu_time']), 2)
except:
return 0
self.new_info[pid] = {}
self.new_info[pid]['cpu_time'] = process_cpu_time
if percent > 0: return percent
return 0.00
def get_process_cpu_time(self, cpu_times):
cpu_time = 0.00
for s in cpu_times: cpu_time += s
return cpu_time
def get_old(self):
if self.old_info: return True
data = cache.get(self.old_path)
if not data: return False
self.old_info = data
del (data)
return True
def get_cpu_time(self):
if self.__cpu_time: return self.__cpu_time
self.__cpu_time = 0.00
s = psutil.cpu_times()
self.__cpu_time = s.user + s.system + s.nice + s.idle
return self.__cpu_time
# ------------------------------ 获取进程cpu end ------------------------------
# ------------------------------ 获取进程net start ------------------------------
def get_process_network(self, pid):
'''
@name 获取进程网络流量
@author hwliang<2021-09-13>
@param pid<int> 进程ID
@return tuple
'''
if not self.__process_net_list:
self.get_process_net_list()
if not self.last_net_process_time: return 0, 0, 0, 0
if not pid in self.__process_net_list: return 0, 0, 0, 0
if not pid in self.last_net_process:
return self.__process_net_list[pid]['up'], self.__process_net_list[pid]['up_package'], \
self.__process_net_list[pid]['down'], self.__process_net_list[pid]['down_package']
up = int((self.__process_net_list[pid]['up'] - self.last_net_process[pid]['up']) / (
time.time() - self.last_net_process_time))
down = int((self.__process_net_list[pid]['down'] - self.last_net_process[pid]['down']) / (
time.time() - self.last_net_process_time))
up_package = int((self.__process_net_list[pid]['up_package'] - self.last_net_process[pid]['up_package']) / (
time.time() - self.last_net_process_time))
down_package = int(
(self.__process_net_list[pid]['down_package'] - self.last_net_process[pid]['down_package']) / (
time.time() - self.last_net_process_time))
return up, up_package, down, down_package
def get_process_net_list(self):
w_file = '/dev/shm/bt_net_process'
if not os.path.exists(w_file): return
self.last_net_process = cache.get('net_process')
self.last_net_process_time = cache.get('last_net_process')
net_process_body = public.readFile(w_file)
if not net_process_body: return
net_process = net_process_body.split('\n')
for np in net_process:
if not np: continue
tmp = {}
np_list = np.split()
if len(np_list) < 5: continue
tmp['pid'] = int(np_list[0])
tmp['down'] = int(np_list[1])
tmp['up'] = int(np_list[2])
tmp['down_package'] = int(np_list[3])
tmp['up_package'] = int(np_list[4])
self.__process_net_list[tmp['pid']] = tmp
cache.set('net_process', self.__process_net_list, 600)
cache.set('last_net_process', time.time(), 600)
def get_network(self):
try:
self.get_net_old()
networkIo = psutil.net_io_counters()[:4]
self.new_net_info['upTotal'] = networkIo[0]
self.new_net_info['downTotal'] = networkIo[1]
self.new_net_info['upPackets'] = networkIo[2]
self.new_net_info['downPackets'] = networkIo[3]
self.new_net_info['time'] = time.time()
if not self.old_net_info: self.old_net_info = {}
if not 'upTotal' in self.old_net_info:
time.sleep(0.1)
networkIo = psutil.net_io_counters()[:4]
self.old_net_info['upTotal'] = networkIo[0]
self.old_net_info['downTotal'] = networkIo[1]
self.old_net_info['upPackets'] = networkIo[2]
self.old_net_info['downPackets'] = networkIo[3]
self.old_net_info['time'] = time.time()
s = self.new_net_info['time'] - self.old_net_info['time']
networkInfo = {}
networkInfo['upTotal'] = networkIo[0]
networkInfo['downTotal'] = networkIo[1]
networkInfo['up'] = round((float(networkIo[0]) - self.old_net_info['upTotal']) / s, 2)
networkInfo['down'] = round((float(networkIo[1]) - self.old_net_info['downTotal']) / s, 2)
networkInfo['downPackets'] = networkIo[3]
networkInfo['upPackets'] = networkIo[2]
networkInfo['downPackets_s'] = int((networkIo[3] - self.old_net_info['downPackets']) / s)
networkInfo['upPackets_s'] = int((networkIo[2] - self.old_net_info['upPackets']) / s)
cache.set(self.old_net_path, self.new_net_info, 600)
return networkInfo
except:
return None
def get_net_old(self):
if self.old_net_info: return True
data = cache.get(self.old_net_path)
if not data: return False
if not data: return False
self.old_net_info = data
del (data)
return True
# ------------------------------ 获取进程net end ------------------------------
# ------------------------------ 获取启动项列表 start ------------------------------
def get_run_list(self, search: str = ''):
runFile = ['/etc/rc.local', '/etc/profile', '/etc/inittab', '/etc/rc.sysinit']
runList = []
for rfile in runFile:
if not os.path.exists(rfile): continue
bodyR = self.clear_comments(public.readFile(rfile))
if not bodyR: continue
stat = os.stat(rfile)
accept = str(oct(stat.st_mode)[-3:])
if accept == '644': continue
tmp = {}
tmp['name'] = rfile
tmp['srcfile'] = rfile
tmp['size'] = os.path.getsize(rfile)
tmp['access'] = accept
tmp['ps'] = self.get_run_ps(rfile)
runList.append(tmp)
runlevel = self.get_my_runlevel()
runPath = ['/etc/init.d', '/etc/rc' + runlevel + '.d']
tmpAll = []
islevel = False
for rpath in runPath:
if not os.path.exists(rpath): continue
if runPath[1] == rpath: islevel = True
for f in os.listdir(rpath):
if f[:1] != 'S': continue
filename = rpath + '/' + f
if not os.path.exists(filename): continue
if os.path.isdir(filename): continue
if os.path.islink(filename):
flink = os.readlink(filename).replace('../', '/etc/')
if not os.path.exists(flink): continue
filename = flink
tmp = {}
tmp['name'] = f
if islevel: tmp['name'] = f[3:]
if tmp['name'] in tmpAll: continue
stat = os.stat(filename)
accept = str(oct(stat.st_mode)[-3:])
if accept == '644': continue
tmp['srcfile'] = filename
tmp['access'] = accept
tmp['size'] = os.path.getsize(filename)
tmp['ps'] = self.get_run_ps(tmp['name'])
runList.append(tmp)
tmpAll.append(tmp['name'])
data = {}
data['run_list'] = runList
data['run_level'] = runlevel
if search:
data['run_list'] = self.search_run(data['run_list'], search)
return public.returnResult(code=1, msg='success', status=True, data=data)
# 启动项查询
def search_run(self, data, search):
try:
ldata = []
for i in data:
if search in i['name'] or search in i['srcfile'] or search in i['ps']:
ldata.append(i)
return ldata
except:
return data
# 清除注释
def clear_comments(self, body):
bodyTmp = body.split("\n")
bodyR = ""
for tmp in bodyTmp:
if tmp.startswith('#'): continue
if tmp.strip() == '': continue
bodyR += tmp
return bodyR
# 服务注释
def get_run_ps(self, name):
runPs = {'netconsole': '网络控制台日志', 'network': '网络服务', 'jexec': 'JAVA', 'tomcat8': 'Apache Tomcat',
'tomcat7': 'Apache Tomcat', 'mariadb': 'Mariadb',
'tomcat9': 'Apache Tomcat', 'tomcat': 'Apache Tomcat', 'memcached': 'Memcached缓存器',
'php-fpm-53': 'PHP-5.3', 'php-fpm-52': 'PHP-5.2',
'php-fpm-54': 'PHP-5.4', 'php-fpm-55': 'PHP-5.5', 'php-fpm-56': 'PHP-5.6', 'php-fpm-70': 'PHP-7.0',
'php-fpm-71': 'PHP-7.1',
'php-fpm-72': 'PHP-7.2', 'rsync_inotify': 'rsync实时同步', 'pure-ftpd': 'FTP服务',
'mongodb': 'MongoDB', 'nginx': 'Web服务器(Nginx)',
'httpd': 'Web服务器(Apache)', 'bt': 'YakPanel', 'mysqld': 'MySQL数据库', 'rsynd': 'rsync主服务',
'php-fpm': 'PHP服务', 'systemd': '系统核心服务',
'/etc/rc.local': '用户自定义启动脚本', '/etc/profile': '全局用户环境变量',
'/etc/inittab': '用于自定义系统运行级别', '/etc/rc.sysinit': '系统初始化时调用的脚本',
'sshd': 'SSH服务', 'crond': '计划任务服务', 'udev-post': '设备管理系统', 'auditd': '审核守护进程',
'rsyslog': 'rsyslog服务', 'sendmail': '邮件发送服务', 'blk-availability': 'lvm2相关',
'local': '用户自定义启动脚本', 'netfs': '网络文件系统', 'lvm2-monitor': 'lvm2相关',
'xensystem': 'xen云平台相关', 'iptables': 'iptables防火墙', 'ip6tables': 'iptables防火墙 for IPv6',
'firewalld': 'firewall防火墙'}
if name in runPs: return runPs[name]
return name
# 获取当前运行级别
def get_my_runlevel(self):
try:
runlevel = public.ExecShell('runlevel')[0].split()[1]
except:
runlevel_dict = {"multi-user.target": '3', 'rescue.target': '1', 'poweroff.target': '0',
'graphical.target': '5', "reboot.target": '6'}
r_tmp = public.ExecShell('systemctl get-default')[0].strip()
if r_tmp in runlevel_dict:
runlevel = runlevel_dict[r_tmp]
else:
runlevel = '3'
return runlevel
# ------------------------------ 获取启动项列表 end ------------------------------
class Process(object):
process = RealProcess()
# 获取进程列表
def get_process_list(self):
return self.process.get_process_list()
# 获取进程信息->pid
def get_process_info_by_pid(self, get: Any) -> dict:
if not hasattr(get, 'pid'): return {'status': False, 'msg': '参数错误', 'data': {}}
return self.process.get_process_info_by_pid(get.pid)
# 通过name获取进程信息
def get_process_info_by_name(self, get: Any) -> dict:
if not hasattr(get, 'name'): return {'status': False, 'msg': '参数错误', 'data': {}}
return self.process.get_process_info_by_name(get.name)
def get_process_info_by_exec(self, get: Any) -> dict:
if not hasattr(get, 'cli'): return {'status': False, 'msg': '参数错误', 'data': {}}
return self.process.get_process_info_by_exec(get.cli)
def get_process_info_by_port(self, get: Any) -> dict:
if not hasattr(get, 'port'): return {'status': False, 'msg': '参数错误', 'data': {}}
return self.process.get_process_info_by_port(get.port)
def get_process_info_by_ip(self, get: Any) -> dict:
if not hasattr(get, 'ip'): return {'status': False, 'msg': '参数错误', 'data': {}}
return self.process.get_process_info_by_ip(get.ip)
def get_process_info_by_openfile(self, get: Any) -> dict:
if not hasattr(get, 'file_path'): return {'status': False, 'msg': '参数错误', 'data': {}}
return self.process.get_process_info_by_openfile(get.file_path)
# 获取进程树
def get_process_tree(self, get: Any) -> dict:
if not hasattr(get, 'pid'): return {'status': False, 'msg': '参数错误', 'data': {}}
return self.process.get_process_tree(get.pid)
# 结束进程pid
def kill_pid(self, get: Any) -> dict:
if not hasattr(get, 'pid'): return {'status': False, 'msg': '参数错误', 'data': {}}
return self.process.kill_pid(get.pid)
# 结束进程名
def kill_name(self, get: Any) -> dict:
if not hasattr(get, 'name'): return {'status': False, 'msg': '参数错误', 'data': {}}
return self.process.kill_name(get.name)
# 结束进程树
def kill_tree(self, get: Any) -> dict:
if not hasattr(get, 'pid'): return {'status': False, 'msg': '参数错误', 'data': {}}
return self.process.kill_tree(get.pid)
# 结束所有进程 pid,进程名,进程树
def kill_proc_all(self, get: Any) -> dict:
if not hasattr(get, 'pid'): return {'status': False, 'msg': '参数错误', 'data': {}}
return self.process.kill_proc_all(get.pid)
def kill_port(self, get: Any) -> dict:
if not hasattr(get, 'port'): return {'status': False, 'msg': '参数错误', 'data': {}}
return self.process.kill_port(get.port)
def add_black_ip(self, get: Any) -> dict:
if not hasattr(get, 'ips'): return {'status': False, 'msg': '参数错误', 'data': {}}
return self.process.add_black_ip(get.ips)
def del_black_ip(self, get: Any) -> dict:
if not hasattr(get, 'ips'): return {'status': False, 'msg': '参数错误', 'data': {}}
return self.process.del_black_ip(get.ips)
def get_process_ps(self, get: Any) -> dict:
if not hasattr(get, 'name'): return {'status': False, 'msg': '参数错误', 'data': {}}
return self.process.get_process_ps(get.name)
def get_run_list(self, get: Any) -> dict:
if not hasattr(get, 'search'): return {'status': False, 'msg': '参数错误', 'data': {}}
return self.process.get_run_list(get.search)
if __name__ == "__main__":
p = RealProcess()
print(p.get_computers_use())
# print('========================')
# print(p.get_process_list()['data'])
# print('========================')
# print(p.get_process_info_by_pid(1)['data'])
# print('========================')
# print(p.get_process_info_by_name('systemd'))
# print('========================')
# res = p.get_process_tree(1)
# print(res['data'][1])
# print('========================')
# print(p.kill_pid(1))
# print('========================')
# print(p.kill_name('systemd'))
# print('========================')
# print(p.kill_tree(1))
# print('========================')
# print(p.kill_proc_all(1))
# print('========================')
# print(p.get_process_info_by_exec('nginx'))
# print('========================')
# print(p.get_process_info_by_port(8888))
# print('========================')
# print(p.get_process_ps('nginx'))
# print('========================')
# print(p.get_process_info_by_ip('192.168.168.66'))
# print('========================')
# print(p.add_black_ip(['1.1.1.1']))
# print('========================')
# print(p.del_black_ip(['1.1.1.1']))
# print('========================')

View File

@@ -0,0 +1,133 @@
{
"bioset": "用于处理块设备上的I/O请求的进程",
"BT-MonitorAgent": "YakPanel 监控相关进程 (BT-MonitorAgent)",
"rngd": "一个熵守护的进程",
"master": "用于管理和协调子进程的活动的进程",
"irqbalance": "一个IRQ平衡守护的进程",
"rhsmcertd": "主要用于管理Red Hat订阅证书并维护系统的订阅状态的进程",
"auditd": "是Linux审计系统中用户空间的一个组的进程",
"chronyd": "调整内核中运行的系统时钟和时钟服务器同步的进程",
"qmgr": "PBS管理器的进程",
"oneavd": "YakPanel 微步木马检测 (oneavd)",
"postgres": "PostgreSQL数据库的进程",
"grep": "一个命令行工具的进程",
"lsof": "一个命令行工具的进程",
"containerd-shim-runc-v2": "Docker容器的一个组件的进程",
"pickup": "用于监听Unix域套接字的进程",
"cleanup": "邮件传输代理MTA中的一个组件的进程",
"trivial-rewrite": "邮件传输代理MTA中的一个组件的进程",
"containerd": "docker依赖服务的进程",
"redis-server": "redis服务的进程",
"rcu_sched": "linux系统rcu机制服务的进程",
"jsvc": "YakPanel Tomcat 相关 (jsvc)",
"oneav": "YakPanel 微步木马检测 (oneav)",
"mysqld": "MySQL服务的进程",
"php-fpm": "PHP的子进程",
"php-cgi": "PHP-CGI的进程",
"nginx": "Nginx服务的进程",
"httpd": "Apache服务的进程",
"sshd": "SSH服务的进程",
"pure-ftpd": "FTP服务的进程",
"sftp-server": "SFTP服务的进程",
"mysqld_safe": "MySQL服务的进程",
"firewalld": "防火墙服务的进程",
"Yak-Panel": "YakPanel 主面板进程",
"Yak-Task": "YakPanel 后台任务进程",
"NetworkManager": "网络管理服务的进程",
"svlogd": "日志守护的进程",
"memcached": "Memcached缓存器的进程",
"gunicorn": "YakPanel 相关进程",
"YakPanel": "YakPanel 面板进程",
"baota_coll": "YakPanel 云控主控端进程 (进程名: baota_coll)",
"baota_client": "YakPanel 云控被控端进程 (进程名: baota_client)",
"node": "Node.js程序的进程",
"supervisord": "Supervisor的进程",
"rsyslogd": "rsyslog日志服务的进程",
"crond": "计划任务服务的进程",
"cron": "计划任务服务的进程",
"rsync": "rsync文件同步的进程",
"ntpd": "网络时间同步服务的进程",
"rpc.mountd": "NFS网络文件系统挂载服务的进程",
"sendmail": "sendmail邮件服务的进程",
"postfix": "postfix邮件服务的进程",
"npm": "Node.js NPM管理器的进程",
"PM2": "Node.js PM2进程管理器的进程",
"htop": "htop进程监控软件的进程",
"btpython": "YakPanel 独立 Python 环境 (btpython)",
"btappmanagerd": "应用管理器插件进程 (btappmanagerd)",
"dockerd": "Docker容器管理器的进程",
"docker-proxy": "Docker容器管理器的进程",
"docker-registry": "Docker容器管理器的进程",
"docker-distribution": "Docker容器管理器的进程",
"docker-network": "Docker容器管理器的进程",
"docker-volume": "Docker容器管理器的进程",
"docker-swarm": "Docker容器管理器的进程",
"docker-systemd": "Docker容器管理器的进程",
"docker-containerd": "Docker容器管理器的进程",
"docker-containerd-shim": "Docker容器管理器的进程",
"docker-runc": "Docker容器管理器的进程",
"docker-init": "Docker容器管理器的进程",
"docker-init-systemd": "Docker容器管理器的进程",
"docker-init-upstart": "Docker容器管理器的进程",
"docker-init-sysvinit": "Docker容器管理器的进程",
"docker-init-openrc": "Docker容器管理器的进程",
"docker-init-runit": "Docker容器管理器的进程",
"docker-init-systemd-resolved": "Docker容器管理器的进程",
"rpcbind": "NFS网络文件系统服务的进程",
"dbus-daemon": "D-Bus消息总线守护的进程",
"systemd-logind": "登录管理器的进程",
"systemd-journald": "Systemd日志管理服务的进程",
"systemd-udevd": "系统设备管理服务的进程",
"systemd-timedated": "系统时间日期服务的进程",
"systemd-timesyncd": "系统时间同步服务的进程",
"systemd-resolved": "系统DNS解析服务的进程",
"systemd-hostnamed": "系统主机名服务的进程",
"systemd-networkd": "系统网络管理服务的进程",
"systemd-resolvconf": "系统DNS解析服务的进程",
"systemd-local-resolv": "系统DNS解析服务的进程",
"systemd-sysctl": "系统系统参数服务的进程",
"systemd-modules-load": "系统模块加载服务的进程",
"systemd-modules-restore": "系统模块恢复服务的进程",
"agetty": "TTY登陆验证程序的进程",
"sendmail-mta": "MTA邮件传送代理的进程",
"(sd-pam)": "可插入认证模块的进程",
"polkitd": "授权管理服务的进程",
"mongod": "MongoDB数据库服务的进程",
"mongodb": "MongoDB数据库服务的进程",
"mongodb-mms-monitor": "MongoDB数据库服务的进程",
"mongodb-mms-backup": "MongoDB数据库服务的进程",
"mongodb-mms-restore": "MongoDB数据库服务的进程",
"mongodb-mms-agent": "MongoDB数据库服务的进程",
"mongodb-mms-analytics": "MongoDB数据库服务的进程",
"mongodb-mms-tools": "MongoDB数据库服务的进程",
"mongodb-mms-backup-agent": "MongoDB数据库服务的进程",
"mongodb-mms-backup-tools": "MongoDB数据库服务的进程",
"mongodb-mms-restore-agent": "MongoDB数据库服务的进程",
"mongodb-mms-restore-tools": "MongoDB数据库服务的进程",
"mongodb-mms-analytics-agent": "MongoDB数据库服务的进程",
"mongodb-mms-analytics-tools": "MongoDB数据库服务的进程",
"dhclient": "DHCP协议客户端的进程",
"dhcpcd": "DHCP协议客户端的进程",
"dhcpd": "DHCP服务器的进程",
"isc-dhcp-server": "DHCP服务器的进程",
"isc-dhcp-server6": "DHCP服务器的进程",
"dhcp6c": "DHCP服务器的进程",
"dhcpcd": "DHCP服务器的进程",
"dhcpd": "DHCP服务器的进程",
"avahi-daemon": "Zeroconf守护的进程",
"login": "登录的进程",
"systemd": "系统管理服务的进程",
"systemd-sysv": "系统管理服务的进程",
"systemd-journal-gateway": "系统管理服务的进程",
"systemd-journal-remote": "系统管理服务的进程",
"systemd-journal-upload": "系统管理服务的进程",
"systemd-networkd": "系统网络管理服务的进程",
"rpc.idmapd": "NFS网络文件系统相关服务的进程",
"cupsd": "打印服务的进程",
"cups-browsed": "打印服务的进程",
"sh": "shell的进程",
"php": "PHP CLI模式的进程",
"blkmapd": "NFS映射服务的进程",
"lsyncd": "文件同步服务的进程",
"sleep": "延迟的进程"
}

755
mod/base/process/server.py Normal file
View File

@@ -0,0 +1,755 @@
# coding: utf-8
# -------------------------------------------------------------------
# yakpanel
# -------------------------------------------------------------------
# Copyright (c) 2015-2099 yakpanel(http://www.yakpanel.com) All rights reserved.
# -------------------------------------------------------------------
# Author: sww <sww@yakpanel.com>
# -------------------------------------------------------------------
import json
import os
# ------------------------------
# 服务模型
# ------------------------------
import sys, re
import time
import traceback
if "/www/server/panel/class" not in sys.path:
sys.path.insert(0, "/www/server/panel/class")
os.chdir("/www/server/panel")
import public
import glob
# 关闭系统加固执行函数后打开
def syssafe_admin(func):
def wrapper(*args, **kwargs):
syssafe_flag = 0
# 检查系统加固并且关闭
if os.path.exists('/www/server/panel/plugin/syssafe/init.sh'):
res = public.ExecShell('/www/server/panel/plugin/syssafe/init.sh status')
if 'already running' in res[0]:
try:
syssafe_flag = 1
public.ExecShell('/www/server/panel/plugin/syssafe/init.sh stop')
res = public.ExecShell('/www/server/panel/plugin/syssafe/init.sh status')
if 'already running' in res[0]:
import PluginLoader
PluginLoader.plugin_run('syssafe', 'set_open', public.to_dict_obj({'status': 0}))
print('已关闭系统加固!')
except:
pass
e = None
result = None
try:
result = func(*args, **kwargs)
except Exception as ex:
e= ex
try:
if syssafe_flag:
public.ExecShell('/www/server/panel/plugin/syssafe/init.sh stop')
res = public.ExecShell('/www/server/panel/plugin/syssafe/init.sh status')
if 'already running' not in res[0]:
import PluginLoader
PluginLoader.plugin_run('syssafe', 'set_open', public.to_dict_obj({'status': 1}))
print('已开启系统加固!')
except:
pass
if e is not None:
raise e
return result
return wrapper
class RealServer:
server_list = ['mysqld_safe', 'redis-server', 'mongod', 'postgres', 'nginx', 'memcached', 'httpd', 'pure-ftpd', 'jsvc', 'dockerd']
system_info = None
# --------------------- 常用服务管理 start----------------------
def server_admin(self, server_name: str, option: str) -> dict:
"""
服务管理
:param server_name:'mysqld_safe', 'redis-server', 'mongod', 'postgres', 'nginx', 'memcached', 'httpd', 'pure-ftpd', 'jsvc', 'dockerd'
:param option: start,stop,restart
:return:
"""
servers = {
"mongod": self.__mongod_admin,
"redis-server": self.__redis_admin,
"memcached": self.__memcached_admin,
"dockerd": self.__docker_admin,
"jsvc": self.__tomcat_admin,
"pure-ftpd": self.__ftp_admin,
"httpd": self.__apache_admin,
"mysqld_safe": self.__mysqld_admin,
"nginx": self.__nginx_admin,
"postgres": self.__pgsql_admin,
}
from system import system
self.syst = system()
if server_name in self.server_list:
res = servers[server_name](option)
return public.returnResult(code=1, msg=res['msg'], status=res['status'])
else:
return public.returnResult(code=0, msg='operation failure Parameter does not exist', status=False)
def __mongod_admin(self, option: str) -> dict:
try:
Command = {"start": "/etc/init.d/mongodb start",
"stop": "/etc/init.d/mongodb stop", }
if option != 'restart':
public.ExecShell(Command.get(option))
return public.returnMsg(True, 'operate successfully!')
public.ExecShell(Command.get('stop'))
public.ExecShell(Command.get('start'))
return public.returnMsg(True, 'operate successfully!')
except:
return public.returnMsg(False, 'operation failure')
def __redis_admin(self, option: str) -> dict:
try:
get = public.dict_obj()
get.name = 'redis'
get.type = option
return self.syst.serverAdmin(get)
except:
return public.returnMsg(False, 'operation failure')
def __memcached_admin(self, option: str) -> dict:
try:
get = public.dict_obj()
get.name = 'memcached'
get.type = option
return self.syst.serverAdmin(get)
except:
return public.returnMsg(False, 'operation failure')
def __docker_admin(self, option: str) -> dict:
try:
exec_str = 'systemctl {} docker.socket'.format(option)
public.ExecShell(exec_str)
return public.returnMsg(True, "operate successfully")
except:
return public.returnMsg(False, 'operation failure')
def __tomcat_admin(self, option: str) -> dict:
try:
get = public.dict_obj()
get.name = 'tomcat'
get.type = option
self.syst.serverAdmin(get)
return public.returnMsg(True, 'operate successfully!')
except:
return public.returnMsg(False, 'operation failure')
def __ftp_admin(self, option: str) -> dict:
try:
get = public.dict_obj()
get.name = 'pure-ftpd'
get.type = option
return self.syst.serverAdmin(get)
except:
return public.returnMsg(False, 'operation failure')
def __apache_admin(self, option: str) -> dict:
try:
get = public.dict_obj()
get.name = 'apache'
get.type = option
res = self.syst.serverAdmin(get)
import time
time.sleep(1)
return res
except:
return public.returnMsg(False, 'operation failure')
def __mysqld_admin(self, option: str) -> dict:
try:
get = public.dict_obj()
get.name = 'mysqld'
get.type = option
return self.syst.serverAdmin(get)
except:
return public.returnMsg(False, 'operation failure')
def __nginx_admin(self, option: str) -> dict:
try:
get = public.dict_obj()
get.name = 'nginx'
get.type = option
return self.syst.serverAdmin(get)
except:
return public.returnMsg(False, 'operation failure')
def __pgsql_admin(self, option: str) -> dict:
try:
get = public.dict_obj()
get.name = 'pgsql'
get.type = option
return self.syst.serverAdmin(get)
except:
return public.returnMsg(False, 'operation failure')
# ----------------------常用服务管理 end----------------------
# ----------------------常用服务状态 start----------------------
def server_status(self, server_name: str) -> dict:
"""
服务状态
:param server_name: 'mysqld_safe', 'redis-server', 'mongod', 'postgres', 'nginx', 'memcached', 'httpd', 'pure-ftpd', 'jsvc', 'dockerd'
:return:
"""
try:
if server_name in self.server_list:
res = self.__get_status(server_name)
return public.returnResult(code=1, msg=res['msg'], data=res['data'], status=res['status'])
else:
return public.returnResult(code=0, msg='operation failure Parameter does not exist', status=False)
except Exception as e:
return public.returnResult(code=0, msg='operation failure', status=False)
def __is_installation(self, name: str) -> bool:
map = {
"mysqld_safe": "mysqld",
"redis-server": "redis",
"mongod": "mongodb",
"postgres": "pgsql",
"nginx": "nginx",
"memcached": "memcached",
"httpd": "httpd",
"pure-ftpd": "pure-ftpd",
"jsvc": "tomcat",
"dockerd": "docker",
"php": "php",
"tamper_proof": "tamper_proof",
"bt_security": "bt_security",
"syssafe": "syssafe",
}
import glob
dir_path = '/etc/init.d/'
files = [os.path.basename(f) for f in glob.glob(dir_path + "*")]
if name == "dockerd":
res = public.ExecShell('docker -v')[0]
if 'version' in res:
return True
return False
if name == "postgres":
res = public.ExecShell('/www/server/pgsql/bin/psql --version')[0]
pgsql = False
if 'PostgreSQL' in res:
pgsql = True
Manager = False
if os.path.exists('/www/server/panel/plugin/pgsql_manager'):
Manager = True
return {'pgsql': pgsql, 'Manager': Manager}
if name == "php":
php_l = [i for i in files if name in i.lower()]
if len(php_l) != 0:
return True
if name == "tamper_proof":
return os.path.exists('/www/server/panel/plugin/tamper_proof')
if name == "bt_security":
return os.path.exists('/www/server/panel/plugin/bt_security')
if name == "syssafe":
return os.path.exists('/www/server/panel/plugin/syssafe')
if map[name] in files:
return True
return False
def __get_status(self, server_name: str) -> dict:
try:
if not self.__is_installation(server_name):
return {'status': True, 'msg': '', 'data': {'install': False, 'status': False}}
res = public.ExecShell('ps -ef|grep {}|grep -v grep'.format(server_name))[0]
if 'mongod' in res:
return {'status': True, 'msg': '', 'data': {'install': True, 'status': True}}
return {'status': True, 'msg': '', 'data': {'install': True, 'status': False}}
except:
return {'status': False, 'msg': '获取失败!', 'data': {'install': False, 'status': False}}
# ----------------------常用服务状态 end----------------------
# ---------------------- 通用服务管理 start----------------------
def universal_server_admin(self, server_name: str, option: str) -> dict:
"""
通用服务管理 服务器在/etc/init.d/目录下有同名的启动文件且启动文件中有start,stop,restart,status命令
:param server_name: 服务名称
:param option: start,stop,restart
:return:
"""
try:
get = public.dict_obj()
get.name = server_name
get.type = option
dir_path = '/etc/init.d/'
files = [os.path.basename(f) for f in glob.glob(dir_path + "*")]
if server_name in files:
res = public.ExecShell('/etc/init.d/{} {}'.format(server_name, option))
if 'is running' in res[0].lower() or 'is active' in res[0].lower() or 'already running' in res[0].lower():
return public.returnResult(code=1, msg='operate successfully!', status=True)
if 'is stopped' in res[0].lower() or 'is not running' in res[0].lower():
return public.returnResult(code=1, msg='operate successfully!', status=True)
else:
return public.returnResult(code=0, msg='operation failure The service was not found in /etc/init.d/', status=False)
except:
return public.returnResult(code=0, msg='operation failure', status=False)
# ---------------------- 通用服务管理 end----------------------
# ---------------------- 通用服务状态 start----------------------
def universal_server_status(self, server_name: str) -> dict:
"""
通用服务状态 服务器在/etc/init.d/目录下有同名的启动文件且启动文件中有status命令status中有输出is running或is active
:param server_name: 服务名称
:return:
"""
try:
get = public.dict_obj()
get.name = server_name
get.type = 'status'
dir_path = '/etc/init.d/'
files = [os.path.basename(f) for f in glob.glob(dir_path + "*")]
if server_name in files:
res = public.ExecShell('/etc/init.d/{} status'.format(server_name))
if 'is running' in res[0].lower() or 'is active' in res[0].lower() or 'already running' in res[0].lower():
return public.returnResult(code=1, msg='运行中', data=True)
return public.returnResult(code=1, msg='未运行', data=False)
return public.returnResult(code=0, msg='服务不存在!', status=False)
except:
return public.returnResult(code=0, msg='获取失败!', data=False)
# ---------------------- 通用服务状态 end----------------------
# ---------------------- 添加开机自启 启动脚本 start----------------------
# 添加开机自启
@syssafe_admin
def add_boot(self, server_name: str, pid_file: str, start_exec: str, stop_exec: str, default_start: str = '2 3 4 5') -> dict:
"""
添加开机自启
:param server_name: 服务名称
:param pid_file: 启动pid记录文件
:param start_exec: 启动命令
:param stop_exec: 停止命令
:param default_start: 默认启动级别
:return:
"""
content = """
#! /bin/sh
# chkconfig: 2345 55 25
### BEGIN INIT INFO
# Provides: {name}
# Required-Start: $all
# Required-Stop: $all
# Default-Start: {default_start}
# Default-Stop: 0 1 6
# Short-Description: {name}
# Description: {name}
### END INIT INFO
# Author: licess
# website: http://www.yakpanel.com
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
case "$1" in
start)
echo -n "Starting {name}... "
if [ -f {pid_file} ];then
mPID=$(cat {pid_file})
isStart=`ps ax | awk '{{ print $1 }}' | grep -e "^${{mPID}}$"`
if [ "$isStart" != "" ];then
echo "{name} (pid $mPID) already running."
exit 1
fi
fi
nohup {start_exec} &
if [ $? != 0 ]; then
echo " failed"
exit 1
else
pid=`ps -ef|grep "{start_exec}" |grep -v grep|awk '{{print $2}}'`
echo $! > {pid_file}
echo " done"
fi
;;
stop)
echo -n "Stopping {name}... "
if [ -f {pid_file} ];then
mPID=$(cat {pid_file})
isStart = `ps ax | awk '{{ print $1 }}' | grep -e "^${{mPID}}$"`
if [ "$isStart" = "" ];then
echo "{name} is stopped"
exit 1
fi
else
echo "{name} is stopped"
exit 1
fi
nohup {stop_exec} &
if [ $? != 0 ]; then
echo " failed. Use force-quit"
exit 1
else
echo " done"
fi
;;
status)
if [ -f {pid_file} ];then
mPID=`cat {pid_file}`
isStart=`ps ax | awk '{{ print $1 }}' | grep -e "^${{mPID}}$"`
if [ "$isStart" != '' ];then
echo "{name} (pid `pidof {name}`) is running."
exit 1
else
echo "{name} is stopped"
exit 0
fi
else
echo "{name} is stopped"
exit 0
fi
;;
restart)
$0 stop
sleep 1
$0 start
;;
esac
""".format(name=server_name, pid_file=pid_file, start_exec=start_exec, stop_exec=stop_exec, default_start=default_start)
if os.path.exists(os.path.join('/etc/init.d/', server_name)):
return public.returnResult(code=1, msg='operation failure Service already exists', status=False)
try:
public.writeFile(os.path.join('/etc/init.d/', server_name), content)
os.chmod(os.path.join('/etc/init.d/', server_name), 0o777)
if os.path.exists('/usr/sbin/update-rc.d'):
public.ExecShell('update-rc.d -f {} defaults'.format(server_name))
else:
public.ExecShell('systemctl enable {}'.format(server_name))
return public.returnResult(code=1, msg='operate successfully!', status=True)
except:
return public.returnResult(code=0, msg='operation failure', status=False)
# ---------------------- 添加开机自启 启动脚本 end----------------------
# ---------------------- 删除开机自启 启动脚本 start----------------------
def del_boot(self, server_name: str) -> dict:
"""
删除启动脚本
:param server_name: 服务名称
:return:
"""
try:
if os.path.exists(os.path.join('/etc/init.d/', server_name)):
if os.path.exists('/usr/sbin/update-rc.d'):
public.ExecShell('update-rc.d -f {} remove'.format(server_name))
else:
public.ExecShell('systemctl disable {}'.format(server_name))
os.remove(os.path.join('/etc/init.d/', server_name))
return public.returnResult(code=1, msg='operate successfully!', status=True)
return public.returnResult(code=0, msg='operation failure Service does not exist', status=False)
except:
return public.returnResult(code=0, msg='operation failure', status=False)
# ---------------------- 删除开机自启 启动脚本 end----------------------
# ---------------------- 创建服务守护进程 start----------------------
@syssafe_admin
def create_daemon(self, server_name: str,
pid_file: str,
start_exec: str,
workingdirectory: str,
stop_exec: str = None,
user: str = 'root',
is_power_on: int = 1,
logs_file: str = '',
environments: str = '',
is_fork=None,
restart_type='always',
fork_time_out=20) -> dict:
"""
创建服务守护进程
:param server_name: 服务名称
:param pid_file: 启动pid记录文件
:param start_exec: 启动命令
:param stop_exec: 停止命令
:return:
"""
# 检查系统加固插件是否存在
try:
if not stop_exec:
stop_exec = '/usr/bin/pkill -9 "{}"'.format(start_exec)
content = '''
[Unit]
Description={server_name}
After=network.target
[Service]
{environments}
ExecStart={start_exec}
ExecStop={stop_exec}
WorkingDirectory={workingdirectory}
Restart={restart_type}
SyslogIdentifier={server_name}
User={user}
Type=simple
PrivateTmp=false
PIDFile={pid_file}
[Install]
WantedBy=multi-user.target
'''.format(
start_exec=start_exec,
workingdirectory=workingdirectory,
user=user,
pid_file=pid_file,
server_name=server_name,
environments=environments,
restart_type=restart_type,
stop_exec=stop_exec
)
exe_shell = ''
if is_fork or is_fork is None:
content = content.replace('Type=simple', 'Type=forking')
if not os.path.exists('/usr/lib/systemd/system/'):
os.makedirs('/usr/lib/systemd/system/')
public.writeFile('/usr/lib/systemd/system/{}.service'.format(server_name), content)
if is_power_on:
exe_shell += 'systemctl enable {}\n'.format(server_name) + " && "
exe_shell += 'systemctl daemon-reload' + " && "
if not logs_file:
logs_file = '/www/wwwlogs/project_{}.log'.format(server_name)
rsyslog_conf = public.readFile('/etc/rsyslog.conf')
add_conf = "if $programname == '{}' then {}\n".format(server_name, logs_file)
if rsyslog_conf:
idx = rsyslog_conf.find("if $programname == '{}' then".format(server_name))
if idx == -1:
rsyslog_conf += "\n" + add_conf
else:
line_idx = rsyslog_conf.find('\n', idx)
rsyslog_conf = rsyslog_conf[:idx] + add_conf + rsyslog_conf[line_idx:]
public.writeFile('/etc/rsyslog.conf', rsyslog_conf)
exe_shell += 'systemctl restart rsyslog' + " && "
if not os.path.exists(logs_file):
exe_shell += 'touch {}'.format(logs_file) + ' && '
exe_shell += 'chown -R {user}:{user} {logs_file}'.format(user=user, logs_file=logs_file) + ' && '
if is_fork is not None:
exe_shell += 'systemctl restart {}'.format(server_name)
public.ExecShell(exe_shell)
return public.returnResult(code=1, msg='operate successfully!', status=True)
public.ExecShell(exe_shell)
import subprocess,psutil
try:
start_time = time.time()
process = subprocess.Popen(["systemctl", "restart", server_name], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
while True:
try:
p = psutil.Process(process.pid)
print(p.status())
# 检查进程的状态
if p.status() == psutil.STATUS_ZOMBIE:
break
except:
pass
if process.poll() is not None:
break
if time.time() - start_time > fork_time_out:
raise
time.sleep(0.1)
except:
content = content.replace('Type=forking','Type=simple')
public.writeFile('/usr/lib/systemd/system/{}.service'.format(server_name), content)
public.ExecShell('systemctl daemon-reload && systemctl restart {}'.format(server_name))
return public.returnResult(code=1, msg='operate successfully!', status=True)
except:
return public.returnResult(code=0, msg='operation failure', status=False)
# ---------------------- 创建服务守护进程 end----------------------
# ---------------------- 删除服务守护进程 start----------------------
@syssafe_admin
def del_daemon(self, server_name: str) -> dict:
"""
删除服务守护进程
:param server_name: 服务名称
:return:
"""
try:
public.ExecShell('systemctl stop {}'.format(server_name))
if os.path.exists('/usr/lib/systemd/system/{}.service'.format(server_name)):
public.ExecShell('systemctl disable {}'.format(server_name))
os.remove('/usr/lib/systemd/system/{}.service'.format(server_name))
public.ExecShell('systemctl daemon-reload')
public.ExecShell(r'sed -i "/if \$programname == {}/d" /etc/rsyslog.conf'.format(server_name))
public.ExecShell('systemctl restart rsyslog')
return public.returnResult(code=1, msg='operate successfully!', status=True)
return public.returnResult(code=0, msg='operation failure', status=False)
except:
return public.returnResult(code=0, msg='operation failure', status=False)
# ---------------------- 删除服务守护进程 end----------------------
# ---------------------- 服务守护进程状态 start----------------------
def daemon_status(self, server_name: str) -> dict:
"""
服务守护进程状态
:param server_name: 服务名称
:return:
"""
try:
if not os.path.exists('/usr/lib/systemd/system/{}.service'.format(server_name)):
return public.returnResult(code=0, msg='服务不存在!', status=False)
if not self.system_info:
self.system_info = public.ExecShell("systemctl |grep service|grep -E 'active|deactivating'|awk '{print $1}'")[0]
if server_name+'.service' in self.system_info:
return public.returnResult(code=1, msg='运行中', status=True)
return public.returnResult(code=1, msg='未运行', status=False)
except:
return public.returnResult(code=0, msg='operation failure', status=False)
# ---------------------- 服务守护进程状态 end----------------------
def daemon_admin(self, server_name: str,action:str) -> dict:
"""
:param server_name: 项目名称
:param action: 操作
"""
public.ExecShell('systemctl {} {}'.format(action,server_name))
return public.returnResult(code=1, msg='操作指令已执行', status=True)
# if action == 'start' or action == 'restart':
# num = 0
# for i in range(5):
# time.sleep(0.01)
# if self.daemon_status(server_name)['status']:
# num += 1
# if num > 3:
# return public.returnResult(code=1, msg='启动成功!', status=True)
# return public.returnResult(code=0, msg='启动失败!', status=False)
# return public.returnResult(code=1, msg='关闭成功!' + res[0] + res[1], status=True)
def get_daemon_pid(self, server_name: str) -> dict:
"""
获取守护进程pid
:param server_name: 项目名称
"""
res = public.ExecShell("systemctl show --property=MainPID {}".format(server_name))[0] # type: str
if not res.startswith('MainPID='):
return public.returnResult(code=0, msg='获取失败!', status=False)
try:
pid = int(res.split("=", 1)[1])
return public.returnResult(code=1, msg='获取成功!', data=pid, status=True)
except:
return public.returnResult(code=0, msg='获取失败', status=False)
# ---------------------- 延时定时启动 start----------------------
def add_task(self, shell: str, time: int) -> dict:
"""
服务定时启动
:param server_name: 服务名称
:param start_exec: 启动命令
:param minute: 定时启动时间
:return:
"""
data = {
'type': 3,
'time': time,
'name': shell,
'title': '',
'fun': '',
'args': ''
}
res = public.set_tasks_run(data)
if res['status']:
return public.returnResult(code=1, msg='operate successfully!', status=True)
return public.returnResult(code=0, msg='operation failure', status=False)
# ---------------------- 服务定时启动 end----------------------
class Server:
server = RealServer()
def server_admin(self, get):
try:
if hasattr(self.server, get.name):
return getattr(self.server, get.name)(get.type)
return public.returnMsg(False, 'operation failure Parameter does not exist')
except:
return public.returnMsg(False, 'operation failure')
def server_status(self, get):
try:
if hasattr(self.server, get.name):
return getattr(self.server, get.name)()
return public.returnMsg(False, 'operation failure Parameter does not exist')
except:
return public.returnMsg(False, 'operation failure')
def universal_server_admin(self, get):
try:
return self.server.universal_server_admin(get.name, get.type)
except:
return public.returnMsg(False, 'operation failure')
def universal_server_status(self, get):
try:
return self.server.universal_server_status(get.name)
except:
return public.returnMsg(False, 'operation failure')
def add_boot(self, get):
try:
return self.server.add_boot(get.name, get.pid_file, get.start_exec, get.stop_exec)
except:
return public.returnMsg(False, 'operation failure')
def del_boot(self, get):
try:
return self.server.del_boot(get.name)
except:
return public.returnMsg(False, 'operation failure')
def create_daemon(self, get):
try:
return self.server.create_daemon(get.name, get.pid_file, get.start_exec, get.user)
except:
return public.returnMsg(False, 'operation failure')
def del_daemon(self, get):
try:
return self.server.del_daemon(get.name)
except:
return public.returnMsg(False, 'operation failure')
def daemon_status(self, get):
try:
return self.server.daemon_status(get.name)
except:
return public.returnMsg(False, 'operation failure')
def add_task(self, get):
try:
return self.server.add_task(get.shell, get.time)
except:
return public.returnMsg(False, 'operation failure')

630
mod/base/process/user.py Normal file
View File

@@ -0,0 +1,630 @@
# coding: utf-8
# -------------------------------------------------------------------
# yakpanel
# -------------------------------------------------------------------
# Copyright (c) 2015-2099 yakpanel(http://www.yakpanel.com) All rights reserved.
# -------------------------------------------------------------------
# Author: sww <sww@yakpanel.com>
# -------------------------------------------------------------------
import json
import os
# ------------------------------
# 用户模型
# ------------------------------
import sys
import traceback
import psutil
if "/www/server/panel/class" not in sys.path:
sys.path.insert(0, "/www/server/panel/class")
os.chdir("/www/server/panel")
import public
from typing import List, Dict, Any, Union
class RealUser:
def __init__(self):
self.groupList = self.get_group_list()['data']
print(self.groupList)
# --------------------获取用户列表 Start--------------------
def get_user_list(self, search: str = '') -> Dict[str, Union[bool, str, List[Dict[str, Any]]]]:
"""
获取用户列表
:param search: 搜索关键词 可搜索 用户名、备注、shell、home、用户组
:return: list
"""
try:
tmpList = public.readFile('/etc/passwd').split("\n")
userList = []
for ul in tmpList:
tmp = ul.split(':')
if len(tmp) < 6: continue
userInfo = {}
userInfo['username'] = tmp[0]
userInfo['uid'] = tmp[2]
userInfo['gid'] = tmp[3]
userInfo['group'] = self._get_group_name(tmp[3])
userInfo['ps'] = self._get_user_ps(tmp[0], tmp[4])
userInfo['home'] = tmp[5]
userInfo['login_shell'] = tmp[6]
userList.append(userInfo)
if search != '':
userList = self._search_user(userList, search)
return public.returnResult(code=1, data=userList, msg='获取用户列表成功!', status=True)
except Exception as e:
print(traceback.format_exc())
return public.returnResult(code=0, data=[], msg='获取用户列表失败!错误:' + str(e), status=False)
def _get_user_ps(self, name: str, ps: str) -> str:
"""
获取用户备注
:param name: 用户名
:param ps: 备注
:return: str
"""
userPs = {'www': 'YakPanel', 'root': '超级管理员', 'mysql': '用于运行MySQL的用户',
'mongo': '用于运行MongoDB的用户',
'git': 'git用户', 'mail': 'mail', 'nginx': '第三方nginx用户', 'postfix': 'postfix邮局用户',
'lp': '打印服务帐号',
'daemon': '控制后台进程的系统帐号', 'nobody': '匿名帐户', 'bin': '管理大部分命令的帐号',
'adm': '管理某些管理文件的帐号', 'smtp': 'smtp邮件'}
if name in userPs: return userPs[name]
if not ps: return name
return ps
def _get_group_name(self, gid: str) -> str:
"""
获取用户组名称
:param gid: 用户组ID
:return: str
"""
for g in self.groupList:
if g['gid'] == gid: return g['group']
return ''
def _search_user(self, data: List[Dict[str, Any]], search: str) -> List[Dict[str, Union[str, Any]]]:
"""
搜索用户
:param data: 用户列表
:param search: 搜索关键词
:return: list
"""
try:
ldata = []
for i in data:
if search in i['username'] or search in i['ps'] or search in i['login_shell'] or search in i['home'] or search in i['group']:
ldata.append(i)
return ldata
except:
return data
def get_group_list(self):
"""
获取用户组列表
:return:list
"""
tmpList = public.readFile('/etc/group').split("\n")
groupList = []
for gl in tmpList:
tmp = gl.split(':')
if len(tmp) < 3: continue
groupInfo = {}
groupInfo['group'] = tmp[0]
groupInfo['gid'] = tmp[2]
groupList.append(groupInfo)
return public.returnResult(code=1, data=groupList, msg='获取用户组列表成功!', status=True)
# --------------------获取用户列表 End----------------------
# --------------------删除用户 Start------------------------
def remove_user(self, user: str) -> Dict[str, Any]:
users = ['www', 'root', 'mysql', 'shutdown', 'postfix', 'smmsp', 'sshd', 'systemd-network', 'systemd-bus-proxy',
'avahi-autoipd', 'mail', 'sync', 'lp', 'adm', 'bin', 'mailnull', 'ntp', 'daemon', 'sys']
if user in users: return public.returnResult(code=0, msg='系统用户或关键用户不能删除!', status=False)
r = public.ExecShell("userdel " + user)
if r[1].find('process') != -1:
try:
pid = r[1].split()[-1]
p = psutil.Process(int(pid))
pname = p.name()
p.kill()
public.ExecShell("pkill -9 " + pname)
r = public.ExecShell("userdel " + user)
public.ExecShell("rm -rf /home/" + user)
except:
pass
if r[1].find('userdel:') != -1: return public.returnMsg(False, r[1])
return public.returnResult(code=1, msg='删除成功!', status=True)
# --------------------删除用户 End------------------------
# --------------------添加用户 Start------------------------
def add_user(self, user: str, pwd: str, group: str) -> Dict[str, Any]:
try:
if not user: return public.returnResult(code=0, msg='用户名不能为空!', status=False)
if not pwd: return public.returnResult(code=0, msg='密码不能为空!', status=False)
if not self._check_user(user): return public.returnResult(code=1, msg='用户已存在!', status=True)
if not self._check_group(group): self.add_group(group)
r = public.ExecShell("useradd -g " + group + " -m " + user + ' -p' + pwd)
if r[1].find('useradd:') != -1 and r[1].find('already exists') == 1: return public.returnResult(code=0, msg=r[1], status=False)
# public.ExecShell("echo \"" + user + ":" + pwd + "\" | chpasswd")
return public.returnResult(code=1, msg='Successfully added!', status=True)
except:
print(traceback.format_exc())
return public.returnResult(code=0, msg='添加失败!', status=False)
def _check_user(self, user: str) -> bool:
"""
检查用户是否存在
:param user: 用户名
:return: bool
"""
tmpList = public.readFile('/etc/passwd').split("\n")
for ul in tmpList:
tmp = ul.split(':')
if len(tmp) < 6: continue
if tmp[0] == user: return False
return True
def _check_group(self, group: str) -> bool:
"""
检查用户组是否存在
:param group: 用户组
:return: bool
"""
tmpList = public.readFile('/etc/group').split("\n")
for gl in tmpList:
tmp = gl.split(':')
if len(tmp) < 3: continue
if tmp[0] == group: return True
return False
# --------------------添加用户 End------------------------
# --------------------修改用户密码 Start------------------------
def edit_user_pwd(self, user: str, pwd: str) -> Dict[str, Any]:
try:
if not user: return public.returnResult(code=0, msg='用户名不能为空!', status=False)
if not pwd: return public.returnResult(code=0, msg='密码不能为空!', status=False)
if self._check_user(user): return public.returnResult(code=0, msg='用户不存在!', status=False)
public.ExecShell("echo \"" + user + ":" + pwd + "\" | chpasswd")
return public.returnResult(code=1, msg='修改成功!', status=True)
except:
return public.returnResult(code=0, msg='修改失败!', status=False)
# --------------------修改用户密码 End------------------------
# --------------------修改用户组 Start------------------------
def edit_user_group(self, user: str, group: str) -> Dict[str, Any]:
try:
if not user: return public.returnMsg(False, '用户名不能为空!')
if not group: return public.returnMsg(False, '用户组不能为空!')
if self._check_user(user): return public.returnMsg(False, '用户不存在!')
if not self._check_group(group): self.add_group(group)
r = public.ExecShell("usermod -g " + group + " " + user)
if r[1].find('usermod:') != -1: return public.returnMsg(False, r[1])
return public.returnResult(code=1, msg='修改成功!', status=True)
except:
return public.returnResult(code=0, msg='修改失败!', status=False)
# --------------------修改用户组 End------------------------
# --------------------修改用户备注 Start------------------------
def edit_user_ps(self, user: str, ps: str) -> Dict[str, Any]:
try:
if not user: return public.returnResult(code=0, msg='用户名不能为空!', status=False)
if self._check_user(user): return public.returnResult(code=0, msg='用户不存在!', status=False)
r = public.ExecShell("usermod -c " + ps + " " + user)
if r[1].find('usermod:') != -1: return public.returnResult(code=0, msg=r[1], status=False)
return public.returnResult(code=1, msg='修改成功!', status=True)
except:
return public.returnResult(code=0, msg='修改失败!', status=False)
# --------------------修改用户备注 End------------------------
# --------------------修改用户备注 Start------------------------
def edit_user_status(self, user: str, status: int):
try:
if not user: return public.returnResult(code=0, msg='用户名不能为空!', status=False)
if self._check_user(user): return public.returnResult(code=0, msg='用户不存在!', status=False)
if int(status) == 1:
r = public.ExecShell("usermod -L " + user)
else:
r = public.ExecShell("usermod -U " + user)
if r[1].find('usermod:') != -1: return public.returnResult(code=0, msg=r[1], status=False)
return public.returnResult(code=1, msg='修改成功!', status=True)
except:
return public.returnResult(code=0, msg='修改失败!', status=False)
# --------------------修改用户备注 End------------------------
# --------------------修改用户登录Shell Start------------------------
def edit_user_login_shell(self, user: str, login_shell: str) -> Dict[str, Any]:
try:
if not user: return public.returnResult(code=0, msg='用户名不能为空!', status=False)
if self._check_user(user): return public.returnResult(code=0, msg='用户不存在!', status=False)
r = public.ExecShell("usermod -s " + login_shell + " " + user)
if r[1].find('usermod:') != -1: return public.returnResult(code=0, msg=r[1], status=False)
return public.returnResult(code=1, msg='修改成功!', status=True)
except:
return public.returnResult(code=0, msg='修改失败!', status=False)
# --------------------修改用户登录Shell End------------------------
# --------------------修改用户家目录 Start------------------------
def edit_user_home(self, user: str, home: str) -> Dict[str, Any]:
try:
if not user: return public.returnResult(code=0, msg='用户名不能为空!', status=False)
if self._check_user(user): return public.returnResult(code=0, msg='用户不存在!', status=False)
r = public.ExecShell("usermod -d " + home + " " + user)
if r[1].find('usermod:') != -1: return public.returnResult(code=0, msg=r[1], status=False)
return public.returnResult(code=1, msg='修改成功!', status=True)
except:
return public.returnResult(code=0, msg='修改失败!', status=False)
# --------------------修改用户家目录 End------------------------
# --------------------获取用户信息 Start------------------------
def get_user_info(self, user: str) -> Dict[str, Any]:
try:
user = user.strip()
tmpList = public.readFile('/etc/passwd').split("\n")
userInfo = {}
for ul in tmpList:
tmp = ul.split(':')
if len(tmp) < 6: continue
if tmp[0] == user:
userInfo['username'] = tmp[0]
userInfo['uid'] = tmp[2]
userInfo['gid'] = tmp[3]
userInfo['group'] = self._get_group_name(tmp[3])
userInfo['ps'] = self._get_user_ps(tmp[0], tmp[4])
userInfo['home'] = tmp[5]
userInfo['login_shell'] = tmp[6]
break
return public.returnResult(code=1, data=userInfo, msg='获取用户信息成功!', status=True)
except:
print(traceback.format_exc())
return public.returnResult(code=0, msg='获取用户信息失败!', status=False)
# --------------------添加用户组 Start------------------------
def add_group(self, group: str) -> Dict[str, Any]:
"""
添加用户组
:param group: 用户组
:return: dict
"""
try:
if not group: return public.returnResult(code=0, msg='用户组不能为空!', status=False)
if self._check_group(group): return public.returnResult(code=0, msg='用户组已存在!', status=False)
r = public.ExecShell("groupadd " + group)
if r[1].find('groupadd:') != -1: return public.returnResult(code=0, msg=r[1], status=False)
return public.returnResult(code=1, msg='Successfully added!', status=True)
except:
return public.returnResult(code=0, msg='添加失败!', status=False)
# --------------------添加用户组 End------------------------
# --------------------删除用户组 Start------------------------
def remove_group(self, group: str) -> Dict[str, Any]:
"""
删除用户组
:param group: 用户组
:return: dict
"""
try:
if not group: return public.returnResult(code=0, msg='用户组不能为空!', status=False)
if not self._check_group(group): return public.returnResult(code=0, msg='用户组不存在!', status=False)
r = public.ExecShell("groupdel " + group)
if r[1].find('groupdel:') != -1: return public.returnResult(code=0, msg=r[1], status=False)
return public.returnResult(code=1, msg='删除成功!', status=True)
except:
return public.returnResult(code=0, msg='删除失败!', status=False)
# --------------------删除用户组 End------------------------
# --------------------修改用户组名称 Start------------------------
def edit_group_name(self, group: str, new_group: str) -> Dict[str, Any]:
"""
修改用户组名称
:param group: 用户组
:param new_group: 新用户组
:return: dict
"""
try:
if not group: return public.returnResult(code=0, msg='用户组不能为空!', status=False)
if not new_group: return public.returnResult(code=0, msg='新用户组不能为空!', status=False)
if not self._check_group(group): return public.returnResult(code=0, msg='用户组不存在!', status=False)
if self._check_group(new_group): return public.returnResult(code=0, msg='新用户组已存在!', status=False)
r = public.ExecShell("groupmod -n " + new_group + " " + group)
if r[1].find('groupmod:') != -1: return public.returnResult(code=0, msg=r[1], status=False)
return public.returnResult(code=1, msg='修改成功!', status=True)
except:
return public.returnResult(code=0, msg='修改失败!', status=False)
# --------------------获取用户组列表 End------------------------
# --------------------获取用户组信息 Start------------------------
def get_group_info(self, group) -> Dict[str, Any]:
"""
获取用户组信息
:param group: 用户组
:return: dict
"""
try:
group = group.strip()
tmpList = public.readFile('/etc/group').split("\n")
groupInfo = {}
for gl in tmpList:
tmp = gl.split(':')
if len(tmp) < 3: continue
if tmp[0] == group:
groupInfo['group'] = tmp[0]
groupInfo['gid'] = tmp[2]
break
return public.returnResult(code=1, data=groupInfo, msg='获取用户组信息成功!', status=True)
except:
return public.returnResult(code=0, msg='获取用户组信息失败!', status=False)
# --------------------获取用户组信息 End------------------------
# --------------------获取用户组信息 Start------------------------
def get_group_user(self, group: str) -> Dict[str, Any]:
"""
获取用户组用户
:param group: 用户组
:return: dict
"""
try:
group = group.strip()
tmpList = self.get_user_list()['data']
userList = []
for ul in tmpList:
if ul['group'] == group:
userList.append(ul['username'])
return public.returnResult(code=1, data=userList, msg='获取用户组用户成功!', status=True)
except:
return public.returnResult(code=0, msg='获取用户组用户失败!', status=False)
# --------------------获取用户组信息 End------------------------
# --------------------获取用户组信息 Start------------------------
def get_user_group(self, user: str) -> Dict[str, Any]:
"""
获取用户组用户
:param user: 用户
:return: dict
"""
try:
user = user.strip()
tmpList = self.get_user_list()['data']
groupList = []
for gl in tmpList:
if gl['username'] == user:
groupList.append(gl['group'])
return public.returnResult(code=1, data=groupList, msg='获取用户组用户成功!', status=True)
except:
return public.returnResult(code=0, msg='获取用户组用户失败!', status=False)
# --------------------获取用户组信息 End------------------------
# --------------------修改用户权限 Start------------------------
def edit_user_permission(self, user: str, permission: str) -> Dict[str, Any]:
"""
修改用户权限
:param user:
:param permission:
:return:
"""
try:
if not user: return public.returnResult(code=0, msg='用户名不能为空!', status=False)
if self._check_user(user): return public.returnResult(code=0, msg='用户不存在!', status=False)
r = public.ExecShell("chmod -R " + permission + " /home/" + user)
if r[1].find('chmod:') != -1: return public.returnResult(code=0, msg=r[1], status=False)
return public.returnResult(code=1, msg='修改成功!', status=True)
except:
return public.returnResult(code=0, msg='修改失败!', status=False)
# --------------------修改用户权限 End------------------------
# --------------------修改用户组权限 Start------------------------
def edit_group_permission(self, group: str, permission: str) -> Dict[str, Any]:
"""
修改用户组权限
:param group:
:param permission:
:return:
"""
try:
if not group: return public.returnResult(code=0, msg='用户组不能为空!', status=False)
if not self._check_group(group): return public.returnResult(code=0, msg='用户组不存在!', status=False)
r = public.ExecShell("chmod -R " + permission + " /home/" + group)
if r[1].find('chmod:') != -1: return public.returnResult(code=0, msg=r[1], status=False)
return public.returnResult(code=1, msg='修改成功!', status=True)
except:
return public.returnResult(code=0, msg='修改失败!', status=False)
def edit_user_name(self, user: str, new_user: str) -> Dict[str, Any]:
try:
user = user.strip()
new_user = new_user.strip()
r = public.ExecShell("usermod -l " + new_user + " " + user)
if r[1].find('usermod:') != -1: return public.returnResult(code=0, msg=r[1], status=False)
return public.returnResult(code=1, msg='修改成功!', status=True)
except:
return public.returnResult(code=0, msg='修改失败!', status=False)
class User(object):
def __init__(self):
self.real_user = RealUser()
# 获取用户列表
def get_user_list(self, get):
search = ''
if hasattr(get, 'search'):
search = get.search
return self.real_user.get_user_list(search)
# 删除用户
def remove_user(self, get):
if not hasattr(get, 'user'):
return public.returnMsg(False, '用户不存在!')
user = get.user.strip()
return self.real_user.remove_user(user)
# 添加用户
def add_user(self, get):
if not hasattr(get, 'user'):
return public.returnMsg(False, '用户名不能为空!')
if not hasattr(get, 'pwd'):
return public.returnMsg(False, '密码不能为空!')
if not hasattr(get, 'group'):
return public.returnMsg(False, '用户组不能为空!')
user = get.user.strip()
pwd = get.pwd.strip()
group = get.group.strip()
return self.real_user.add_user(user, pwd, group)
# 修改用户密码
def edit_user_pwd(self, get):
if not hasattr(get, 'user'):
return public.returnMsg(False, '用户名不能为空!')
if not hasattr(get, 'pwd'):
return public.returnMsg(False, '密码不能为空!')
user = get.user.strip()
pwd = get.pwd.strip()
return self.real_user.edit_user(user, pwd)
# 修改用户的用户组
def edit_user_group(self, get):
if not hasattr(get, 'user'):
return public.returnMsg(False, '用户名不能为空!')
if not hasattr(get, 'group'):
return public.returnMsg(False, '用户组不能为空!')
user = get.user.strip()
group = get.group.strip()
return self.real_user.edit_group(user, group)
# 修改用户备注
def edit_user_ps(self, get):
if not hasattr(get, 'user'):
return public.returnMsg(False, '用户名不能为空!')
user = get.user.strip()
return self.real_user.edit_user_ps(user)
# 添加用户组
def add_group(self, get):
if not hasattr(get, 'group'):
return public.returnMsg(False, '用户组不能为空!')
group = get.group.strip()
return self.real_user.add_group(group)
# 删除用户组
def remove_group(self, get):
if not hasattr(get, 'group'):
return public.returnMsg(False, '用户组不能为空!')
group = get.group.strip()
return self.real_user.remove_group(group)
# 修改用户组名称
def edit_group_name(self, get):
if not hasattr(get, 'group'):
return public.returnMsg(False, '用户组不能为空!')
if not hasattr(get, 'new_group'):
return public.returnMsg(False, '新用户组不能为空!')
group = get.group.strip()
new_group = get.new_group.strip()
return self.real_user.edit_group_name(group, new_group)
# 获取用户组列表
def get_group_list(self, get):
return self.real_user.get_group_list()
# 获取用户组信息
def get_group_info(self, get):
if not hasattr(get, 'group'):
return public.returnMsg(False, '用户组不能为空!')
group = get.group.strip()
return self.real_user.get_group_info(group)
# 获取用户组用户
def get_group_user(self, get):
if not hasattr(get, 'group'):
return public.returnMsg(False, '用户组不能为空!')
group = get.group.strip()
return self.real_user.get_group_user(group)
# 获取用户组用户
def get_user_group(self, get):
if not hasattr(get, 'user'):
return public.returnMsg(False, '用户不能为空!')
user = get.user.strip()
return self.real_user.get_user_group(user)
# 修改用户备注
def edit_ps(self, get):
if not hasattr(get, 'user'):
return public.returnMsg(False, '用户名不能为空!')
if not hasattr(get, 'ps'):
return public.returnMsg(False, '备注不能为空!')
user = get.user.strip()
ps = get.ps.strip()
return self.real_user.edit_ps(user, ps)
# 修改用户登录Shell
def edit_user_login_shell(self, get):
if not hasattr(get, 'user'):
return public.returnMsg(False, '用户名不能为空!')
if not hasattr(get, 'login_shell'):
return public.returnMsg(False, '登录Shell不能为空!')
user = get.user.strip()
login_shell = get.login_shell.strip()
return self.real_user.edit_login_shell(user, login_shell)
# 修改用户家目录
def edit_user_home(self, get):
if not hasattr(get, 'user'):
return public.returnMsg(False, '用户名不能为空!')
if not hasattr(get, 'home'):
return public.returnMsg(False, '家目录不能为空!')
user = get.user.strip()
home = get.home.strip()
return self.real_user.edit_home(user, home)
# 修改用户权限
def edit_user_permission(self, get):
if not hasattr(get, 'user'):
return public.returnMsg(False, '用户名不能为空!')
if not hasattr(get, 'permission'):
return public.returnMsg(False, '权限不能为空!')
user = get.user.strip()
permission = get.permission.strip()
return self.real_user.edit_user_permission(user, permission)
# 修改用户组权限
def edit_group_permission(self, get):
if not hasattr(get, 'group'):
return public.returnMsg(False, '用户组不能为空!')
if not hasattr(get, 'permission'):
return public.returnMsg(False, '权限不能为空!')
group = get.group.strip()
permission = get.permission.strip()
return self.real_user.edit_group_permission(group, permission)
def edit_user_name(self, get):
if not hasattr(get, 'user'):
return public.returnMsg(False, '用户名不能为空!')
if not hasattr(get, 'new_user'):
return public.returnMsg(False, '新用户名不能为空!')
user = get.user.strip()
new_user = get.new_user.strip()
return self.real_user.edit_user_name(user, new_user)
if __name__ == "__main__":
user = User()
print(user.get_user_list(public.to_dict_obj({})))

View File

@@ -0,0 +1,86 @@
User类
返回值默认类型:
Dict[str, Any]
{
'status': bool,
'msg': str,
'data': Any
}
def get_user_list(self, search: str = '') -> Dict[str, Union[bool, str, List[Dict[str, Any]]]]:
# 获取用户列表
# 传参search可选参数搜索关键词
def _get_user_ps(self, name: str, ps: str) -> str:
# 获取用户备注
# 传参name用户名ps备注
def _get_group_name(self, gid: str) -> str:
# 获取用户组名称
# 传参gid用户组ID
def _search_user(self, data: List[Dict[str, Any]], search: str) -> List[Dict[str, Union[str, Any]]]:
# 搜索用户
# 传参data用户列表search搜索关键词
def _get_group_list(self) -> List[Dict[str, Union[str, str]]]:
# 获取用户组列表
def remove_user(self, user: str) -> Dict[str, Any]:
# 删除用户
# 传参user用户名
def add_user(self, user: str, pwd: str, group: str) -> Dict[str, Any]:
# 添加用户
# 传参user用户名pwd密码group用户组
def edit_user(self, user: str, pwd: str) -> Dict[str, Any]:
# 修改用户密码
# 传参user用户名pwd新密码
def edit_group(self, user: str, group: str) -> Dict[str, Any]:
# 修改用户组
# 传参user用户名group新用户组
def edit_ps(self, user: str, ps: str) -> Dict[str, Any]:
# 修改用户备注
# 传参user用户名ps新备注
def edit_login_shell(self, user: str, login_shell: str) -> Dict[str, Any]:
# 修改用户登录Shell
# 传参user用户名login_shell新Shell
def edit_home(self, user: str, home: str) -> Dict[str, Any]:
# 修改用户家目录
# 传参user用户名home新家目录
def get_user_info(self, user: str) -> Dict[str, Any]:
# 获取用户信息
# 传参user用户名
def add_group(self, group: str) -> Dict[str, Any]:
# 添加用户组
# 传参group用户组
def remove_group(self, group: str) -> Dict[str, Any]:
# 删除用户组
# 传参group用户组
def edit_group_name(self, group: str, new_group: str) -> Dict[str, Any]:
# 修改用户组名称
# 传参group用户组new_group新用户组
def get_group_list(self) -> Dict[str, Union[bool, str, List[Dict[str, Any]]]]:
# 获取用户组列表
def get_group_info(self, group) -> Dict[str, Any]:
# 获取用户组信息
# 传参group用户组
def get_group_user(self, group: str) -> Dict[str, Any]:
# 获取用户组用户
# 传参group用户组
def get_user_group(self, user: str) -> Dict[str, Any]:
# 获取用户组用户
# 传参user用户