import os,json
class AdminDataBase(object): def __init__(self, database_path): self.database_path = database_path def InitDatabase(self): if os.path.exists(self.database_path) != None : init_database = \ { "HostList": [["1000", "127.0.0.1", "username", "password", "22"]], "HostGroup": [{"DefaultGroup": ["1000"]}, ], } with open(self.database_path, "w", encoding="utf-8") as fp: fp.write(json.dumps(init_database)) print("[+] {} 结构已被初始化.".format(self.database_path))
def ShowHostList(self): with open(self.database_path, "r", encoding="utf-8") as Read_Pointer: load_json = json.loads(Read_Pointer.read()) base = load_json.get("HostList") print("-" * 80) print("UUID \t 主机地址 \t\t\t 登录用户 \t\t 登录密码 \t 端口") print("-" * 80) for each in range(0, len(base)): print("{0:4} \t {1:15} \t {2:10} \t {3:10} \t {4:10}" .format(base[each][0], base[each][1], base[each][2], base[each][3], base[each][4])) print()
def AddHost(self, uuid, address, username, password, port): with open(self.database_path, "r", encoding="utf-8") as Read_Pointer: load_json = json.loads(Read_Pointer.read()) host_list = load_json.get("HostList") for each in range(len(host_list) - 1, len(host_list)): if (host_list[each][0] != uuid): host_list.append([uuid, address, username, password, port]) load_json["HostList"] = host_list with open(self.database_path, "w", encoding="utf-8") as Write_Pointer: dump_json = json.dumps(load_json) Write_Pointer.write(dump_json) print("[+] UUID {} 添加成功.".format(uuid)) else: print("[-] UUID {} 列表中存在,添加失败.".format(uuid)) return 0
def ModifyHost(self,uuid,modify_address,modify_username,modify_password,modify_port): with open(self.database_path, "r", encoding="utf-8") as Read_Pointer: load_json = json.loads(Read_Pointer.read()) host_list = load_json.get("HostList")
index = -1 for index, value in enumerate(host_list): if value[0] == uuid: print("[*] 已找到UUID {} 所在下标为 {}".format(uuid,index)) break else: index = -1
if index != -1: host_list[index] = [uuid,modify_address,modify_username,modify_password,modify_port] load_json["HostList"] = host_list
with open(self.database_path, "w", encoding="utf-8") as Write_Pointer: dump_json = json.dumps(load_json) Write_Pointer.write(dump_json) print("[+] UUID {} 修改完成.".format(uuid)) return 0 else: print("[-] UUID {} 不存在.".format(uuid)) return 0
def DeleteHost(self,uuid): with open(self.database_path, "r", encoding="utf-8") as Read_Pointer: load_json = json.loads(Read_Pointer.read()) host_group = load_json.get("HostGroup") for each in range(0,len(host_group)): try: for k,v in host_group[each].items(): if v.count(uuid) != 0: v.remove(uuid) load_json["HostGroup"] = v with open(self.database_path, "w", encoding="utf-8") as Write_Pointer: dump_json = json.dumps(load_json) Write_Pointer.write(dump_json) else: break except Exception: print("[-] 当前组不能存在: {}".format(uuid)) return 0 host_list = load_json.get("HostList") try: index = -1 for index, value in enumerate(host_list): if value[0] == uuid: break else: index = -1 if index != -1: host_list.pop(index) load_json["HostList"] = host_list with open(self.database_path, "w", encoding="utf-8") as Write_Pointer: dump_json = json.dumps(load_json) Write_Pointer.write(dump_json) print("[+] UUID {} 主机: {} 已被移除.".format(uuid,host_list[index][1])) return 0 except Exception: return 0
def AddHostGroup(self,add_group_name): with open(self.database_path, "r", encoding="utf-8") as Read_Pointer: load_json = json.loads( Read_Pointer.read() ) group_obj = load_json.get("HostGroup") for each in range(0, len(group_obj)): list_name = str(list(group_obj[each].keys())[0])
if (list_name == add_group_name): print("[-] {} 存在于组中,无法继续添加.".format(list_name)) return 0 tmp = {} tmp[add_group_name] = ["1000"] group_obj.append(tmp)
with open(self.database_path, "w", encoding="utf-8") as Write_Pointer: dump_json = json.dumps(load_json) Write_Pointer.write(dump_json) print("[+] 主机组 {} 已添加".format(add_group_name))
def DeleteHostGroup(self,delete_group_name): with open(self.database_path, "r", encoding="utf-8") as Read_Pointer: load_json = json.loads( Read_Pointer.read() ) group_obj = load_json.get("HostGroup")
for each in range(0, len(group_obj)): list_name = str(list(group_obj[each].keys())[0]) if (list_name == delete_group_name): group_obj.pop(each)
with open(self.database_path, "w", encoding="utf-8") as Write_Pointer: dump_json = json.dumps(load_json) Write_Pointer.write(dump_json) print("[-] 主机组 {} 已移除.".format(delete_group_name)) return 0 print("[-] 主机组 {} 不存在.".format(delete_group_name)) return 0
def AddHostGroupOnUUID(self,group_name, uuid): with open(self.database_path, "r", encoding="utf-8") as Read_Pointer: load_json = json.loads( Read_Pointer.read() ) group_obj = load_json.get("HostGroup") for each in range(0, len(group_obj)): list_name = str(list(group_obj[each].keys())[0]) if (list_name == group_name): tmp = group_obj[each] val = list(tmp.values())[0] if str(uuid) not in val: val.append(str(uuid)) group_obj[each][list_name] = val else: print("[-] UUID {} 已存在于 {} 主机组,添加失败.".format(uuid,group_name)) return 0
with open(self.database_path, "w", encoding="utf-8") as Write_Pointer: dump_json = json.dumps(load_json) Write_Pointer.write(dump_json) print("[+] 向主机组 {} 增加UUID {} 完成".format(group_name, uuid)) return 0
def DeleteHostGroupOnUUID(self,group_name, uuid): with open(self.database_path, "r", encoding="utf-8") as Read_Pointer: load_json =json.loads( Read_Pointer.read() ) group_obj = load_json.get("HostGroup") for each in range(0, len(group_obj)): list_name = str(list(group_obj[each].keys())[0]) if (list_name == group_name): tmp = group_obj[each] val = list(tmp.values())[0] for x in range(0, len(val)): if (val[x] == uuid): print("[*] 搜索UUID: {} 索引值: {}".format(val[x], x)) val.pop(x) group_obj[each][list_name] = val with open(self.database_path, "w", encoding="utf-8") as write_fp: dump_json = json.dumps(load_json) write_fp.write(dump_json) print("[+] 从主机组 {} 弹出UUID {} 完成".format(group_name, uuid)) return 0 return 0
def ShowAllGroup(self): with open(self.database_path, "r", encoding="utf-8") as Read_Pointer: load_json = json.loads( Read_Pointer.read() ) group_obj = load_json.get("HostGroup") for each in range(0, len(group_obj)): for k, v in group_obj[each].items(): print("-" * 140) print("主机组: {}".format(k)) print("-" * 140) for each in range(0, len(v)): base_obj = load_json.get("HostList") for x in range(0, len(base_obj)): if (v[each] == base_obj[x][0]): print("UUID: {0:6} \t 主机地址: {1:15} \t 主机账号: {2:15} \t 主机密码: {3:15} \t 端口: {4:5} \t". format(base_obj[x][0], base_obj[x][1], base_obj[x][2], base_obj[x][3], base_obj[x][4])) print("\n")
def ShowGroup(self): with open(self.database_path, "r", encoding="utf-8") as Read_Pointer: load_json = json.loads( Read_Pointer.read() ) group_obj = load_json.get("HostGroup") print("-" * 80) print("{0:20} \t {1:5} \t {2:100}".format("主机组名","主机数","主机列表")) print("-" * 80) for each in range(0,len(group_obj)): for k,v in group_obj[each].items(): print("{0:20} \t {1:5} \t {2:100}".format(k,str(len(v)), str(v))) print() return 0
def PingGroup(self,group_name): with open(self.database_path, "r", encoding="utf-8") as Read_Pointer: load_json = json.loads( Read_Pointer.read() ) group_obj = load_json.get("HostGroup") for each in range(0, len(group_obj)): for k, v in group_obj[each].items(): if (k == group_name): item_list = v for val in range(0, len(item_list)): base_obj = load_json.get("HostList") for base_count in range(0, len(base_obj)): if (base_obj[base_count][0] == item_list[val]): print("地址: {} \t 用户名: {} [ok]".format(base_obj[base_count][1],base_obj[base_count][2])) if __name__ == "__main__": db = AdminDataBase("database.json") while True: try: cmd = str(input("[LyShell] # ")).split() if (cmd == ""): continue elif (cmd[0] == "exit"): exit(1) elif (cmd[0] == "clear"): os.system("cls") elif(cmd[0] == "Init"): db.InitDatabase() elif(cmd[0] == "ShowHostList"): db.ShowHostList() elif(cmd[0] == "ShowGroup"): db.ShowGroup() elif(cmd[0] == "ShowAllGroup"): db.ShowAllGroup() elif(cmd[0] == "AddHost" or cmd[0] == "ModifyHost"): if (len(cmd) - 1 >= 5): uuid = str(cmd[1]).split("=")[1] address = str(cmd[2]).split("=")[1] username = str(cmd[3]).split("=")[1] password = str(cmd[4]).split("=")[1] port = str(cmd[5]).split("=")[1] if cmd[0] == "AddHost": db.AddHost(uuid,address,username,password,port) elif cmd[0] == "ModifyHost": db.ModifyHost(uuid,address,username,password,port) elif(cmd[0] == "DeleteHost"): if(len(cmd)-1 >= 1): uuid = str(cmd[1]).split("=")[1] db.DeleteHost(uuid) elif(cmd[0] == "AddHostGroup"): group_name = str(cmd[1]).split("=")[1] db.AddHostGroup(group_name) elif(cmd[0] == "DeleteHostGroup"): group_name = str(cmd[1]).split("=")[1] db.DeleteHostGroup(group_name) elif(cmd[0] == "AddHostGroupOnUUID"): group_name = str(cmd[1]).split("=")[1] uuid = str(cmd[2]).split("=")[1] db.AddHostGroupOnUUID(group_name,uuid) elif(cmd[0] == "DelHostGroupOnUUID"): group_name = str(cmd[1]).split("=")[1] uuid = str(cmd[2]).split("=")[1] db.DeleteHostGroupOnUUID(group_name,uuid) elif(cmd[0] == "PingGroup"): group_name = str(cmd[1]).split("=")[1] db.PingGroup(group_name) elif (cmd[0] == "help"): print("By: LyShark") else: print("Not Command") except Exception: continue
|