views:

1055

answers:

4

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.

+2  A: 

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!

Alex Martelli
It's entirely possible to ping and traceroute without raw sockets or ICMP, with UDP packets. Many tools do this.
Glenn Maynard
@Glenn, that's not compatible with the actual ping and traceroute commands: if your counterparts support UDP echoes, these pseudo-ping and -traceroute versions will work, but, without ICMP, you're outside of the standard, and your checks might perfectly well fail (without a counterpart complying beyond standards) where the standard ICMP-based approaches would work.
Alex Martelli
Thanks. I guess I'm going to go back to trying this using the shell commands. New question about doing this here: http://stackoverflow.com/questions/1151897/how-can-i-perform-a-ping-or-traceroute-in-python-accessing-the-output-as-it-is-p
Dave Forgac
shelling out to the setuid-root commands is generally the best way, yep -- lemme look at your other question now.
Alex Martelli
ping does use ICMP, but standard UNIX traceroute actually uses UDP by default (although it can be told to use ICMP instead). You don't need UDP echoes, because a closed port should elict an ICMP "Destination Unreachable" with a code of 0x03 ("Port Unreachable").
caf
UDP traceroute is every bit as standard as ICMP. Just like ICMP, it's a natural side-effect of the standard TTL field. Ping is trickier--many secured systems simply discard packets to unopened ports, and if it's coincidentally an open port--you can't guarantee it isn't--then it probably won't work.
Glenn Maynard
A: 

Try using pyNMS (pyNMS is a set of Python modules for network management)

Bye

RRUZ
pyNMS is now pycopia: http://code.google.com/p/pycopia/
itsadok
A: 

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

Autoplectic
A: 

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.

Dougie