Partage
  • Partager sur Facebook
  • Partager sur Twitter

[PYTHON] Découpage réseau (IP+CIDR)

Sujet résolu
Anonyme
    17 juillet 2019 à 16:57:02

    Bonjour à tous,

    Je voulais mettre en place un script python qui analyse les règles pare-feu, je fais face à un problème, la clé CIDR. C'est à dire le découpage réseau via une fonction python.

    Une ligne "règle pare-feu" :

    "true","-1","false","true","","RULE","15","tcp","192.168.0.1-192.168.0.4","302-304","allow","192.168.0.8-192.168.0.9"

    Ce que j'aimerais avoir en retour sous forme de 4 listes :

    ip_source['192.168.0.1','192.168.0.2','192.168.0.3','192.168.0.4']
    ip_destination['192.168.0.8','192.168.0.9']
    port['302','303','304']
    protocole['tcp']

    Autre exemple avec CIDR :


    "true","-1","false","true","","RULE","23","udp","192.168.0.1/24","412","allow","192.168.0.8-192.168.0.9"


    ip_source['192.168.0.1'...............'192.168.0.255']
    ip_destination['192.168.0.8','192.168.0.9']
    port['412']
    protocole['udp']

    Pour les listes "port" et "protocole" je ne rencontre pas de problème.

    C'est uniquement le découpage réseau qui me pose problème, si vous avez la moindre idée ou suggestion je suis preneur.

    Merci à tous,

    Cordialement,

    E

    • Partager sur Facebook
    • Partager sur Twitter
      17 juillet 2019 à 17:12:51

      source_ip = src.split('-') #['xxx.xxx.x.y' , 'xxx.xxx.x.z']
      first_ip = source_ip[0].split('.')[3] # y
      second_ip = source_ip[1].split('.')[3] # z
      
      ip_source = []
      for i in range(first_ip, second_ip):
          ip_source.insert(xxx.xxx.x.i)

      Y'a surement un moyen plus 'pythonic' de le faire .. mais quelque chose du genre devrait faire le taff.


      • Partager sur Facebook
      • Partager sur Twitter
      Anonyme
        17 juillet 2019 à 17:16:01

        Merci beaucoup, c'est le cas simple, car si une ip est 192.168.1.10 et que l'autre est 192.168.2.12 ça ce complique.

        Puis après avec la clé CIDR il faut calculer la plage d'adresse IP et la je pèche...

        Mais ton idée est l'une des solutions à mes problème alors je prends, merci beaucoup !

        • Partager sur Facebook
        • Partager sur Twitter
          17 juillet 2019 à 17:18:06

          Si tu reste dans la plage 192.168.x.y : il suffi de faire une double boucle. Mais le principe reste le meme.
          • Partager sur Facebook
          • Partager sur Twitter
          Anonyme
            17 juillet 2019 à 17:19:55

            Exactement, ça répond déjà à 50% de ma demande, c'est très bien merci beaucoup.
            • Partager sur Facebook
            • Partager sur Twitter
              17 juillet 2019 à 22:49:10

              Salut,

              Le module de la bibliothèque standard ipaddress devrait t'aider énormément.

              Tu as donné deux cas de figure. Soit tu reçois deux addresses ip séparées par un tiret. Dans ce cas, tu sépares les deux addresses, tu utilises la fonction summarize_address_range et te voilà avec un générateur. De là, pour te faire une liste des addresses possibles, c'est comme ceci

              >>> import ipaddress
              >>> data = '192.168.1.10-192.168.2.12'
              >>> ip_from, ip_to = (ipaddress.IPv4Address(s) for s in data.split('-'))
              >>> ip_from
              IPv4Address('192.168.1.10')
              >>> ip_to
              IPv4Address('192.168.2.12')
              >>> ip_range = list(ipaddress.summarize_address_range(ip_from, ip_to))
              >>> ip_range
              [IPv4Network('192.168.1.10/31'), IPv4Network('192.168.1.12/30'), IPv4Network('192.168.1.16/28'), IPv4Network('192.168.1.32/27'), IPv4Network('192.168.1.64/26'), IPv4Network('192.168.1.128/25'), IPv4Network('192.168.2.0/29'), IPv4Network('192.168.2.8/30'), IPv4Network('192.168.2.12/32')]
              >>> # Chaque IPv4Network est un itérable. On utilise de itertools chain.from_iterable pour concaténer le tout en liste
              ... 
              >>> from itertools import chain
              >>> ip_range = list(chain.from_iterable(ip_range))
              >>> ip_range
              [IPv4Address('192.168.1.10'), IPv4Address('192.168.1.11'), IPv4Address('192.168.1.12'), IPv4Address('192.168.1.13'), IPv4Address('192.168.1.14'), IPv4Address('192.168.1.15'), IPv4Address('192.168.1.16'), IPv4Address('192.168.1.17'), IPv4Address('192.168.1.18'), IPv4Address('192.168.1.19'), IPv4Address('192.168.1.20'), IPv4Address('192.168.1.21'), IPv4Address('192.168.1.22'), IPv4Address('192.168.1.23'), IPv4Address('192.168.1.24'), IPv4Address('192.168.1.25'), IPv4Address('192.168.1.26'), IPv4Address('192.168.1.27'), IPv4Address('192.168.1.28'), IPv4Address('192.168.1.29'), IPv4Address('192.168.1.30'), IPv4Address('192.168.1.31'), IPv4Address('192.168.1.32'), IPv4Address('192.168.1.33'), IPv4Address('192.168.1.34'), IPv4Address('192.168.1.35'), IPv4Address('192.168.1.36'), IPv4Address('192.168.1.37'), IPv4Address('192.168.1.38'), IPv4Address('192.168.1.39'), IPv4Address('192.168.1.40'), IPv4Address('192.168.1.41'), IPv4Address('192.168.1.42'), IPv4Address('192.168.1.43'), IPv4Address('192.168.1.44'), IPv4Address('192.168.1.45'), IPv4Address('192.168.1.46'), IPv4Address('192.168.1.47'), IPv4Address('192.168.1.48'), IPv4Address('192.168.1.49'), IPv4Address('192.168.1.50'), IPv4Address('192.168.1.51'), IPv4Address('192.168.1.52'), IPv4Address('192.168.1.53'), IPv4Address('192.168.1.54'), IPv4Address('192.168.1.55'), IPv4Address('192.168.1.56'), IPv4Address('192.168.1.57'), IPv4Address('192.168.1.58'), IPv4Address('192.168.1.59'), IPv4Address('192.168.1.60'), IPv4Address('192.168.1.61'), IPv4Address('192.168.1.62'), IPv4Address('192.168.1.63'), IPv4Address('192.168.1.64'), IPv4Address('192.168.1.65'), IPv4Address('192.168.1.66'), IPv4Address('192.168.1.67'), IPv4Address('192.168.1.68'), IPv4Address('192.168.1.69'), IPv4Address('192.168.1.70'), IPv4Address('192.168.1.71'), IPv4Address('192.168.1.72'), IPv4Address('192.168.1.73'), IPv4Address('192.168.1.74'), IPv4Address('192.168.1.75'), IPv4Address('192.168.1.76'), IPv4Address('192.168.1.77'), IPv4Address('192.168.1.78'), IPv4Address('192.168.1.79'), IPv4Address('192.168.1.80'), IPv4Address('192.168.1.81'), IPv4Address('192.168.1.82'), IPv4Address('192.168.1.83'), IPv4Address('192.168.1.84'), IPv4Address('192.168.1.85'), IPv4Address('192.168.1.86'), IPv4Address('192.168.1.87'), IPv4Address('192.168.1.88'), IPv4Address('192.168.1.89'), IPv4Address('192.168.1.90'), IPv4Address('192.168.1.91'), IPv4Address('192.168.1.92'), IPv4Address('192.168.1.93'), IPv4Address('192.168.1.94'), IPv4Address('192.168.1.95'), IPv4Address('192.168.1.96'), IPv4Address('192.168.1.97'), IPv4Address('192.168.1.98'), IPv4Address('192.168.1.99'), IPv4Address('192.168.1.100'), IPv4Address('192.168.1.101'), IPv4Address('192.168.1.102'), IPv4Address('192.168.1.103'), IPv4Address('192.168.1.104'), IPv4Address('192.168.1.105'), IPv4Address('192.168.1.106'), IPv4Address('192.168.1.107'), IPv4Address('192.168.1.108'), IPv4Address('192.168.1.109'), IPv4Address('192.168.1.110'), IPv4Address('192.168.1.111'), IPv4Address('192.168.1.112'), IPv4Address('192.168.1.113'), IPv4Address('192.168.1.114'), IPv4Address('192.168.1.115'), IPv4Address('192.168.1.116'), IPv4Address('192.168.1.117'), IPv4Address('192.168.1.118'), IPv4Address('192.168.1.119'), IPv4Address('192.168.1.120'), IPv4Address('192.168.1.121'), IPv4Address('192.168.1.122'), IPv4Address('192.168.1.123'), IPv4Address('192.168.1.124'), IPv4Address('192.168.1.125'), IPv4Address('192.168.1.126'), IPv4Address('192.168.1.127'), IPv4Address('192.168.1.128'), IPv4Address('192.168.1.129'), IPv4Address('192.168.1.130'), IPv4Address('192.168.1.131'), IPv4Address('192.168.1.132'), IPv4Address('192.168.1.133'), IPv4Address('192.168.1.134'), IPv4Address('192.168.1.135'), IPv4Address('192.168.1.136'), IPv4Address('192.168.1.137'), IPv4Address('192.168.1.138'), IPv4Address('192.168.1.139'), IPv4Address('192.168.1.140'), IPv4Address('192.168.1.141'), IPv4Address('192.168.1.142'), IPv4Address('192.168.1.143'), IPv4Address('192.168.1.144'), IPv4Address('192.168.1.145'), IPv4Address('192.168.1.146'), IPv4Address('192.168.1.147'), IPv4Address('192.168.1.148'), IPv4Address('192.168.1.149'), IPv4Address('192.168.1.150'), IPv4Address('192.168.1.151'), IPv4Address('192.168.1.152'), IPv4Address('192.168.1.153'), IPv4Address('192.168.1.154'), IPv4Address('192.168.1.155'), IPv4Address('192.168.1.156'), IPv4Address('192.168.1.157'), IPv4Address('192.168.1.158'), IPv4Address('192.168.1.159'), IPv4Address('192.168.1.160'), IPv4Address('192.168.1.161'), IPv4Address('192.168.1.162'), IPv4Address('192.168.1.163'), IPv4Address('192.168.1.164'), IPv4Address('192.168.1.165'), IPv4Address('192.168.1.166'), IPv4Address('192.168.1.167'), IPv4Address('192.168.1.168'), IPv4Address('192.168.1.169'), IPv4Address('192.168.1.170'), IPv4Address('192.168.1.171'), IPv4Address('192.168.1.172'), IPv4Address('192.168.1.173'), IPv4Address('192.168.1.174'), IPv4Address('192.168.1.175'), IPv4Address('192.168.1.176'), IPv4Address('192.168.1.177'), IPv4Address('192.168.1.178'), IPv4Address('192.168.1.179'), IPv4Address('192.168.1.180'), IPv4Address('192.168.1.181'), IPv4Address('192.168.1.182'), IPv4Address('192.168.1.183'), IPv4Address('192.168.1.184'), IPv4Address('192.168.1.185'), IPv4Address('192.168.1.186'), IPv4Address('192.168.1.187'), IPv4Address('192.168.1.188'), IPv4Address('192.168.1.189'), IPv4Address('192.168.1.190'), IPv4Address('192.168.1.191'), IPv4Address('192.168.1.192'), IPv4Address('192.168.1.193'), IPv4Address('192.168.1.194'), IPv4Address('192.168.1.195'), IPv4Address('192.168.1.196'), IPv4Address('192.168.1.197'), IPv4Address('192.168.1.198'), IPv4Address('192.168.1.199'), IPv4Address('192.168.1.200'), IPv4Address('192.168.1.201'), IPv4Address('192.168.1.202'), IPv4Address('192.168.1.203'), IPv4Address('192.168.1.204'), IPv4Address('192.168.1.205'), IPv4Address('192.168.1.206'), IPv4Address('192.168.1.207'), IPv4Address('192.168.1.208'), IPv4Address('192.168.1.209'), IPv4Address('192.168.1.210'), IPv4Address('192.168.1.211'), IPv4Address('192.168.1.212'), IPv4Address('192.168.1.213'), IPv4Address('192.168.1.214'), IPv4Address('192.168.1.215'), IPv4Address('192.168.1.216'), IPv4Address('192.168.1.217'), IPv4Address('192.168.1.218'), IPv4Address('192.168.1.219'), IPv4Address('192.168.1.220'), IPv4Address('192.168.1.221'), IPv4Address('192.168.1.222'), IPv4Address('192.168.1.223'), IPv4Address('192.168.1.224'), IPv4Address('192.168.1.225'), IPv4Address('192.168.1.226'), IPv4Address('192.168.1.227'), IPv4Address('192.168.1.228'), IPv4Address('192.168.1.229'), IPv4Address('192.168.1.230'), IPv4Address('192.168.1.231'), IPv4Address('192.168.1.232'), IPv4Address('192.168.1.233'), IPv4Address('192.168.1.234'), IPv4Address('192.168.1.235'), IPv4Address('192.168.1.236'), IPv4Address('192.168.1.237'), IPv4Address('192.168.1.238'), IPv4Address('192.168.1.239'), IPv4Address('192.168.1.240'), IPv4Address('192.168.1.241'), IPv4Address('192.168.1.242'), IPv4Address('192.168.1.243'), IPv4Address('192.168.1.244'), IPv4Address('192.168.1.245'), IPv4Address('192.168.1.246'), IPv4Address('192.168.1.247'), IPv4Address('192.168.1.248'), IPv4Address('192.168.1.249'), IPv4Address('192.168.1.250'), IPv4Address('192.168.1.251'), IPv4Address('192.168.1.252'), IPv4Address('192.168.1.253'), IPv4Address('192.168.1.254'), IPv4Address('192.168.1.255'), IPv4Address('192.168.2.0'), IPv4Address('192.168.2.1'), IPv4Address('192.168.2.2'), IPv4Address('192.168.2.3'), IPv4Address('192.168.2.4'), IPv4Address('192.168.2.5'), IPv4Address('192.168.2.6'), IPv4Address('192.168.2.7'), IPv4Address('192.168.2.8'), IPv4Address('192.168.2.9'), IPv4Address('192.168.2.10'), IPv4Address('192.168.2.11'), IPv4Address('192.168.2.12')]
              

              Dans le deuxième cas, tu reçois un masque réseau. C'est encore plus simple. Il suffit de faire

              >>> data = '192.168.0.1/24'
              >>> ip_range = list(ipaddress.IPv4Network(data, strict=False))
              >>> ip_range
              [IPv4Address('192.168.0.0'), IPv4Address('192.168.0.1'), IPv4Address('192.168.0.2'), IPv4Address('192.168.0.3'), IPv4Address('192.168.0.4'), IPv4Address('192.168.0.5'), IPv4Address('192.168.0.6'), IPv4Address('192.168.0.7'), IPv4Address('192.168.0.8'), IPv4Address('192.168.0.9'), IPv4Address('192.168.0.10'), IPv4Address('192.168.0.11'), IPv4Address('192.168.0.12'), IPv4Address('192.168.0.13'), IPv4Address('192.168.0.14'), IPv4Address('192.168.0.15'), IPv4Address('192.168.0.16'), IPv4Address('192.168.0.17'), IPv4Address('192.168.0.18'), IPv4Address('192.168.0.19'), IPv4Address('192.168.0.20'), IPv4Address('192.168.0.21'), IPv4Address('192.168.0.22'), IPv4Address('192.168.0.23'), IPv4Address('192.168.0.24'), IPv4Address('192.168.0.25'), IPv4Address('192.168.0.26'), IPv4Address('192.168.0.27'), IPv4Address('192.168.0.28'), IPv4Address('192.168.0.29'), IPv4Address('192.168.0.30'), IPv4Address('192.168.0.31'), IPv4Address('192.168.0.32'), IPv4Address('192.168.0.33'), IPv4Address('192.168.0.34'), IPv4Address('192.168.0.35'), IPv4Address('192.168.0.36'), IPv4Address('192.168.0.37'), IPv4Address('192.168.0.38'), IPv4Address('192.168.0.39'), IPv4Address('192.168.0.40'), IPv4Address('192.168.0.41'), IPv4Address('192.168.0.42'), IPv4Address('192.168.0.43'), IPv4Address('192.168.0.44'), IPv4Address('192.168.0.45'), IPv4Address('192.168.0.46'), IPv4Address('192.168.0.47'), IPv4Address('192.168.0.48'), IPv4Address('192.168.0.49'), IPv4Address('192.168.0.50'), IPv4Address('192.168.0.51'), IPv4Address('192.168.0.52'), IPv4Address('192.168.0.53'), IPv4Address('192.168.0.54'), IPv4Address('192.168.0.55'), IPv4Address('192.168.0.56'), IPv4Address('192.168.0.57'), IPv4Address('192.168.0.58'), IPv4Address('192.168.0.59'), IPv4Address('192.168.0.60'), IPv4Address('192.168.0.61'), IPv4Address('192.168.0.62'), IPv4Address('192.168.0.63'), IPv4Address('192.168.0.64'), IPv4Address('192.168.0.65'), IPv4Address('192.168.0.66'), IPv4Address('192.168.0.67'), IPv4Address('192.168.0.68'), IPv4Address('192.168.0.69'), IPv4Address('192.168.0.70'), IPv4Address('192.168.0.71'), IPv4Address('192.168.0.72'), IPv4Address('192.168.0.73'), IPv4Address('192.168.0.74'), IPv4Address('192.168.0.75'), IPv4Address('192.168.0.76'), IPv4Address('192.168.0.77'), IPv4Address('192.168.0.78'), IPv4Address('192.168.0.79'), IPv4Address('192.168.0.80'), IPv4Address('192.168.0.81'), IPv4Address('192.168.0.82'), IPv4Address('192.168.0.83'), IPv4Address('192.168.0.84'), IPv4Address('192.168.0.85'), IPv4Address('192.168.0.86'), IPv4Address('192.168.0.87'), IPv4Address('192.168.0.88'), IPv4Address('192.168.0.89'), IPv4Address('192.168.0.90'), IPv4Address('192.168.0.91'), IPv4Address('192.168.0.92'), IPv4Address('192.168.0.93'), IPv4Address('192.168.0.94'), IPv4Address('192.168.0.95'), IPv4Address('192.168.0.96'), IPv4Address('192.168.0.97'), IPv4Address('192.168.0.98'), IPv4Address('192.168.0.99'), IPv4Address('192.168.0.100'), IPv4Address('192.168.0.101'), IPv4Address('192.168.0.102'), IPv4Address('192.168.0.103'), IPv4Address('192.168.0.104'), IPv4Address('192.168.0.105'), IPv4Address('192.168.0.106'), IPv4Address('192.168.0.107'), IPv4Address('192.168.0.108'), IPv4Address('192.168.0.109'), IPv4Address('192.168.0.110'), IPv4Address('192.168.0.111'), IPv4Address('192.168.0.112'), IPv4Address('192.168.0.113'), IPv4Address('192.168.0.114'), IPv4Address('192.168.0.115'), IPv4Address('192.168.0.116'), IPv4Address('192.168.0.117'), IPv4Address('192.168.0.118'), IPv4Address('192.168.0.119'), IPv4Address('192.168.0.120'), IPv4Address('192.168.0.121'), IPv4Address('192.168.0.122'), IPv4Address('192.168.0.123'), IPv4Address('192.168.0.124'), IPv4Address('192.168.0.125'), IPv4Address('192.168.0.126'), IPv4Address('192.168.0.127'), IPv4Address('192.168.0.128'), IPv4Address('192.168.0.129'), IPv4Address('192.168.0.130'), IPv4Address('192.168.0.131'), IPv4Address('192.168.0.132'), IPv4Address('192.168.0.133'), IPv4Address('192.168.0.134'), IPv4Address('192.168.0.135'), IPv4Address('192.168.0.136'), IPv4Address('192.168.0.137'), IPv4Address('192.168.0.138'), IPv4Address('192.168.0.139'), IPv4Address('192.168.0.140'), IPv4Address('192.168.0.141'), IPv4Address('192.168.0.142'), IPv4Address('192.168.0.143'), IPv4Address('192.168.0.144'), IPv4Address('192.168.0.145'), IPv4Address('192.168.0.146'), IPv4Address('192.168.0.147'), IPv4Address('192.168.0.148'), IPv4Address('192.168.0.149'), IPv4Address('192.168.0.150'), IPv4Address('192.168.0.151'), IPv4Address('192.168.0.152'), IPv4Address('192.168.0.153'), IPv4Address('192.168.0.154'), IPv4Address('192.168.0.155'), IPv4Address('192.168.0.156'), IPv4Address('192.168.0.157'), IPv4Address('192.168.0.158'), IPv4Address('192.168.0.159'), IPv4Address('192.168.0.160'), IPv4Address('192.168.0.161'), IPv4Address('192.168.0.162'), IPv4Address('192.168.0.163'), IPv4Address('192.168.0.164'), IPv4Address('192.168.0.165'), IPv4Address('192.168.0.166'), IPv4Address('192.168.0.167'), IPv4Address('192.168.0.168'), IPv4Address('192.168.0.169'), IPv4Address('192.168.0.170'), IPv4Address('192.168.0.171'), IPv4Address('192.168.0.172'), IPv4Address('192.168.0.173'), IPv4Address('192.168.0.174'), IPv4Address('192.168.0.175'), IPv4Address('192.168.0.176'), IPv4Address('192.168.0.177'), IPv4Address('192.168.0.178'), IPv4Address('192.168.0.179'), IPv4Address('192.168.0.180'), IPv4Address('192.168.0.181'), IPv4Address('192.168.0.182'), IPv4Address('192.168.0.183'), IPv4Address('192.168.0.184'), IPv4Address('192.168.0.185'), IPv4Address('192.168.0.186'), IPv4Address('192.168.0.187'), IPv4Address('192.168.0.188'), IPv4Address('192.168.0.189'), IPv4Address('192.168.0.190'), IPv4Address('192.168.0.191'), IPv4Address('192.168.0.192'), IPv4Address('192.168.0.193'), IPv4Address('192.168.0.194'), IPv4Address('192.168.0.195'), IPv4Address('192.168.0.196'), IPv4Address('192.168.0.197'), IPv4Address('192.168.0.198'), IPv4Address('192.168.0.199'), IPv4Address('192.168.0.200'), IPv4Address('192.168.0.201'), IPv4Address('192.168.0.202'), IPv4Address('192.168.0.203'), IPv4Address('192.168.0.204'), IPv4Address('192.168.0.205'), IPv4Address('192.168.0.206'), IPv4Address('192.168.0.207'), IPv4Address('192.168.0.208'), IPv4Address('192.168.0.209'), IPv4Address('192.168.0.210'), IPv4Address('192.168.0.211'), IPv4Address('192.168.0.212'), IPv4Address('192.168.0.213'), IPv4Address('192.168.0.214'), IPv4Address('192.168.0.215'), IPv4Address('192.168.0.216'), IPv4Address('192.168.0.217'), IPv4Address('192.168.0.218'), IPv4Address('192.168.0.219'), IPv4Address('192.168.0.220'), IPv4Address('192.168.0.221'), IPv4Address('192.168.0.222'), IPv4Address('192.168.0.223'), IPv4Address('192.168.0.224'), IPv4Address('192.168.0.225'), IPv4Address('192.168.0.226'), IPv4Address('192.168.0.227'), IPv4Address('192.168.0.228'), IPv4Address('192.168.0.229'), IPv4Address('192.168.0.230'), IPv4Address('192.168.0.231'), IPv4Address('192.168.0.232'), IPv4Address('192.168.0.233'), IPv4Address('192.168.0.234'), IPv4Address('192.168.0.235'), IPv4Address('192.168.0.236'), IPv4Address('192.168.0.237'), IPv4Address('192.168.0.238'), IPv4Address('192.168.0.239'), IPv4Address('192.168.0.240'), IPv4Address('192.168.0.241'), IPv4Address('192.168.0.242'), IPv4Address('192.168.0.243'), IPv4Address('192.168.0.244'), IPv4Address('192.168.0.245'), IPv4Address('192.168.0.246'), IPv4Address('192.168.0.247'), IPv4Address('192.168.0.248'), IPv4Address('192.168.0.249'), IPv4Address('192.168.0.250'), IPv4Address('192.168.0.251'), IPv4Address('192.168.0.252'), IPv4Address('192.168.0.253'), IPv4Address('192.168.0.254'), IPv4Address('192.168.0.255')]
              




              • Partager sur Facebook
              • Partager sur Twitter
              Anonyme
                18 juillet 2019 à 9:28:58

                Pile poile, c'est parfait !

                J'ai mis unicode(data) sinon cela ne fonctionnait pas pour moi..

                • Partager sur Facebook
                • Partager sur Twitter
                  18 juillet 2019 à 9:52:41

                  Content de t’avoir aidé. 

                  Utiliser unicode() ?? T’es toujours sur Python 2. Ça fait froid dans le dos.

                  • Partager sur Facebook
                  • Partager sur Twitter
                  Anonyme
                    18 juillet 2019 à 10:10:29

                    L'existant ne me permet malheureusement pas d'utiliser python3 pour le moment..

                    [IPv4Address('192.168.1.10'), IPv4Address('192.168.1.11'), IPv4Address('192.168.2.11'), IPv4Address('192.168.2.12')]

                    Petit question, dans la liste présente ci dessus idéalement elle devrais devenir comme celle ci-dessous :

                    ['192.168.1.10', '192.168.1.11','192.168.2.11','192.168.2.12']

                    Il y a une solution en direct pour éviter de stocker "IPv4Address" ?

                    • Partager sur Facebook
                    • Partager sur Twitter
                      18 juillet 2019 à 11:48:52

                      Tu ne t'es pas pris la peine de lire la documentation de ipaddress... :(

                      En appelant la fonction str sur l'objet IPv4Address, il te retourne une chaîne de caractères. 

                      # python 2.7 
                      >>> import ipaddress
                      >>> data = u'192.168.0.1/24'
                      >>> ip_range = list(ipaddress.IPv4Network(data, strict=False))
                      >>> [str(ip) for ip in ip_range]
                      ['192.168.0.0', '192.168.0.1', '192.168.0.2', '192.168.0.3', '192.168.0.4', '192.168.0.5', '192.168.0.6', '192.168.0.7', '192.168.0.8', '192.168.0.9', '192.168.0.10', '192.168.0.11', '192.168.0.12', '192.168.0.13', '192.168.0.14', '192.168.0.15', '192.168.0.16', '192.168.0.17', '192.168.0.18', '192.168.0.19', '192.168.0.20', '192.168.0.21', '192.168.0.22', '192.168.0.23', '192.168.0.24', '192.168.0.25', '192.168.0.26', '192.168.0.27', '192.168.0.28', '192.168.0.29', '192.168.0.30', '192.168.0.31', '192.168.0.32', '192.168.0.33', '192.168.0.34', '192.168.0.35', '192.168.0.36', '192.168.0.37', '192.168.0.38', '192.168.0.39', '192.168.0.40', '192.168.0.41', '192.168.0.42', '192.168.0.43', '192.168.0.44', '192.168.0.45', '192.168.0.46', '192.168.0.47', '192.168.0.48', '192.168.0.49', '192.168.0.50', '192.168.0.51', '192.168.0.52', '192.168.0.53', '192.168.0.54', '192.168.0.55', '192.168.0.56', '192.168.0.57', '192.168.0.58', '192.168.0.59', '192.168.0.60', '192.168.0.61', '192.168.0.62', '192.168.0.63', '192.168.0.64', '192.168.0.65', '192.168.0.66', '192.168.0.67', '192.168.0.68', '192.168.0.69', '192.168.0.70', '192.168.0.71', '192.168.0.72', '192.168.0.73', '192.168.0.74', '192.168.0.75', '192.168.0.76', '192.168.0.77', '192.168.0.78', '192.168.0.79', '192.168.0.80', '192.168.0.81', '192.168.0.82', '192.168.0.83', '192.168.0.84', '192.168.0.85', '192.168.0.86', '192.168.0.87', '192.168.0.88', '192.168.0.89', '192.168.0.90', '192.168.0.91', '192.168.0.92', '192.168.0.93', '192.168.0.94', '192.168.0.95', '192.168.0.96', '192.168.0.97', '192.168.0.98', '192.168.0.99', '192.168.0.100', '192.168.0.101', '192.168.0.102', '192.168.0.103', '192.168.0.104', '192.168.0.105', '192.168.0.106', '192.168.0.107', '192.168.0.108', '192.168.0.109', '192.168.0.110', '192.168.0.111', '192.168.0.112', '192.168.0.113', '192.168.0.114', '192.168.0.115', '192.168.0.116', '192.168.0.117', '192.168.0.118', '192.168.0.119', '192.168.0.120', '192.168.0.121', '192.168.0.122', '192.168.0.123', '192.168.0.124', '192.168.0.125', '192.168.0.126', '192.168.0.127', '192.168.0.128', '192.168.0.129', '192.168.0.130', '192.168.0.131', '192.168.0.132', '192.168.0.133', '192.168.0.134', '192.168.0.135', '192.168.0.136', '192.168.0.137', '192.168.0.138', '192.168.0.139', '192.168.0.140', '192.168.0.141', '192.168.0.142', '192.168.0.143', '192.168.0.144', '192.168.0.145', '192.168.0.146', '192.168.0.147', '192.168.0.148', '192.168.0.149', '192.168.0.150', '192.168.0.151', '192.168.0.152', '192.168.0.153', '192.168.0.154', '192.168.0.155', '192.168.0.156', '192.168.0.157', '192.168.0.158', '192.168.0.159', '192.168.0.160', '192.168.0.161', '192.168.0.162', '192.168.0.163', '192.168.0.164', '192.168.0.165', '192.168.0.166', '192.168.0.167', '192.168.0.168', '192.168.0.169', '192.168.0.170', '192.168.0.171', '192.168.0.172', '192.168.0.173', '192.168.0.174', '192.168.0.175', '192.168.0.176', '192.168.0.177', '192.168.0.178', '192.168.0.179', '192.168.0.180', '192.168.0.181', '192.168.0.182', '192.168.0.183', '192.168.0.184', '192.168.0.185', '192.168.0.186', '192.168.0.187', '192.168.0.188', '192.168.0.189', '192.168.0.190', '192.168.0.191', '192.168.0.192', '192.168.0.193', '192.168.0.194', '192.168.0.195', '192.168.0.196', '192.168.0.197', '192.168.0.198', '192.168.0.199', '192.168.0.200', '192.168.0.201', '192.168.0.202', '192.168.0.203', '192.168.0.204', '192.168.0.205', '192.168.0.206', '192.168.0.207', '192.168.0.208', '192.168.0.209', '192.168.0.210', '192.168.0.211', '192.168.0.212', '192.168.0.213', '192.168.0.214', '192.168.0.215', '192.168.0.216', '192.168.0.217', '192.168.0.218', '192.168.0.219', '192.168.0.220', '192.168.0.221', '192.168.0.222', '192.168.0.223', '192.168.0.224', '192.168.0.225', '192.168.0.226', '192.168.0.227', '192.168.0.228', '192.168.0.229', '192.168.0.230', '192.168.0.231', '192.168.0.232', '192.168.0.233', '192.168.0.234', '192.168.0.235', '192.168.0.236', '192.168.0.237', '192.168.0.238', '192.168.0.239', '192.168.0.240', '192.168.0.241', '192.168.0.242', '192.168.0.243', '192.168.0.244', '192.168.0.245', '192.168.0.246', '192.168.0.247', '192.168.0.248', '192.168.0.249', '192.168.0.250', '192.168.0.251', '192.168.0.252', '192.168.0.253', '192.168.0.254', '192.168.0.255']
                      



                      • Partager sur Facebook
                      • Partager sur Twitter
                      Anonyme
                        18 juillet 2019 à 12:01:45

                        J'ai trouvé une solution entre temps, la même, merci beaucoup, et si j'ai étudié la documentation :) Merci beaucoup !
                        • Partager sur Facebook
                        • Partager sur Twitter

                        [PYTHON] Découpage réseau (IP+CIDR)

                        × Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié.
                        • Editeur
                        • Markdown