mdserver-web/class/core/mw.py

919 lines
23 KiB
Python
Raw Normal View History

2018-11-08 03:53:01 -05:00
# coding: utf-8
import os
import sys
import time
import string
import json
import hashlib
import shlex
import datetime
import subprocess
import re
import db
from random import Random
2018-11-20 06:11:09 -05:00
def execShell(cmdstring, cwd=None, timeout=None, shell=True):
if shell:
cmdstring_list = cmdstring
else:
cmdstring_list = shlex.split(cmdstring)
if timeout:
end_time = datetime.datetime.now() + datetime.timedelta(seconds=timeout)
sub = subprocess.Popen(cmdstring_list, cwd=cwd, stdin=subprocess.PIPE,
shell=shell, bufsize=4096, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
while sub.poll() is None:
time.sleep(0.1)
if timeout:
if end_time <= datetime.datetime.now():
raise Exception("Timeout%s" % cmdstring)
2021-05-01 05:42:19 -04:00
if sys.version_info[0] == 2:
return sub.communicate()
2021-05-01 05:16:47 -04:00
data = sub.communicate()
# python3 fix 返回byte数据
if isinstance(data[0], bytes):
t1 = str(data[0], encoding='utf-8')
if isinstance(data[1], bytes):
t2 = str(data[1], encoding='utf-8')
return (t1, t2)
2018-11-20 06:11:09 -05:00
2018-11-08 03:53:01 -05:00
def getRunDir():
return os.getcwd()
2018-11-12 01:38:35 -05:00
def getRootDir():
return os.path.dirname(os.path.dirname(getRunDir()))
2018-11-08 03:53:01 -05:00
2018-12-07 01:18:30 -05:00
def getPluginDir():
return getRunDir() + '/plugins'
def getServerDir():
return getRootDir() + '/server'
2018-12-17 05:16:59 -05:00
def getWwwDir():
2019-01-18 03:05:21 -05:00
file = getRunDir() + '/data/site.pl'
if os.path.exists(file):
return readFile(file).strip()
2018-12-17 05:16:59 -05:00
return getRootDir() + '/wwwroot'
2019-01-18 03:05:21 -05:00
def setWwwDir(wdir):
file = getRunDir() + '/data/site.pl'
return writeFile(file, wdir)
2018-12-18 00:17:37 -05:00
def getLogsDir():
return getRootDir() + '/wwwlogs'
2019-01-02 00:58:53 -05:00
def getBackupDir():
return getRootDir() + '/backup'
2019-01-18 03:05:21 -05:00
def setBackupDir(bdir):
file = getRunDir() + '/data/backup.pl'
2019-01-19 04:47:27 -05:00
return writeFile(file, bdir)
2019-01-18 03:05:21 -05:00
2018-11-20 06:11:09 -05:00
def getOs():
2018-12-27 04:22:11 -05:00
return sys.platform
2018-11-20 06:11:09 -05:00
2018-12-18 00:17:37 -05:00
def isAppleSystem():
if getOs() == 'darwin':
return True
return False
2018-12-27 06:38:15 -05:00
def deleteFile(file):
if os.path.exists(file):
os.remove(file)
2018-12-18 06:37:46 -05:00
def isInstalledWeb():
path = getServerDir() + '/openresty/nginx/sbin/nginx'
if os.path.exists(path):
return True
return False
2018-12-19 02:41:00 -05:00
def restartWeb():
if isInstalledWeb():
initd = getServerDir() + '/openresty/init.d/openresty'
2021-11-11 04:12:11 -05:00
execShell(initd + ' ' + 'restart')
2018-12-19 02:41:00 -05:00
2019-02-21 06:13:39 -05:00
def restartMw():
import system_api
system_api.system_api().restartMw()
2019-02-09 23:27:17 -05:00
def checkWebConfig():
op_dir = getServerDir() + '/openresty'
cmd = "ulimit -n 10240 && " + op_dir + \
"/nginx/sbin/nginx -t -c " + op_dir + "/nginx/conf/nginx.conf"
result = execShell(cmd)
searchStr = 'successful'
if result[1].find(searchStr) == -1:
2019-02-21 01:35:37 -05:00
msg = getInfo('配置文件错误: {1}', (result[1],))
writeLog("软件管理", msg)
2019-02-09 23:27:17 -05:00
return result[1]
return True
2018-11-08 03:53:01 -05:00
def M(table):
sql = db.Sql()
return sql.table(table)
2018-11-09 02:46:28 -05:00
def getPage(args, result='1,2,3,4,5,8'):
2018-12-11 00:29:27 -05:00
data = getPageObject(args, result)
return data[0]
def getPageObject(args, result='1,2,3,4,5,8'):
2018-11-08 03:53:01 -05:00
# 取分页
import page
# 实例化分页类
page = page.Page()
info = {}
2018-11-09 02:46:28 -05:00
info['count'] = 0
2021-05-01 02:17:42 -04:00
if 'count' in args:
2018-11-09 02:46:28 -05:00
info['count'] = int(args['count'])
2018-11-08 03:53:01 -05:00
info['row'] = 10
2021-05-01 02:17:42 -04:00
if 'row' in args:
2018-12-10 04:27:09 -05:00
info['row'] = int(args['row'])
2018-11-08 03:53:01 -05:00
info['p'] = 1
2021-05-01 02:17:42 -04:00
if 'p' in args:
2018-11-11 22:41:00 -05:00
info['p'] = int(args['p'])
2018-11-08 03:53:01 -05:00
info['uri'] = {}
info['return_js'] = ''
2021-05-01 02:17:42 -04:00
if 'tojs' in args:
2018-11-09 03:23:17 -05:00
info['return_js'] = args['tojs']
2018-11-08 03:53:01 -05:00
2018-12-11 00:29:27 -05:00
return (page.GetPage(info, result), page)
2018-11-08 03:53:01 -05:00
def md5(str):
# 生成MD5
try:
m = hashlib.md5()
2021-05-01 02:17:42 -04:00
m.update(str.encode("utf-8"))
2018-11-08 03:53:01 -05:00
return m.hexdigest()
2021-05-01 02:17:42 -04:00
except Exception as ex:
2018-11-08 03:53:01 -05:00
return False
2018-11-25 10:30:19 -05:00
def getFileMd5(filename):
2018-11-08 03:53:01 -05:00
# 文件的MD5值
if not os.path.isfile(filename):
return False
myhash = hashlib.md5()
f = file(filename, 'rb')
while True:
b = f.read(8096)
if not b:
break
myhash.update(b)
f.close()
return myhash.hexdigest()
2018-11-25 10:30:19 -05:00
def getRandomString(length):
2018-11-08 03:53:01 -05:00
# 取随机字符串
str = ''
chars = 'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz0123456789'
chrlen = len(chars) - 1
random = Random()
for i in range(length):
str += chars[random.randint(0, chrlen)]
return str
def getJson(data):
import json
return json.dumps(data)
2018-12-25 04:37:08 -05:00
def returnData(status, msg, data=None):
2018-12-25 02:41:46 -05:00
return {'status': status, 'msg': msg, 'data': data}
2018-12-25 02:12:59 -05:00
2018-12-25 04:37:08 -05:00
def returnJson(status, msg, data=None):
2019-02-18 11:01:30 -05:00
# if data == None:
# return {'status': status, 'msg': msg}
# return {'status': status, 'msg': msg, 'data': data}
2019-01-18 02:46:59 -05:00
if data == None:
2019-02-12 22:28:47 -05:00
return getJson({'status': status, 'msg': msg})
return getJson({'status': status, 'msg': msg, 'data': data})
2018-11-08 03:53:01 -05:00
def returnMsg(status, msg, args=()):
# 取通用字曲返回
2018-12-19 00:54:19 -05:00
pjson = 'static/language/' + getLanguage() + '/public.json'
logMessage = json.loads(readFile(pjson))
2018-11-08 03:53:01 -05:00
keys = logMessage.keys()
2018-11-11 10:51:11 -05:00
2018-11-08 03:53:01 -05:00
if msg in keys:
msg = logMessage[msg]
for i in range(len(args)):
rep = '{' + str(i + 1) + '}'
msg = msg.replace(rep, args[i])
2018-11-13 01:18:06 -05:00
return {'status': status, 'msg': msg, 'data': args}
2018-11-08 03:53:01 -05:00
2018-11-25 21:55:53 -05:00
def getInfo(msg, args=()):
# 取提示消息
for i in range(len(args)):
rep = '{' + str(i + 1) + '}'
msg = msg.replace(rep, args[i])
return msg
2018-11-08 03:53:01 -05:00
def getMsg(key, args=()):
# 取提示消息
try:
logMessage = json.loads(
2018-11-11 10:51:11 -05:00
readFile('static/language/' + getLanguage() + '/public.json'))
2018-11-08 03:53:01 -05:00
keys = logMessage.keys()
msg = None
if key in keys:
msg = logMessage[key]
for i in range(len(args)):
rep = '{' + str(i + 1) + '}'
msg = msg.replace(rep, args[i])
return msg
except:
return key
def getLan(key):
# 取提示消息
logMessage = json.loads(
2018-11-11 10:51:11 -05:00
readFile('static/language/' + getLanguage() + '/template.json'))
2018-11-08 03:53:01 -05:00
keys = logMessage.keys()
msg = None
if key in keys:
msg = logMessage[key]
return msg
def readFile(filename):
# 读文件内容
try:
fp = open(filename, 'r')
fBody = fp.read()
fp.close()
return fBody
2021-05-01 02:17:42 -04:00
except Exception as e:
2018-11-08 03:53:01 -05:00
return False
def getDate():
# 取格式时间
import time
return time.strftime('%Y-%m-%d %X', time.localtime())
2018-11-11 10:51:11 -05:00
def getLanguage():
2018-11-08 03:53:01 -05:00
path = 'data/language.pl'
if not os.path.exists(path):
return 'Simplified_Chinese'
return readFile(path).strip()
def writeLog(type, logMsg, args=()):
# 写日志
try:
import time
import db
import json
sql = db.Sql()
mDate = time.strftime('%Y-%m-%d %X', time.localtime())
data = (type, logMsg, mDate)
result = sql.table('logs').add('type,log,addtime', data)
2019-02-13 13:11:48 -05:00
except Exception as e:
2022-03-23 03:21:35 -04:00
pass
2018-11-08 03:53:01 -05:00
def writeFile(filename, str):
# 写文件内容
try:
fp = open(filename, 'w+')
fp.write(str)
fp.close()
return True
except:
return False
2019-01-18 14:05:55 -05:00
def HttpGet(url, timeout=10):
"""
发送GET请求
@url 被请求的URL地址(必需)
@timeout 超时时间默认60秒
return string
"""
if sys.version_info[0] == 2:
2018-11-08 03:53:01 -05:00
try:
2019-01-18 14:05:55 -05:00
import urllib2
import ssl
if sys.version_info[0] == 2:
reload(urllib2)
reload(ssl)
try:
ssl._create_default_https_context = ssl._create_unverified_context
except:
pass
response = urllib2.urlopen(url, timeout=timeout)
return response.read()
except Exception as ex:
return str(ex)
else:
try:
import urllib.request
import ssl
try:
ssl._create_default_https_context = ssl._create_unverified_context
except:
pass
response = urllib.request.urlopen(url, timeout=timeout)
result = response.read()
if type(result) == bytes:
result = result.decode('utf-8')
return result
except Exception as ex:
return str(ex)
2019-02-26 02:30:21 -05:00
def HttpGet2(url, timeout):
2021-11-12 06:17:28 -05:00
import urllib.request
2019-02-26 02:30:21 -05:00
2019-03-04 06:45:06 -05:00
try:
2021-11-12 06:17:28 -05:00
req = urllib.request.urlopen(url, timeout=timeout)
result = req.read().decode('utf-8')
2019-03-04 06:45:06 -05:00
return result
except Exception as e:
return str(e)
2019-02-26 02:30:21 -05:00
2019-01-18 14:05:55 -05:00
def httpGet(url, timeout=10):
2019-02-26 02:30:21 -05:00
return HttpGet2(url, timeout)
2019-01-18 14:05:55 -05:00
def HttpPost(url, data, timeout=10):
"""
发送POST请求
@url 被请求的URL地址(必需)
@data POST参数可以是字符串或字典(必需)
@timeout 超时时间默认60秒
return string
"""
if sys.version_info[0] == 2:
2018-11-08 03:53:01 -05:00
try:
2019-01-18 14:05:55 -05:00
import urllib
import urllib2
import ssl
2018-11-08 03:53:01 -05:00
ssl._create_default_https_context = ssl._create_unverified_context
2019-01-18 14:05:55 -05:00
data = urllib.urlencode(data)
req = urllib2.Request(url, data)
response = urllib2.urlopen(req, timeout=timeout)
return response.read()
except Exception as ex:
return str(ex)
else:
try:
import urllib.request
import ssl
try:
ssl._create_default_https_context = ssl._create_unverified_context
except:
pass
data = urllib.parse.urlencode(data).encode('utf-8')
req = urllib.request.Request(url, data)
response = urllib.request.urlopen(req, timeout=timeout)
result = response.read()
if type(result) == bytes:
result = result.decode('utf-8')
return result
except Exception as ex:
return str(ex)
def httpPost(url, data, timeout=10):
return HttpPost(url, data, timeout)
2018-11-08 03:53:01 -05:00
def writeSpeed(title, used, total, speed=0):
# 写进度
if not title:
data = {'title': None, 'progress': 0,
'total': 0, 'used': 0, 'speed': 0}
else:
progress = int((100.0 * used / total))
data = {'title': title, 'progress': progress,
'total': total, 'used': used, 'speed': speed}
writeFile('/tmp/panelSpeed.pl', json.dumps(data))
return True
def getSpeed():
# 取进度
path = getRootDir()
2019-02-19 04:29:07 -05:00
data = readFile(path + '/tmp/panelSpeed.pl')
2018-11-08 03:53:01 -05:00
if not data:
data = json.dumps({'title': None, 'progress': 0,
'total': 0, 'used': 0, 'speed': 0})
2019-02-19 04:29:07 -05:00
writeFile(path + '/tmp/panelSpeed.pl', data)
2018-11-08 03:53:01 -05:00
return json.loads(data)
def getLastLine(inputfile, lineNum):
# 读文件指定倒数行数
try:
fp = open(inputfile, 'r')
lastLine = ""
lines = fp.readlines()
count = len(lines)
if count > lineNum:
num = lineNum
else:
num = count
i = 1
lastre = []
for i in range(1, (num + 1)):
if lines:
n = -i
lastLine = lines[n].strip()
fp.close()
lastre.append(lastLine)
result = ''
num -= 1
while num >= 0:
result += lastre[num] + "\n"
num -= 1
return result
except:
return getMsg('TASK_SLEEP')
2019-01-10 13:13:40 -05:00
def getNumLines(path, num, p=1):
pyVersion = sys.version_info[0]
try:
2021-11-22 16:03:55 -05:00
import html
2019-01-10 13:13:40 -05:00
if not os.path.exists(path):
return ""
start_line = (p - 1) * num
count = start_line + num
fp = open(path, 'rb')
buf = ""
fp.seek(-1, 2)
if fp.read(1) == "\n":
fp.seek(-1, 2)
data = []
b = True
n = 0
for i in range(count):
while True:
newline_pos = str.rfind(str(buf), "\n")
pos = fp.tell()
if newline_pos != -1:
if n >= start_line:
line = buf[newline_pos + 1:]
try:
2021-11-22 16:03:55 -05:00
data.insert(0, html.escape(line))
except Exception as e:
2019-01-10 13:13:40 -05:00
pass
buf = buf[:newline_pos]
n += 1
break
else:
if pos == 0:
b = False
break
to_read = min(4096, pos)
fp.seek(-to_read, 1)
t_buf = fp.read(to_read)
if pyVersion == 3:
if type(t_buf) == bytes:
t_buf = t_buf.decode('utf-8')
buf = t_buf + buf
fp.seek(-to_read, 1)
if pos - to_read == 0:
buf = "\n" + buf
if not b:
break
fp.close()
except Exception as e:
return ''
return "\n".join(data)
2018-11-08 03:53:01 -05:00
def downloadFile(url, filename):
import urllib
urllib.urlretrieve(url, filename=filename, reporthook=downloadHook)
def downloadHook(count, blockSize, totalSize):
speed = {'total': totalSize, 'block': blockSize, 'count': count}
2021-04-30 23:45:29 -04:00
print('%02d%%' % (100.0 * count * blockSize / totalSize))
2018-11-08 03:53:01 -05:00
2018-12-10 01:22:22 -05:00
def getLocalIp():
2018-11-08 03:53:01 -05:00
# 取本地外网IP
try:
import re
filename = 'data/iplist.txt'
ipaddress = readFile(filename)
2022-06-12 12:43:23 -04:00
if not ipaddress or ipaddress == '127.0.0.1':
import urllib
2018-11-08 03:53:01 -05:00
url = 'http://pv.sohu.com/cityjson?ie=utf-8'
2022-06-12 12:43:23 -04:00
req = urllib.request.urlopen(url, timeout=10)
content = req.read().decode('utf-8')
2021-05-08 12:40:14 -04:00
ipaddress = re.search('\d+.\d+.\d+.\d+', content).group(0)
2018-11-08 03:53:01 -05:00
writeFile(filename, ipaddress)
ipaddress = re.search('\d+.\d+.\d+.\d+', ipaddress).group(0)
return ipaddress
2022-06-12 12:43:23 -04:00
except Exception as ex:
# print(ex)
2018-12-12 12:28:54 -05:00
return '127.0.0.1'
2018-11-08 03:53:01 -05:00
def inArray(arrays, searchStr):
# 搜索数据中是否存在
for key in arrays:
if key == searchStr:
return True
return False
def checkIp(ip):
# 检查是否为IPv4地址
import re
p = re.compile(
'^((25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[01]?\d\d?)$')
if p.match(ip):
return True
else:
return False
def checkPort(port):
# 检查端口是否合法
2019-02-11 04:57:29 -05:00
ports = ['21', '25', '443', '7200', '8080', '888', '8888', '8443']
2018-11-08 03:53:01 -05:00
if port in ports:
return False
intport = int(port)
if intport < 1 or intport > 65535:
return False
return True
def getStrBetween(startStr, endStr, srcStr):
# 字符串取中间
start = srcStr.find(startStr)
if start == -1:
return None
end = srcStr.find(endStr)
if end == -1:
return None
return srcStr[start + 1:end]
def getCpuType():
# 取CPU类型
cpuinfo = open('/proc/cpuinfo', 'r').read()
rep = "model\s+name\s+:\s+(.+)"
tmp = re.search(rep, cpuinfo)
cpuType = None
if tmp:
cpuType = tmp.groups()[0]
return cpuType
2018-11-20 06:11:09 -05:00
def isRestart():
2018-11-08 03:53:01 -05:00
# 检查是否允许重启
num = M('tasks').where('status!=?', ('1',)).count()
if num > 0:
return False
return True
2018-12-04 05:55:27 -05:00
def isUpdateLocalSoft():
num = M('tasks').where('status!=?', ('1',)).count()
2018-12-04 06:38:27 -05:00
if os.path.exists('mdserver-web.zip'):
2018-12-04 05:55:27 -05:00
return True
2018-12-04 06:38:27 -05:00
if num > 0:
data = M('tasks').where('status!=?', ('1',)).field(
'id,type,execstr').limit('1').select()
argv = data[0]['execstr'].split('|dl|')
if data[0]['type'] == 'download' and argv[1] == 'mdserver-web.zip':
return True
2018-12-04 05:55:27 -05:00
return False
2018-11-08 03:53:01 -05:00
def hasPwd(password):
# 加密密码字符
import crypt
return crypt.crypt(password, password)
2018-12-19 02:21:31 -05:00
def getTimeout(url):
2018-11-08 03:53:01 -05:00
start = time.time()
result = httpGet(url)
if result != 'True':
return False
return int((time.time() - start) * 1000)
2019-01-18 02:46:59 -05:00
def makeConf():
file = getRunDir() + '/data/json/config.json'
if not os.path.exists(file):
c = {}
c['title'] = 'Linux面板'
c['home'] = 'http://github/midoks/mdserver-web'
c['recycle_bin'] = True
c['template'] = 'default'
writeFile(file, json.dumps(c))
return c
c = readFile(file)
return json.loads(c)
def getConfig(k):
c = makeConf()
return c[k]
def setConfig(k, v):
c = makeConf()
c[k] = v
file = getRunDir() + '/data/json/config.json'
return writeFile(file, json.dumps(c))
def getHostAddr():
if os.path.exists('data/iplist.txt'):
return readFile('data/iplist.txt').strip()
return '127.0.0.1'
def setHostAddr(addr):
file = getRunDir() + '/data/iplist.txt'
return writeFile(file, addr)
def getHostPort():
if os.path.exists('data/port.pl'):
return readFile('data/port.pl').strip()
return '7200'
def setHostPort(port):
file = getRunDir() + '/data/port.pl'
return writeFile(file, port)
2018-11-08 03:53:01 -05:00
def auth_decode(data):
2018-11-20 06:11:09 -05:00
# 解密数据
2018-11-08 03:53:01 -05:00
token = GetToken()
# 是否有生成Token
if not token:
return returnMsg(False, 'REQUEST_ERR')
# 校验access_key是否正确
if token['access_key'] != data['btauth_key']:
return returnMsg(False, 'REQUEST_ERR')
# 解码数据
import binascii
import hashlib
import urllib
import hmac
import json
tdata = binascii.unhexlify(data['data'])
# 校验signature是否正确
signature = binascii.hexlify(
hmac.new(token['secret_key'], tdata, digestmod=hashlib.sha256).digest())
if signature != data['signature']:
return returnMsg(False, 'REQUEST_ERR')
# 返回
return json.loads(urllib.unquote(tdata))
# 数据加密
def auth_encode(data):
token = GetToken()
pdata = {}
# 是否有生成Token
if not token:
return returnMsg(False, 'REQUEST_ERR')
# 生成signature
import binascii
import hashlib
import urllib
import hmac
import json
tdata = urllib.quote(json.dumps(data))
# 公式 hex(hmac_sha256(data))
pdata['signature'] = binascii.hexlify(
hmac.new(token['secret_key'], tdata, digestmod=hashlib.sha256).digest())
# 加密数据
pdata['btauth_key'] = token['access_key']
pdata['data'] = binascii.hexlify(tdata)
pdata['timestamp'] = time.time()
# 返回
return pdata
def checkToken(get):
2018-11-11 10:51:11 -05:00
# 检查Token
2018-11-08 03:53:01 -05:00
tempFile = 'data/tempToken.json'
if not os.path.exists(tempFile):
return False
import json
import time
tempToken = json.loads(readFile(tempFile))
if time.time() > tempToken['timeout']:
return False
if get.token != tempToken['token']:
return False
return True
def checkInput(data):
# 过滤输入
if not data:
return data
if type(data) != str:
return data
checkList = [
{'d': '<', 'r': ''},
{'d': '>', 'r': ''},
{'d': '\'', 'r': ''},
{'d': '"', 'r': ''},
{'d': '&', 'r': ''},
{'d': '#', 'r': ''},
{'d': '<', 'r': ''}
]
for v in checkList:
data = data.replace(v['d'], v['r'])
return data
2018-11-11 10:51:11 -05:00
def checkCert(certPath='ssl/certificate.pem'):
2018-11-08 03:53:01 -05:00
# 验证证书
openssl = '/usr/local/openssl/bin/openssl'
if not os.path.exists(openssl):
openssl = 'openssl'
certPem = readFile(certPath)
s = "\n-----BEGIN CERTIFICATE-----"
tmp = certPem.strip().split(s)
for tmp1 in tmp:
if tmp1.find('-----BEGIN CERTIFICATE-----') == -1:
tmp1 = s + tmp1
writeFile(certPath, tmp1)
2018-11-09 02:46:28 -05:00
result = execShell(openssl + " x509 -in " +
2018-11-08 03:53:01 -05:00
certPath + " -noout -subject")
if result[1].find('-bash:') != -1:
return True
if len(result[1]) > 2:
return False
if result[0].find('error:') != -1:
return False
return True
2019-02-25 22:26:17 -05:00
def getPathSize(path):
# 取文件或目录大小
if not os.path.exists(path):
return 0
if not os.path.isdir(path):
return os.path.getsize(path)
size_total = 0
for nf in os.walk(path):
for f in nf[2]:
filename = nf[0] + '/' + f
size_total += os.path.getsize(filename)
return size_total
2018-11-08 03:53:01 -05:00
2018-12-03 03:44:29 -05:00
def toSize(size):
2018-11-08 03:53:01 -05:00
# 字节单位转换
d = ('b', 'KB', 'MB', 'GB', 'TB')
s = d[0]
for b in d:
if size < 1024:
2019-01-19 03:37:08 -05:00
return str(round(size, 2)) + ' ' + b
2019-01-19 03:41:11 -05:00
size = float(size) / 1024.0
2018-11-08 03:53:01 -05:00
s = b
2019-01-19 03:37:08 -05:00
return str(round(size, 2)) + ' ' + b
2018-11-08 03:53:01 -05:00
2018-12-03 03:44:29 -05:00
def getMacAddress():
# 获取mac
import uuid
mac = uuid.UUID(int=uuid.getnode()).hex[-12:]
return ":".join([mac[e:e + 2] for e in range(0, 11, 2)])
2018-11-08 03:53:01 -05:00
def get_string(t):
if t != -1:
max = 126
m_types = [{'m': 122, 'n': 97}, {'m': 90, 'n': 65}, {'m': 57, 'n': 48}, {
'm': 47, 'n': 32}, {'m': 64, 'n': 58}, {'m': 96, 'n': 91}, {'m': 125, 'n': 123}]
else:
max = 256
t = 0
m_types = [{'m': 255, 'n': 0}]
arr = []
for i in range(max):
if i < m_types[t]['n'] or i > m_types[t]['m']:
continue
arr.append(chr(i))
return arr
def get_string_find(t):
if type(t) != list:
t = [t]
return_str = ''
for s1 in t:
return_str += get_string(int(s1[0]))[int(s1[1:])]
return return_str
def get_string_arr(t):
s_arr = {}
t_arr = []
for s1 in t:
for i in range(6):
if not i in s_arr:
s_arr[i] = get_string(i)
for j in range(len(s_arr[i])):
if s1 == s_arr[i][j]:
t_arr.append(str(i) + str(j))
return t_arr
2019-02-18 03:21:31 -05:00
def getSSHPort():
try:
file = '/etc/ssh/sshd_config'
2021-02-01 06:55:21 -05:00
conf = readFile(file)
2019-02-18 03:21:31 -05:00
rep = "#*Port\s+([0-9]+)\s*\n"
port = re.search(rep, conf).groups(0)[0]
return int(port)
except:
return 22
def getSSHStatus():
if os.path.exists('/usr/bin/apt-get'):
status = execShell("service ssh status | grep -P '(dead|stop)'")
else:
import system_api
version = system_api.system_api().getSystemVersion()
if version.find(' Mac ') != -1:
return True
if version.find(' 7.') != -1:
status = execShell("systemctl status sshd.service | grep 'dead'")
else:
status = execShell(
"/etc/init.d/sshd status | grep -e 'stopped' -e '已停'")
if len(status[0]) > 3:
status = False
else:
status = True
return status