Created
February 9, 2018 17:05
-
-
Save h3ct0r/4b788a7b342cc9e55061747f525ba919 to your computer and use it in GitHub Desktop.
Python node link analyser
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
path_1_2 ={ | |
'src': 1, | |
'dst': 2, | |
'paths': [ | |
{'path': ['a', 'f', 'b'], 'retransmits': 0}, | |
{'path': ['a', 'e', 'b'], 'retransmits': 0}, | |
{'path': ['a', 'e', 'i', 'f', 'b'], 'retransmits': 0}, | |
] | |
} | |
path_3_2 = { | |
'src': 1, | |
'dst': 2, | |
'paths': [ | |
{'path': ['c', 'g', 'j', 'n', 'p', 'm', 'i', 'f', 'b'], 'retransmits': 4}, | |
{'path': ['c', 'h', 'j', 'n', 'p', 'm', 'i', 'f', 'b'], 'retransmits': 4}, | |
{'path': ['c', 'h', 'd', 'g', 'j', 'n', 'p', 'm', 'i', 'f', 'b'], 'retransmits': 4}, | |
{'path': ['c', 'h', 'd', 'g', 'j', 'n', 'p', 'l', 'i', 'f', 'b'], 'retransmits': 0}, | |
{'path': ['c', 'h', 'd', 'g', 'j', 'o', 'p', 'm', 'i', 'f', 'b'], 'retransmits': 4}, | |
{'path': ['c', 'h', 'd', 'g', 'j', 'o', 'p', 'l', 'i', 'f', 'b'], 'retransmits': 0}, | |
{'path': ['c', 'g', 'j', 'o', 'p', 'm', 'i', 'f', 'b'], 'retransmits': 4}, | |
{'path': ['c', 'g', 'j', 'n', 'p', 'l', 'i', 'f', 'b'], 'retransmits': 0}, | |
{'path': ['c', 'g', 'j', 'o', 'p', 'l', 'i', 'f', 'b'], 'retransmits': 0}, | |
] | |
} | |
path_4_2 = { | |
'src': 1, | |
'dst': 2, | |
'paths': [ | |
{'path': ['d', 'g', 'j', 'n', 'p', 'm', 'i', 'f', 'b'], 'retransmits': 4}, | |
{'path': ['d', 'h', 'j', 'n', 'p', 'm', 'i', 'f', 'b'], 'retransmits': 4}, | |
{'path': ['d', 'h', 'c', 'g', 'j', 'n', 'p', 'm', 'i', 'f', 'b'], 'retransmits': 4}, | |
{'path': ['d', 'h', 'c', 'g', 'j', 'n', 'p', 'l', 'i', 'f', 'b'], 'retransmits': 0}, | |
{'path': ['d', 'h', 'c', 'g', 'j', 'o', 'p', 'm', 'i', 'f', 'b'], 'retransmits': 4}, | |
{'path': ['d', 'h', 'c', 'g', 'j', 'o', 'p', 'l', 'i', 'f', 'b'], 'retransmits': 0}, | |
{'path': ['d', 'g', 'j', 'o', 'p', 'm', 'i', 'f', 'b'], 'retransmits': 4}, | |
{'path': ['d', 'g', 'j', 'n', 'p', 'l', 'i', 'f', 'b'], 'retransmits': 0}, | |
{'path': ['d', 'g', 'j', 'o', 'p', 'l', 'i', 'f', 'b'], 'retransmits': 0}, | |
] | |
} | |
# node_connections_ground_truth = { | |
# 'a': 3, | |
# 'b': 3, | |
# 'c': 3, | |
# 'd': 3, | |
# 'e': 3, | |
# 'f': 3, | |
# 'g': 3, | |
# 'h': 3, | |
# 'i': 4, | |
# 'j': 4, | |
# 'l': 2, | |
# 'm': 2, | |
# 'n': 2, | |
# 'o': 2, | |
# 'p': 4 | |
# } | |
node_connections = {} | |
node_paths = [path_1_2, path_3_2, path_4_2] | |
node_counter_dict = {} | |
for pinger_paths in node_paths: | |
src = pinger_paths['src'] | |
dst = pinger_paths['dst'] | |
for d in pinger_paths['paths']: | |
value = 0 | |
if d['retransmits'] > 1: | |
value += 1 | |
path_len = len(d['path']) | |
for i in xrange(path_len): | |
node = d['path'][i] | |
if node not in node_counter_dict: | |
node_counter_dict[node] = value | |
else: | |
node_counter_dict[node] += value | |
if node not in node_connections: | |
node_connections[node] = set() | |
# first node add src | |
if i == 0: | |
node_connections[node].add(src) | |
# previous node | |
if i - 1 >= 0: | |
node_connections[node].add(d['path'][i - 1]) | |
# next node | |
if i + 1 <= path_len - 1: | |
node_connections[node].add(d['path'][i + 1]) | |
# last node add dst | |
if i == path_len -1: | |
node_connections[node].add(dst) | |
print "Link size for every node:" | |
for k, v in node_connections.items(): | |
print "Node: {} links:{}".format(k, len(v)) | |
print "\nHeuristic score for every node:" | |
for k,v in node_counter_dict.items(): | |
print "Node: {} score:{}".format(k, v/len(node_connections[k])) | |
max_score_node_key = max(node_counter_dict.iterkeys(), key=(lambda key: node_counter_dict[key]/len(node_connections[key]) )) | |
print "\nMax scored node: {}".format(max_score_node_key) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment