Beispiel #1
0
def get_host_rules(data):
    '''
    获取主机下的所有iptables规则
    :param data:
    :return:
    '''
    response = BaseResponse()

    ip_list = data.get('ip_list', None)  # 输入的IP地址
    envir = data.get('envir', None)  # 环境

    value_list = [ip_list, envir]
    for i in value_list:
        if i is None:
            response.message = u'请检查提交的数据'
            return response

    ipv4_re = re.compile(
        r'^(25[0-5]|2[0-4]\d|[0-1]?\d?\d)(\.(25[0-5]|2[0-4]\d|[0-1]?\d?\d)){3}$'
    )
    error_ip = []
    host_ip = ip_list.strip().split(",")

    for ip in host_ip:
        if not ipv4_re.match(ip):
            error_ip.append(ip)
    if len(error_ip) > 0:
        response.host_ip = error_ip
        response.ip_status = False
        response.message = u"IP地址出现错误!!!"
        return response

    set_host = list(set(host_ip))  # 将列表去重
    pwd_check = pwd_query.main(set_host, [], envir)  # 调用密码API 获取密码的列表 *****

    if len(pwd_check) == 0:
        response.message = u"密码检测失败!!请检查所选环境和密码是否存在~"
        response.pwd_status = False
        return response

    re_list = []  # 存放每台IP的执行结果
    for host_info in pwd_check:
        ip = host_info['ip']
        pwd = host_info['pwd']
        iptables_obj = execute_iptables.RunIptables([], ip, pwd)  # 连接服务器对象
        exe_re = iptables_obj.get_all_rules()  # 获取规则的方法
        re_list.append(exe_re)
    response.data = re_list
    response.status = True
    return response
Beispiel #2
0
def add_rule_str(data):
    '''
    获取主机下的所有iptables规则
    :param data:
    :return:
    '''
    response = BaseResponse()

    ip_list = data.get('ip_list', None)  # 输入的IP地址
    envir = data.get('envir', None)  # 环境
    rule_cmd_list = data.get('rule_cmd_list')  # 要添加的规则IP
    rule_cmd_list = json.loads(rule_cmd_list)

    value_list = [ip_list, envir]
    for i in value_list:
        if i is None:
            response.message = u'请检查提交的数据是否为空'
            return response

    ipv4_re = re.compile(
        r'^(25[0-5]|2[0-4]\d|[0-1]?\d?\d)(\.(25[0-5]|2[0-4]\d|[0-1]?\d?\d)){3}$'
    )
    error_ip = []
    host_ip = ip_list.strip().split(",")

    for ip in host_ip:
        if not ipv4_re.match(ip):
            error_ip.append(ip.strip())
    if len(error_ip) > 0:
        response.host_ip = error_ip
        response.ip_status = False
        response.message = u"IP地址出现错误!!!"
        return response

    set_host = list(set(host_ip))  # 将列表去重
    pwd_check = pwd_query.main(set_host, [], envir)  # 调用密码API 获取密码的列表 *****

    if len(pwd_check) == 0:
        response.message = u"密码检测失败!!请检查所选环境和密码是否存在~"
        response.pwd_status = False
        return response

    re_list = []  # 存放每台IP的执行结果
    all_cmd_list = []
    error_list = []
    # 将输入的规则IP 和 对应类型的规则拼接在一起
    if len(rule_cmd_list) > 0:

        for cmd_str in rule_cmd_list:
            if cmd_str.strip().startswith('iptables'):
                all_cmd_list.append(cmd_str)
            else:
                error_list.append(cmd_str)
    if len(error_list) > 0:
        response.message = u"命令输入错误,请检查。 %s " % ",".join(error_list)
        response.cmd_status = False
        return response
    for host_info in pwd_check:
        ip = host_info['ip']
        pwd = host_info['pwd']
        iptables_obj = execute_iptables.RunIptables(all_cmd_list, ip,
                                                    pwd)  # 连接服务器对象
        exe_re = iptables_obj.execute_iptables()  # 获取规则的方法
        re_list.append(exe_re)
    response.data = re_list
    response.status = True
    return response