Skip to content

Instantly share code, notes, and snippets.

@mishudark
Created December 11, 2010 06:48
Show Gist options
  • Save mishudark/737207 to your computer and use it in GitHub Desktop.
Save mishudark/737207 to your computer and use it in GitHub Desktop.
Calcula el vlsm y renderiza un grafo de la red
#!/bin/env python
"""
graph_red.py
Copyright 2010 mishudark <moonsadly@gmail.com><mishudark@astrata.com.mx>
based on graph from nodebox
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
MA 02110-1301, USA.
"""
from nodebox.graphics import *
from nodebox.graphics.physics import Node, Edge, Graph
g = Graph()
dragged = None
results = {}
data = {}
redes = {#red description
'Red 1':{'hosts':3001, 'conections':['router A']},
'Red 2':{'hosts':1501, 'conections':['router A']},
'Red 3':{'hosts':5001, 'conections':['router B']},
'Red 4':{'hosts':3001, 'conections':['router B']},
'Red 5':{'hosts':4001, 'conections':['router C']},
'Red 6':{'hosts':1501, 'conections':['router C']},
'Red 7':{'hosts':251, 'conections':['router D']},
'Red 8':{'hosts':601, 'conections':['router D']},
'router A':{'hosts':3, 'conections':['router B','router D','router E']},
'router B':{'hosts':3, 'conections':['router C','router E']},
'router C':{'hosts':3, 'conections':['router D','router E']},
'router D':{'hosts':3, 'conections':['router E']},
'router E':{'hosts':4, 'conections':[]},
}
red = '172.16.128.0/18'
def get_data():
for i in redes.keys():
data[i] = redes[i]['hosts']
def vlsm():
"""
vlsm calc
hl = tabla de hosts por red
nw = bits para red
hd = bits para hosts
"""
ip,mask = red.split('/')
a,b,c,d = ip.split('.')
octetos = [a,b,c,d]
hosts = 0
hl = []
ipant = ip
for i in data.keys():
hosts += data[i] + 2
hl.append(data[i] + 2)
bits = binario(hosts)
print "bits: %s" %bits
hl.sort(reverse=True)
#make table to all keys values
ids = [] #list of keys
falses = [] #list to items prev added to ids
for i in hl:
for d in data:
if data[d] == i-2 and d not in falses:
ids.append(d)
falses.append(d)
print ids
if(bits > mask):
print "La red no soporta %s hosts" % hosts
else:
cont = -1
for i in hl:
cont+=1
bt = binario(i)
nw = 32 - bt #mask for vlsm
bntemp = (bt % 8)
bn = 0
print "bits:%s"%bntemp
for j in range(bntemp,8):
bn += 2**j
diff = 256 - bn
cadena = ''
octet = int(nw / 8)
if nw % 8 != 0:#correcting octet
octet += 1
for c in range(1,octet):
cadena += str(octetos[c-1])
if c < 4:
cadena += '.'
cadena += str(int(octetos[octet-1]) + diff -1)
if octet < 4:
cadena += '.'
for oc in range(octet+1,5):
cadena += '255'
if oc < 4:
cadena += '.'
#modify original ip for next red
octetos[octet-1] = int(octetos[octet-1]) + diff
#save info in results
key = ids[cont]
results[key] = "%s/%s"%(ipant,nw)
print "%s: %s - %s / %s" %(key,ipant,cadena,nw)
ipant = str(octetos[0])+'.'+str(octetos[1])+'.'+str(octetos[2])+'.'+str(octetos[3])
print results
def binario(num):
bina = 2
bits = 1
while(bina < num):
bits += 1
bina *= 2
return bits
def gfd(key_value):
#return format red: xxx.xxx.xxx.xxx/xx
return "%s: %s" % (key_value,results[key_value])
def gid(key):
#FIXME
#get the original key
value,garbage = key.split(':')
return value
def graphic():
#make graph container
#insert data in graph
for red in redes.keys():
g.append(
Node(
id = gfd(red), # + str(redes[red]['hosts']),
radius = 5,
stroke = color(0),
text = color(0),
)
)
print gfd(red)
for node in g.nodes:
node1 = node
node2 = node #temp
for conection in redes[gid(node.id)]['conections']:
for n in g.nodes:
if gid(n.id) == conection:
node2 = n
g.append(
Edge(
node1,node2,
length = 1.0,
weight = random(),
stroke = color(0)
)
)
#code from 07 example
for node in g.nodes:
node.radius = node.radius + node.radius*node.weight
# 2) Nodes with only one connection ("leaf" nodes) have a shorter connection.
for node in g.nodes:
if len(node.edges) == 1:
node.edges[0].length *= 0.1
g.prune(depth=0) # Remove orphaned nodes with no connections.
g.distance = 30 # Overall spacing between nodes.
g.layout.force = 0.01 # Strength of the attractive & repulsive force.
g.layout.repulsion = 15 # Repulsion radius.
def draw(canvas):
canvas.clear()
background(1)
translate(250, 250)
# With weighted=True, Node.centrality is indicated by a shadow under high-traffic nodes.
# With directed=True, edges have an arrowhead indicating the direction of the connection.
# This requires some extra calculations.
g.draw(weighted=True, directed=True)
g.update(iterations=8)
# Make it interactive!
# When the mouse is pressed, remember on which node.
# Drag this node around when the mouse is moved.
dx = canvas.mouse.x - 250 # Undo translate().
dy = canvas.mouse.y - 250
global dragged
if canvas.mouse.pressed and not dragged:
dragged = g.node_at(dx, dy)
if not canvas.mouse.pressed:
dragged = None
if dragged:
dragged.x = dx
dragged.y = dy
get_data()
vlsm()
graphic()
canvas.size = 800, 600
canvas.run(draw)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment