mask(m,p) : trouver l’adresse ip d’un sous réseau à partir d’une ip locale (p) et d’un masque (m)
rand() : renvoie une ip locale et un masques générés aléatoirement sous forme d’appel à mask()
Le script propose une courte description ainsi que deux exemples lors de son importation.
Note : les adresses ip et les masques sont spécifiés sous forme de listes (tuple) d’int-s, par exemple 192.168.0.1 s’écrit (192, 168, 0, 1)
et 255.255.248.0 s’écrit (255, 255, 248, 0)
from random import randint as rd def mask(m,p): # (mask)*4, (ip)*4 s=[[0,]*8]*4 mb=[[0,]*8]*4 pb=[[0,]*8]*4 for i in range(4): mb[i]=bin(m[i])[2:] pb[i]=bin(p[i])[2:] for j in range(8-len(mb[i])): mb[i]="0"+mb[i] for j in range(8-len(pb[i])): pb[i]="0"+pb[i] print("\nRECAP") print(" ",".".join(str(x) for x in p),"ipv4:") print(" ",".".join(str(x) for x in pb)) print(" ",".".join(str(x) for x in m),"mask:") print(" ",".".join(str(x) for x in mb),end="\n\n") denom=["st","nd","th"] for i in range(4): xnom=denom[i] if i<len(denom) else denom[-1] print(i+1,xnom,"byte ",end="") print(" ",mb[i]+" & "+pb[i],end="") for j in range(8): s[i][j]=1*(mb[i][j]=="1" and pb[i][j]=="1") s[i]="".join(str(x) for x in s[i]) print(":",s[i]) print("\nENDED, local network ip:") print(" ",".".join(str(int("0b"+x,2)) for x in s)) print(" ",".".join(s)) def rand(): m="mask((" k=[""]*4 t=rd(0,30) for i in range(32): k[i//8]+=str(1*(i<=t)) m+=",".join(str(int("0b"+x,2)) for x in k)+"),(" for i in range(4): m+=str(rd(0,255))+","*(i<3) m+="))" print(m) print("functions:") print(" mask(): find local network's ip") print(" args: mask, ip, as lists of dec_nums") print(" rand(): generate random mask and ip") print("example:") print("mask((255,255,255,0),(192,168,1,1))") print("rand()")