How can I perform a ping or traceroute using nativ

2020-03-20 09:23发布

I would like to be able to perform a ping and traceroute from within Python without having to execute the corresponding shell commands so I'd prefer a native python solution.

7条回答
倾城 Initia
2楼-- · 2020-03-20 09:30

The mtrpacket package can be used to send network probes, which can perform either a ping or a traceroute. Since it uses the back-end to the mtr commandline tool, it doesn't require that your script run as root.

It also uses asyncio's event loop, so you can have multiple ongoing traceroutes or pings simultaneously, and deal with their results as they complete.

Here is a Python script to traceroute to 'example.com':

import asyncio
import mtrpacket

async def trace():
    async with mtrpacket.MtrPacket() as mtr:
        for ttl in range(1, 256):
            result = await mtr.probe('example.com', ttl=ttl)
            print(result)

            if result.success:
                break

asyncio.get_event_loop().run_until_complete(trace())

The loop with 'ttl' is used because the 'time-to-live' of an outgoing packet determines the number of network hops the packet will travel before expiring and sending an error back to the original source.

查看更多
太酷不给撩
3楼-- · 2020-03-20 09:36

ICMP Ping is standard as part of the ICMP protocol.

Traceroute uses features of ICMP and IP to determine a path via Time To Live values. Using TTL values, you can do traceroutes in a variety of protocols as long as IP/ICMP work because it is the ICMP TTL EXceeded messages that tell you about the hop in the path.

If you attempt to access a port where no listener is available, by ICMP protocol rules, the host is supposed to send an ICMP Port Unreachable message.

查看更多
三岁会撩人
4楼-- · 2020-03-20 09:36

I wrote a simple tcptraceroute in python which does not need root privileges http://www.thomas-guettler.de/scripts/tcptraceroute.py.txt

But it can't display the IP addresses of the intermediate hops. But sometimes it is useful, since you can guess where the blocking firewall is: Either at the beginning or at the end of the route.

查看更多
混吃等死
5楼-- · 2020-03-20 09:44

Running interpreters as root is often frowned upon on security grounds (and of course you DO need to have root permission to access the "raw" socked as needed by the ICMP specs of ping and traceroute!), but if you have no problems with that it's not hard -- e.g., this post gives a workable ping, and Jeremy Hylton's old page has still-usable underlying code for ICMP (both ping and traceroute) though it's written for very old Python versions and needs a litte facelift to shine with modern ones -- but, the concepts ARE all there, in both the URLs I gave you!

查看更多
姐就是有狂的资本
6楼-- · 2020-03-20 09:48

If you don't mind using an external module and not using UDP or TCP, scapy is an easy solution:

from scapy.all import *
target = ["192.168.1.254"]
result, unans = traceroute(target,l4=UDP(sport=RandShort())/DNS(qd=DNSQR(qname="www.google.com")))

Or you can use the tcp version

from scapy.all import *
target = ["192.168.1.254"]
result, unans = traceroute(target,maxttl=32)

Please note you will have to run scapy as root in order to be able to perform these tasks or you will get:

socket.error: [Errno 1] Operation not permitted
查看更多
太酷不给撩
7楼-- · 2020-03-20 09:48

you might want to check out the scapy package. it's the swiss army knife of network tools for python.

查看更多
登录 后发表回答