OPSI.Util.Ping module

A pure python ping implementation using raw socket.

Note that ICMP messages can only be sent from processes running as root.

Derived from ping.c distributed in Linux’s netkit. That code is copyright (c) 1989 by The Regents of the University of California. That code is in turn derived from code written by Mike Muuss of the US Army Ballistic Research Laboratory in December, 1983 and placed in the public domain. They have my thanks.

Bugs are naturally mine. I’d be glad to hear about them. There are certainly word - size dependenceies here.

Copyright (c) Matthew Dixon Cowles, <http://www.visi.com/~mdc/>. Distributable under the terms of the GNU General Public License version 2. Provided with no warranties of any sort.

Original Version from Matthew Dixon Cowles:
-> ftp://ftp.visi.com/users/mdc/ping.py
Rewrite by Jens Diemer:
-> http://www.python-forum.de/post-69122.html#69122
Rewrite by George Notaras:
-> http://www.g-loaded.eu/2009/10/30/python-ping/

Revision history

November 8, 2009

Improved compatibility with GNU/Linux systems.

Fixes by:
Reported by:
Changes in this release:
  • Re-use time.time() instead of time.clock(). The 2007 implementation worked only under Microsoft Windows. Failed on GNU/Linux. time.clock() behaves differently under the two OSes[1].

[1] http://docs.python.org/library/time.html#time.clock

May 30, 2007

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”

November 22, 1997

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.

December 16, 1997

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().

December 4, 2000

Changed the struct.pack() calls to pack the checksum and ID as unsigned. My thanks to Jerome Poincheval for the fix.

Last commit info:

$LastChangedDate: $ $Rev: $ $Author: $

OPSI.Util.Ping.checksum(source_string)

I’m not too confident that this is right but testing seems to suggest that it gives the same answers as in_cksum in ping.c

OPSI.Util.Ping.ping(dest_addr, timeout=2)

Returns either the delay (in seconds) or none on timeout.

OPSI.Util.Ping.receive_one_ping(my_socket, ID, timeout)

receive the ping from the socket.

OPSI.Util.Ping.send_one_ping(my_socket, dest_addr, ID)

Send one ping to the given >dest_addr<.

OPSI.Util.Ping.verbose_ping(dest_addr, timeout=2, count=4)

Send >count< ping to >dest_addr< with the given >timeout< and display the result.