diff --git a/func/check.py b/func/check.py index a27622d93881a2736eb38f40a44a1e38923cf50b..85335739f629022b806bdbe4bb13afc4a325783d 100644 --- a/func/check.py +++ b/func/check.py @@ -2,6 +2,8 @@ import os import json import platform import re +import time +import paramiko from settings import * from func.utils import list_to_json @@ -55,3 +57,34 @@ def check_os(data): state = 1 error = '无法检测到当前系统,请检查/etc/os-release文件,确认后重试.' return list_to_json(['ip', 'ret', 'error'],[agent_ip, state, error]) + +def check_SSHClent(user=None, passwd=None, ip=ip, port=port): + ssh = paramiko.SSHClient() + ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) + t = 0 + for _ in range(10): + t += 1 + time.sleep(1) + try: + ssh.connect(ip, username=user, password=passwd, port=port) + if user != "root": + stdin, stdout, stderr = ssh.exec_command('sudo -v') + flag = True + ret = stderr.read().decode() + ret = ret.split('\n')[:-1] + for i in range(len(ret)): + if re.match('sudo',ret[i].strip()[0:4]): + flag = False + strsudo = ret[i].strip() + if flag: + if ret != 'sudo': + data = list_to_json(['res', 'error'], ['1', '此用户没有root权限']) + return data + + ssh.close() + data = list_to_json(['res', 'data'], ['0', '验证完成成功']) + return data + except: + print("error:" + ip + user + passwd + str(port)) + data = list_to_json(['res', 'error'], ['1', '此用户没有root权限']) + return data \ No newline at end of file diff --git a/index.py b/index.py index 68abbbfd8edf9ccef6db21e4c7938aacfd689c7f..c2fdae9533f5beeacccf1c24e74ea2d32d16a271 100644 --- a/index.py +++ b/index.py @@ -13,6 +13,7 @@ mods = { 'check_storage' : migration.check_storage, 'check_os': migration.check_os, 'close_tool': migration.close_tool, + 'check_user': migration.check_user, } diff --git a/migration-tools-client.py b/migration-tools-client.py index cd01186c0013dae7039bfa86a6401066c221b8b1..b45e55b4f6d26569fe1c1231b745f7081d4fce14 100644 --- a/migration-tools-client.py +++ b/migration-tools-client.py @@ -36,6 +36,13 @@ def mt_check_os(): return Response(mod, content_type='application/json') +@app.route('/check_user', methods=['GET', 'POST']) +def mt_check_user(): + mod = check_methods() + if mod: + return Response(mod, content_type='application/json') + + if __name__ == '__main__': app.config["JSON_AS_ASCII"] = False uos_sysmig_conf = json.loads(getSysMigConf()) diff --git a/templates/MT_check_root.html b/templates/MT_check_root.html index f52ea7cd2f914be0eb21d98b9233f3beedb2c595..e9db1a7b13d476a407eb530906b206d0615d29ea 100755 --- a/templates/MT_check_root.html +++ b/templates/MT_check_root.html @@ -87,10 +87,13 @@ diff --git a/views/migration.py b/views/migration.py index 8860ad3c8c98ce7fd202ece20961f7ecb7934b3c..ce59f3a0d88d4bec5ef1f805a445d4486a316e6b 100644 --- a/views/migration.py +++ b/views/migration.py @@ -25,4 +25,9 @@ def close_tool(data): os.system('kill -9 `ps -ef | grep "start_webview.py" | grep -v grep | awk \'{print $2}\'`') data = {"ip": "0.0.0.0"} data_json = json.dumps(data) - return data_json \ No newline at end of file + return data_json + +def check_user(data): + services = check_services(data, '/check_user') + if services: + return services \ No newline at end of file