Esempio n. 1
0
 def __load_manage_ip(self):
     """
     根据用户管理的组ID号,获取所有可以管理的主机IP列表,去重后返回唯一IP
     :return:
     """
     ip_list = []
     for gid in self.groups.split(","):
         hosts_in_gid = dbapi.load_host_by_gid(gid)
         for host in hosts_in_gid:
             ip_list.append(host["ip"])
     # 通过set转换一下,去重
     self.manage_ip = list(set(ip_list))
Esempio n. 2
0
 def __load_manage_ip(self):
     """
     根据用户管理的组ID号,获取所有可以管理的主机IP列表,去重后返回唯一IP
     :return:
     """
     ip_list = []
     for gid in self.groups.split(","):
         hosts_in_gid = dbapi.load_host_by_gid(gid)
         for host in hosts_in_gid:
             ip_list.append(host["ip"])
     # 通过set转换一下,去重
     self.manage_ip = list(set(ip_list))
Esempio n. 3
0
def _analyze_command(userobj, input_command_list):
    """
    根据用户输入的命令,分析命令参数,返回一个要操作的所有主机信息的列表[{host_info}], host_info包括:
    {ip,loginuser,auth_type,key}
    :param userobj:  用户对象
    :param input_command_list: 用户命名 e.g:cmd -h 192.168.1.2 -g G01,G02 -c "df -hl"
    :return: 返回主机信息的列表
    """
    # 执行命令的主机ip列表
    ipadress_list = []
    # 包含所有host信息的列表文件,可能有重复的
    all_host_detail_info = []
    # 结果host列表,存放主机信息字典
    exec_host_info = []

    # 获取 -h 后面的Ip地址
    if input_command_list.count("-h") > 0:
        # 获取输入命令中的IP列表
        input_ip_list = input_command_list[input_command_list.index("-h") +
                                           1].split(",")
        ipadress_list.extend(input_ip_list)
        # 获取主机IP对应的详细信息
        for ip in input_ip_list:
            host_detail = userobj.load_host_by_ip(ip)
            all_host_detail_info.append(host_detail)

    # 获取 -g 后面的组ID对应的所有IP地址
    if input_command_list.count("-g") > 0:
        input_gid_list = input_command_list[input_command_list.index("-g") +
                                            1].split(",")
        for gid in input_gid_list:
            host_info_list = dbapi.load_host_by_gid(gid)
            all_host_detail_info.extend(host_info_list)
            # 得到列表中的主机IP
            for host in host_info_list:
                ipadress_list.append(host["ip"])

    # 对列表中的IP进行去重
    ipadress_list = list(set(ipadress_list))

    # 获取要执行的IP列表中的IP对应服务器的信息,登录用户、验证方式、密码或密钥。
    for ip in ipadress_list:
        for host in all_host_detail_info:
            if host["ip"] == ip:
                exec_host_info.append(host)

    return exec_host_info
Esempio n. 4
0
def _analyze_command(userobj, input_command_list):
    """
    根据用户输入的命令,分析命令参数,返回一个要操作的所有主机信息的列表[{host_info}], host_info包括:
    {ip,loginuser,auth_type,key}
    :param userobj:  用户对象
    :param input_command_list: 用户命名 e.g:cmd -h 192.168.1.2 -g G01,G02 -c "df -hl"
    :return: 返回主机信息的列表
    """
    # 执行命令的主机ip列表
    ipadress_list = []
    # 包含所有host信息的列表文件,可能有重复的
    all_host_detail_info = []
    # 结果host列表,存放主机信息字典
    exec_host_info = []

    # 获取 -h 后面的Ip地址
    if input_command_list.count("-h") > 0:
        # 获取输入命令中的IP列表
        input_ip_list = input_command_list[input_command_list.index("-h") + 1].split(",")
        ipadress_list.extend(input_ip_list)
        # 获取主机IP对应的详细信息
        for ip in input_ip_list:
            host_detail = userobj.load_host_by_ip(ip)
            all_host_detail_info.append(host_detail)

    # 获取 -g 后面的组ID对应的所有IP地址
    if input_command_list.count("-g") > 0:
        input_gid_list = input_command_list[input_command_list.index("-g") + 1].split(",")
        for gid in input_gid_list:
            host_info_list = dbapi.load_host_by_gid(gid)
            all_host_detail_info.extend(host_info_list)
            # 得到列表中的主机IP
            for host in host_info_list:
                ipadress_list.append(host["ip"])

    # 对列表中的IP进行去重
    ipadress_list = list(set(ipadress_list))

    # 获取要执行的IP列表中的IP对应服务器的信息,登录用户、验证方式、密码或密钥。
    for ip in ipadress_list:
        for host in all_host_detail_info:
            if host["ip"] == ip:
                exec_host_info.append(host)

    return exec_host_info
Esempio n. 5
0
 def load_host_by_ip(self, ip):
     for gid in self.groups.split(","):
         host_info_list = dbapi.load_host_by_gid(gid)
         for host in host_info_list:
             if host["ip"] == ip:
                 return host
Esempio n. 6
0
def show(userobj, input_command_list):
    """
    执行show命令,查看当前用户管理的所有服务器信息,可用命令:
        show hosts -a : 查看所有服务器IP、主机名、所属组
        show hosts -g gid: 查看组id(gid) 对应的所有主机信息
        show groups: 查看当前用户管理的所有组
    如果show跟其它参数 不认识
    :param userobj:  用户对象
    :param input_command_list: 用户输入的命令,split后的列表
    :return: 返回结果信息
    """
    try:
        show_args = ["hosts", "groups"]

        if len(input_command_list) < 2:
            raise MyException("106")
        else:
            input_args = input_command_list[1]

            if input_args == "hosts":
                # 如果有 -a
                if input_command_list.count("-a") > 0:
                    if len(input_command_list) > 3:
                        raise MyException("103")
                    else:
                        # 显示所有主机信息
                        user_gid_list = userobj.groups.split(",")
                # 没有 -a, 有 -g 吗
                elif input_command_list.count("-g") > 0:
                    user_gid_list = input_command_list[
                        input_command_list.index("-g") + 1].split(",")

                else:
                    raise MyException("103")
                    # 开始显示信息
                for gid in user_gid_list:
                    # 根据用户的组ID,获取组对应的host信息字典
                    hosts_info = dbapi.load_host_by_gid(gid)
                    for host in hosts_info:
                        print(
                            "\033[1;30m \nIP: {0}  主机名: {1}  所属组ID: {2} \033[0m;"
                            .format(host["ip"], host["hostname"], gid))

            elif input_args == "groups":
                # 打印所有组信息
                user_gid = userobj.groups
                groups_dict = dbapi.load_group_info()
                # 从所有组信息中删除不在用户gid中的元素,剩下打印
                _total_gid_list = list(groups_dict.keys())
                for gid in _total_gid_list:
                    if gid not in user_gid.split(","):
                        del groups_dict[gid]
                # 显示用户组信息
                for k, v in groups_dict.items():
                    print("\033[1;30m GID: {0}, GNAME: {1}\033[0m;\n".format(
                        k, v))

            else:
                raise MyException("106")

    except MyException as e:
        write_log(e, "error")
        print(e)
    except Exception as e:
        write_log(e, "error")
Esempio n. 7
0
def show(userobj, input_command_list):
    """
    执行show命令,查看当前用户管理的所有服务器信息,可用命令:
        show hosts -a : 查看所有服务器IP、主机名、所属组
        show hosts -g gid: 查看组id(gid) 对应的所有主机信息
        show groups: 查看当前用户管理的所有组
    如果show跟其它参数 不认识
    :param userobj:  用户对象
    :param input_command_list: 用户输入的命令,split后的列表
    :return: 返回结果信息
    """
    try:
        show_args = ["hosts", "groups"]

        if len(input_command_list) < 2:
            raise MyException("106")
        else:
            input_args = input_command_list[1]

            if input_args == "hosts":
                # 如果有 -a
                if input_command_list.count("-a") > 0:
                    if len(input_command_list) > 3:
                        raise MyException("103")
                    else:
                        # 显示所有主机信息
                        user_gid_list = userobj.groups.split(",")
                # 没有 -a, 有 -g 吗
                elif input_command_list.count("-g") > 0:
                    user_gid_list = input_command_list[input_command_list.index("-g") + 1].split(",")

                else:
                    raise MyException("103")
                    # 开始显示信息
                for gid in user_gid_list:
                    # 根据用户的组ID,获取组对应的host信息字典
                    hosts_info = dbapi.load_host_by_gid(gid)
                    for host in hosts_info:
                        print("\033[1;30m \nIP: {0}  主机名: {1}  所属组ID: {2} \033[0m;".format(host["ip"],
                                                                                           host["hostname"],
                                                                                           gid))

            elif input_args == "groups":
                # 打印所有组信息
                user_gid = userobj.groups
                groups_dict = dbapi.load_group_info()
                # 从所有组信息中删除不在用户gid中的元素,剩下打印
                _total_gid_list = list(groups_dict.keys())
                for gid in _total_gid_list:
                    if gid not in user_gid.split(","):
                        del groups_dict[gid]
                # 显示用户组信息
                for k, v in groups_dict.items():
                    print("\033[1;30m GID: {0}, GNAME: {1}\033[0m;\n".format(k, v))

            else:
                raise MyException("106")

    except MyException as e:
        write_log(e, "error")
        print(e)
    except Exception as e:
        write_log(e, "error")
Esempio n. 8
0
 def load_host_by_ip(self, ip):
     for gid in self.groups.split(","):
         host_info_list = dbapi.load_host_by_gid(gid)
         for host in host_info_list:
             if host["ip"] == ip:
                 return host