Help
cancel
Showing results for 
Search instead for 
Did you mean: 
Ground Controller Lvl 1
Message 1 of 3

Need IP+hostname in tracroute

i have below pyton3 code from Github and works for me . but i would need Hostname+IP address during traceroute
========================================================================

import ipaddress
import socket
from scapy.layers.inet import traceroute
import tkinter as tk
import networkx as nx
import matplotlib
import matplotlib.pyplot as plt


class Application(tk.Frame):
def __init__(self, master=None):
super().__init__(master)
self.pack()
self.create_form()

def create_form(self):
self.formFrame = tk.Frame(self)
self.formFrame.pack()
self.IPLabel = tk.Label(self.formFrame, text='IP')
self.IPLabel.grid(row=0, column=0)
self.IPEntry = tk.Entry(self.formFrame)
self.IPEntry.grid(row=0, column=1)
self.MaskLabel = tk.Label(self.formFrame, text='Mask')
self.MaskLabel.grid(row=0, column=2)
self.MaskEntry = tk.Entry(self.formFrame)
self.MaskEntry.grid(row=0, column=3)
self.topologyButtonText = tk.StringVar()
self.topologyButtonText.set('Show topology')
self.topologyButton = tk.Button(self, textvariable=self.topologyButtonText, command=self.get_topology)
self.topologyButton.pack()

def cleanup_figure(self):
if hasattr(self, 'fig'):
plt.close(self.fig)
self.fig = plt.figure(figsize=(8, 6), dpi=100)

def get_topology(self):
self.cleanup_figure()
self.generate_figure()
plt.show(block=False)

def generate_figure(self):
edges = self.trace_route()
network_graph = nx.Graph()
network_graph.add_edges_from(edges)
node_size = [float(network_graph.degree(v))*50 for v in network_graph]
node_color = [1-1/(1+float(network_graph.degree(v))) for v in network_graph]
nx.draw(network_graph, node_size=node_size, node_color=node_color, width=0.5, with_labels=True)

def trace_route(self):
edges = set()
try:
network = list(ipaddress.IPv4Network(self.IPEntry.get() + '/' + self.MaskEntry.get()))

res, _ = traceroute(list(map(lambda x: str(x), network)))
if not hasattr(self, 'res'):
self.res = res
else:
self.res = self.res + res

tr_dict = dict()
help_dict = dict()

for r in self.res:
if r[0].dst in tr_dict:
if r[1].src in help_dict[r[0].dst]:
continue
tr_dict[r[0].dst][r[0].ttl] = r[1].src
help_dict[r[0].dst].add(r[1].src)
else:
tr_dict[r[0].dst] = {r[0].ttl: r[1].src}
help_dict[r[0].dst] = {r[1].src}

for ttl_dict in tr_dict.values():
last_src='localhost'
for ttl in sorted(ttl_dict.keys()):
edges.add((last_src, ttl_dict[ttl]))
last_src=ttl_dict[ttl]

except Exception as e:
print(e)
edges.add(('localhost', 'localhost'))

return edges


if __name__ == '__main__':
app = Application(master=tk.Tk())
app.master.title('Network Topology Viewer')
app.mainloop()
===============================================================

Please let me know what further we need to add ...
Thanks in advance 

2 Replies
Commander Lvl 2
Message 2 of 3

Re: Need IP+hostname in tracroute

You don't show us what the input is, nor do you show the resulting output. It is difficult to guess what needs to be solved.  You also did not put the python into a code block, so all the (very necessary!) formatting has been lost. For future reference, pointing us to a gist or pointer to a github repo would be far more useful.

 

You're using scapy, so a google for "scapy traceroute" finds a few hits which might be informative. The ip addresses are all there.

 

Once you have an ip address, you need to perform a DNS lookup to get its hostname.

I'll let you do the google "scapy dns lookup" yourself, the first few hits look useful.

 

Please follow-up to let us know how you made out. For good karma, mark a reply as the answer if it helped!

Community Manager
Message 3 of 3

Re: Need IP+hostname in tracroute

Hi @shamim11287 ,


This post was moved to a different board that fits your topic of discussion a bit better.  This means you’ll get better engagement on your post, and it keeps our Community organized so users can more easily find information.


As you’ll notice, your Topic is now in the Programming Help and Discussion board.


Let me know if you have any other questions or if I can help with anything else.

Cheers!

Thank you for your perspective and your contribution!
All the Best

Mark helpful posts with Accept as Solution to help other users locate important info. Don't forget to give Kudos for great content!