Project

General

Profile

verif_ip.py

Philippe Carre, 02/19/2013 03:15 PM

Download (1.36 KB)

 
1
def valid_networks(data,**conditions):
2
    """
3
        on valide que l'ip est bien parmi les reseau eth3 OU eth4
4
        <check name='valid_networks' target='fw_pronote_relai_ip'>
5
                <param name='condition_1' type='eole'>adresse_ip_eth3</param>
6
                <param name='condition_2' type='eole'>adresse_netmask_eth3</param>
7
                <param name='condition_3' type='eole'>adresse_ip_eth4</param>
8
                <param name='condition_4' type='eole'>adresse_netmask_eth4</param>
9
        </check>
10
    """
11
    from creole import eosfunc
12
    from creole.error import TypeEoleError
13
    ips = []
14
    network_found = False
15
    if data is '' or data == '':
16
        return
17
    for num in range(0, len(conditions), 2):
18
        if not conditions.has_key('condition_%s'%(num+1)) or not conditions.has_key('condition_%s'%(num+2)):
19
            raise ValueError('Condition %s valid_networks inconnue'%num+1)
20
        ip = conditions.get('condition_%s'%(num+1))
21
        mask = conditions.get('condition_%s'%(num+2))
22
        if ip is '' or mask is '' :
23
             continue
24
        ips.append(ip)
25
        try:
26
            if eosfunc.calc_network_auto(ip, mask) == eosfunc.calc_network_auto(data, mask):
27
                network_found = True
28
                return True
29
        except:
30
            continue
31
    if network_found == False:
32
        raise TypeEoleError("L'ip %s ne correspond a aucun des reseaux parmi %s" % (data, ips))
33
        return False