Forum Archive

trace route script

hallaste

Simple trace route script fails in Pythonista, but works well in Python 2.7.5.

import socket

def main(dest_name):
dest_addr = socket.gethostbyname(dest_name)
port = 33434
max_hops = 30
icmp = socket.getprotobyname('icmp')
udp = socket.getprotobyname('udp')
ttl = 1
while True:
# error [Errno 1] Operation not permitted
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("", (dest_name, port))
curr_addr = None
curr_name = None
try:

filippocld223

can you correctly format the script?

hallaste
import socket

def main(dest_name):
    dest_addr = socket.gethostbyname(dest_name)
    port = 33434
    max_hops = 30
    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("", (dest_name, port))
        curr_addr = None
        curr_name = None
        try:
            _, 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\t%s" % (ttl, curr_host)

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

if __name__ == "__main__":
    main('google.com')

hallaste

Output I get with Python 2.7.5 is:

1   10.11.0.254 (10.11.0.254)
2   10.5.1.1 (10.5.1.1)
3   173-161-242-118-Philadelphia.hfc.comcastbusiness.net (173.161.242.118)
4   50.166.146.1 (50.166.146.1)
5   68.86.221.37 (68.86.221.37)
6   WGS_IT (10.5.8.83)
7   he-3-5-0-0-11-cr01.ashburn.va.ibone.comcast.net (68.86.92.161)
8   be-24-pe06.ashburn.va.ibone.comcast.net (68.86.82.162)
filippocld223

good :-)

filippocld223

it gives an error in line 11: operation not permitted

hallaste

Yes, I have tried everything I can think of to get he the code running in Pythonista, but with no luck. I was hoping that you could point me in a direction to solve the problem, as the code works in Python 2.7.5

hallaste

will try Pipista, thanks.

filippocld223

it's for apple sandboxing