Implement Traceroute in Python 3

I was wondering how to implement Traceroute in Python, and after some research, I found the code for python2, but it was a bit annoying when I set it to 3, so I decided to publish it to Qiita. Volume

Original thank you URL

UDP traceroute-network route survey in Python

Referenced Stack Overflow

conditions

$ python --version
Python 3.5.2

State to come out

environment

$ uname -a
Darwin Naokis-MacBook.local 16.4.0 Darwin Kernel Version 16.4.0: Thu Dec 22 22:53:21 PST 2016; root:xnu-3789.41.3~3/RELEASE_X86_64 x86_64

Traceroute_python3.py


#!/usr/bin/env python
import sys
import socket

def traceroute(dest_name, port, max_hops):
    dest_addr = socket.gethostbyname(dest_name)
    print("target ==> %s (%s)" % (dest_name, dest_addr))

    socket.setdefaulttimeout(10)
    icmp = socket.getprotobyname('icmp')
    udp = socket.getprotobyname('udp')
    ttl = 1

    while True:
        recv_socket = socket.socket(socket.AF_INET, socket.SOCK_RAW, icmp)
        send_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, udp)
        send_socket.setsockopt(socket.SOL_IP, socket.IP_TTL, ttl)
        recv_socket.bind(("", port))
        send_socket.sendto(bytes(512), (dest_addr, port))
        curr_addr = None
        curr_name = None
        try:
            curr_name, curr_addr = recv_socket.recvfrom(512)
            curr_addr = curr_addr[0]
            try:
                curr_name = socket.gethostbyaddr(curr_addr)[0]
            except socket.error:
                curr_name = curr_addr
        except socket.error:
            pass
        finally:
            send_socket.close()
            recv_socket.close()

        if curr_addr is not None:
            curr_host = "%s (%s)" % (curr_name, curr_addr)
        else:
            curr_host = "*"
        print("%d  %s" % (ttl, curr_host))

        ttl += 1
        if curr_name == dest_name or curr_addr == dest_addr or ttl > max_hops:
            break

if __name__ == "__main__":
    traceroute(str(sys.argv[1]), int(sys.argv[2]), int(sys.argv[3]))


Example

CommandLine.sh


$ sudo python Traceroute_python3.py 8.8.8.8 33434 30
$ sudo python Traceroute_python3.py google.com 32141 30

Apparently, it mixes with normal traceroute, so there may be room for improvement in that area.

Recommended Posts

Implement Traceroute in Python 3
Implement Enigma in python
Implement XENO in python
Implement naive bayes in Python 3.3
Implement Redis Mutex in Python
Implement extension field in Python
Implement fast RPC in Python
Implement method chain in Python
Implement Dijkstra's Algorithm in python
Implement Slack chatbot in Python
Implement stacking learning in Python [Kaggle]
Implement R's power.prop.test function in python
Implement the Singleton pattern in Python
Quickly implement REST API in Python
Quadtree in Python --2
Python in optimization
CURL in python
Metaprogramming in Python
Python 3.3 in Anaconda
Geocoding in python
SendKeys in Python
Meta-analysis in Python
Unittest in python
Epoch in Python
Sudoku in Python
DCI in Python
quicksort in python
nCr in python
N-Gram in Python
Programming in python
Plink in Python
Constant in python
Lifegame in Python.
FizzBuzz in Python
Sqlite in python
StepAIC in Python
N-gram in python
LINE-Bot [0] in Python
Csv in python
Disassemble in Python
Reflection in Python
Constant in python
nCr in Python.
format in python
PPAP in Python
Quad-tree in Python
Reflection in Python
Chemistry in Python
Hashable in python
DirectLiNGAM in Python
LiNGAM in Python
Flatten in python
flatten in python
I tried to implement PLSA in Python
Implement __eq__ etc. generically in Python class
I tried to implement permutation in Python
Implement FIR filters in Python and C
Collectively implement statistical hypothesis testing in Python
I tried to implement PLSA in Python 2
I tried to implement PPO in Python
Sorted list in Python