A pure python ICMP ping implementation using raw sockets
A pure python ping implementation using raw sockets.
Note that ICMP messages can only be sent from processes running as root (in Windows, you must run this script as ‘Administrator’).
Original Version from Matthew Dixon Cowles.
- Copyleft 1989-2011 by the python-ping team, see AUTHORS for more details.
- License: GNU GPL v2, see LICENCE for more details.
Use as a cli tool:
socketubs@socket-laptop [Pyping]: sudo pyping socketubs.net PYTHON-PING socketubs.net (188.8.131.52): 55 data bytes 241 bytes from socketubs.net (184.108.40.206): icmp_seq=0 ttl=55 time=64.5 ms 241 bytes from socketubs.net (220.127.116.11): icmp_seq=1 ttl=55 time=67.7 ms 241 bytes from socketubs.net (18.104.22.168): icmp_seq=2 ttl=55 time=66.6 ms ----socketubs.net PYTHON PING Statistics---- 3 packets transmitted, 3 packets received, 0.0% packet loss round-trip (ms) min/avg/max = 64.457/66.244/67.677 socketubs@socket-laptop [Pyping]: pyping --help
Use as a Python lib:
>>> import pyping >>> r = pyping.ping('socketubs.net') # Need to be root or >>> r = pyping.ping('socketubs.net', udp = True) # But it's udp, not real icmp >>> r.ret_code 0 >>> r.destination 'socketubs.net' >>> r.max_rtt '69.374' >>> r.avg_rtt '68.572' >>> r.min_rtt '67.681' >>> r.destination_ip '22.214.171.124'
- Refactor core.py
- Cleanup projet (by Socketubs): - Add Cli parser - Can be used as python library - Added to pypi - Some Fixes - Clean
- Bugfix if host is unknown
- Merge sources and create a seperate github repository and add a simple CLI interface.
- Bugfixes + cleanup by Jens Diemer. Tested with Ubuntu + Windows 7.
- Cleanup by Martin Falatic : - Restored lost comments and docs. Improved functionality: constant time between pings, internal times consistently use milliseconds. Clarified annotations (e.g., in the checksum routine) - Using unsigned data in IP & ICMP header pack/unpack unless otherwise necessary. - Signal handling - Ping-style output formatting and stats
- Ported to py3k by Zach Ware. Mostly done by 2to3; also minor changes to deal with bytes vs. string changes (no more ord() in checksum() because >source_string< is actually bytes, added .encode() to data in send_one_ping()). That’s about it.
- Changes by Samuel Stauffer: - Replaced time.clock with default_timer which is set to time.clock on windows and time.time on other systems.
- Little rewrite by Jens Diemer: - Change socket asterisk import to a normal import - Replace time.time() with time.clock() - Delete “return None” (or change to “return” only) - In checksum() rename “str” to “source_string”
- Changed the struct.pack() calls to pack the checksum and ID as unsigned. My thanks to Jerome Poincheval for the fix.
For some reason, the checksum bytes are in the wrong order when this is run under Solaris 2.X for SPARC but it works right under Linux x86. Since I don’t know just what’s wrong, I’ll swap the bytes always and then do an htons().
Initial hack. Doesn’t do much, but rather than try to guess what features I (or others) will want in the future, I’ve only put in what I need now.