From c2a663aeb10e0190eae172cf938abc1fec3e5acc Mon Sep 17 00:00:00 2001 From: JensDiemer Date: Thu, 15 Dec 2005 15:10:44 +0000 Subject: [PATCH 01/61] Uploader v0.1 From 711fdab655cc339b97843fd4f33d239fbd4e2a36 Mon Sep 17 00:00:00 2001 From: JensDiemer Date: Wed, 7 Nov 2007 15:53:51 +0000 Subject: [PATCH 02/61] add ping.py --- ping.py | 205 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 205 insertions(+) create mode 100644 ping.py diff --git a/ping.py b/ping.py new file mode 100644 index 0000000..ff3ad95 --- /dev/null +++ b/ping.py @@ -0,0 +1,205 @@ +#!/usr/bin/env python + +""" + 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, . + 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 + + + Revision history + ~~~~~~~~~~~~~~~~ + + 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: $ +""" + + +import os, sys, socket, struct, select, time + +# From /usr/include/linux/icmp.h; your milage may vary. +ICMP_ECHO_REQUEST = 8 # Seems to be the same on Solaris. + + +def 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 + """ + sum = 0 + countTo = (len(source_string)/2)*2 + count = 0 + while count> 16) + (sum & 0xffff) + sum = sum + (sum >> 16) + answer = ~sum + answer = answer & 0xffff + + # Swap bytes. Bugger me if I know why. + answer = answer >> 8 | (answer << 8 & 0xff00) + + return answer + + +def receive_one_ping(my_socket, ID, timeout): + """ + receive the ping from the socket. + """ + timeLeft = timeout + while True: + startedSelect = time.clock() + whatReady = select.select([my_socket], [], [], timeLeft) + howLongInSelect = (time.clock() - startedSelect) + if whatReady[0] == []: # Timeout + return + + timeReceived = time.clock() + recPacket, addr = my_socket.recvfrom(1024) + icmpHeader = recPacket[20:28] + type, code, checksum, packetID, sequence = struct.unpack( + "bbHHh", icmpHeader + ) + if packetID == ID: + bytesInDouble = struct.calcsize("d") + timeSent = struct.unpack("d", recPacket[28:28 + bytesInDouble])[0] + return timeReceived - timeSent + + timeLeft = timeLeft - howLongInSelect + if timeLeft <= 0: + return + + +def send_one_ping(my_socket, dest_addr, ID): + """ + Send one ping to the given >dest_addr<. + """ + dest_addr = socket.gethostbyname(dest_addr) + + # Header is type (8), code (8), checksum (16), id (16), sequence (16) + my_checksum = 0 + + # Make a dummy heder with a 0 checksum. + header = struct.pack("bbHHh", ICMP_ECHO_REQUEST, 0, my_checksum, ID, 1) + bytesInDouble = struct.calcsize("d") + data = (192 - bytesInDouble) * "Q" + data = struct.pack("d", time.clock()) + data + + # Calculate the checksum on the data and the dummy header. + my_checksum = checksum(header + data) + + # Now that we have the right checksum, we put that in. It's just easier + # to make up a new header than to stuff it into the dummy. + header = struct.pack( + "bbHHh", ICMP_ECHO_REQUEST, 0, socket.htons(my_checksum), ID, 1 + ) + packet = header + data + my_socket.sendto(packet, (dest_addr, 1)) # Don't know about the 1 + + +def do_one(dest_addr, timeout): + """ + Returns either the delay (in seconds) or none on timeout. + """ + icmp = socket.getprotobyname("icmp") + try: + my_socket = socket.socket(socket.AF_INET, socket.SOCK_RAW, icmp) + except socket.error, (errno, msg): + if errno == 1: + # Operation not permitted + msg = msg + ( + " - Note that ICMP messages can only be sent from processes" + " running as root." + ) + raise socket.error(msg) + raise # raise the original error + + my_ID = os.getpid() & 0xFFFF + + send_one_ping(my_socket, dest_addr, my_ID) + delay = receive_one_ping(my_socket, my_ID, timeout) + + my_socket.close() + return delay + + +def verbose_ping(dest_addr, timeout = 2, count = 4): + """ + Send >count< ping to >dest_addr< with the given >timeout< and display + the result. + """ + for i in xrange(count): + print "ping %s..." % dest_addr, + try: + delay = do_one(dest_addr, timeout) + except socket.gaierror, e: + print "failed. (socket error: '%s')" % e[1] + break + + if delay == None: + print "failed. (timeout within %ssec.)" % timeout + else: + delay = delay * 1000 + print "get ping in %0.4fms" % delay + print + + +if __name__ == '__main__': + verbose_ping("heise.de") + verbose_ping("google.com") + verbose_ping("a-test-url-taht-is-not-available.com") + verbose_ping("192.168.1.1") \ No newline at end of file From a6fbbb8a6ee86f2ebb3e23eb8a1d090fd2985fdb Mon Sep 17 00:00:00 2001 From: JensDiemer Date: Thu, 24 Jun 2010 09:56:41 +0200 Subject: [PATCH 03/61] change back from time.clock() to time.time() --- ping.py | 20 ++++++-------------- 1 file changed, 6 insertions(+), 14 deletions(-) diff --git a/ping.py b/ping.py index ff3ad95..499a52c 100644 --- a/ping.py +++ b/ping.py @@ -33,7 +33,6 @@ 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" @@ -51,13 +50,6 @@ 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: $ """ @@ -102,13 +94,13 @@ def receive_one_ping(my_socket, ID, timeout): """ timeLeft = timeout while True: - startedSelect = time.clock() + startedSelect = time.time() whatReady = select.select([my_socket], [], [], timeLeft) - howLongInSelect = (time.clock() - startedSelect) + howLongInSelect = (time.time() - startedSelect) if whatReady[0] == []: # Timeout return - timeReceived = time.clock() + timeReceived = time.time() recPacket, addr = my_socket.recvfrom(1024) icmpHeader = recPacket[20:28] type, code, checksum, packetID, sequence = struct.unpack( @@ -137,7 +129,7 @@ def send_one_ping(my_socket, dest_addr, ID): header = struct.pack("bbHHh", ICMP_ECHO_REQUEST, 0, my_checksum, ID, 1) bytesInDouble = struct.calcsize("d") data = (192 - bytesInDouble) * "Q" - data = struct.pack("d", time.clock()) + data + data = struct.pack("d", time.time()) + data # Calculate the checksum on the data and the dummy header. my_checksum = checksum(header + data) @@ -199,7 +191,7 @@ def verbose_ping(dest_addr, timeout = 2, count = 4): if __name__ == '__main__': + verbose_ping("localhost") verbose_ping("heise.de") verbose_ping("google.com") - verbose_ping("a-test-url-taht-is-not-available.com") - verbose_ping("192.168.1.1") \ No newline at end of file + verbose_ping("a-test-url-taht-is-not-available.com") \ No newline at end of file From 9446b98bcd92162429424238c622480887536d01 Mon Sep 17 00:00:00 2001 From: JensDiemer Date: Wed, 7 Jul 2010 12:18:26 +0200 Subject: [PATCH 04/61] chmod +x --- ping.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) mode change 100644 => 100755 ping.py diff --git a/ping.py b/ping.py old mode 100644 new mode 100755 From 06d85e3fb111eb4bca87829e47e971fd91109a60 Mon Sep 17 00:00:00 2001 From: JensDiemer Date: Mon, 12 Sep 2011 12:33:34 +0200 Subject: [PATCH 05/61] add changes by George Notaras: http://www.g-loaded.eu/2009/10/30/python-ping/ --- ping.py | 82 +++++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 56 insertions(+), 26 deletions(-) diff --git a/ping.py b/ping.py index 499a52c..cf328e2 100755 --- a/ping.py +++ b/ping.py @@ -2,54 +2,84 @@ """ 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, . 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: + * George Notaras -- http://www.g-loaded.eu + Reported by: + * Chris Hallman -- http://cdhallman.blogspot.com + + 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: $ """ @@ -65,19 +95,19 @@ def checksum(source_string): to suggest that it gives the same answers as in_cksum in ping.c """ sum = 0 - countTo = (len(source_string)/2)*2 + countTo = (len(source_string) / 2) * 2 count = 0 - while count> 16) + (sum & 0xffff) + sum = (sum >> 16) + (sum & 0xffff) sum = sum + (sum >> 16) answer = ~sum answer = answer & 0xffff @@ -120,7 +150,7 @@ def send_one_ping(my_socket, dest_addr, ID): """ Send one ping to the given >dest_addr<. """ - dest_addr = socket.gethostbyname(dest_addr) + dest_addr = socket.gethostbyname(dest_addr) # Header is type (8), code (8), checksum (16), id (16), sequence (16) my_checksum = 0 @@ -169,7 +199,7 @@ def do_one(dest_addr, timeout): return delay -def verbose_ping(dest_addr, timeout = 2, count = 4): +def verbose_ping(dest_addr, timeout=2, count=4): """ Send >count< ping to >dest_addr< with the given >timeout< and display the result. @@ -177,21 +207,21 @@ def verbose_ping(dest_addr, timeout = 2, count = 4): for i in xrange(count): print "ping %s..." % dest_addr, try: - delay = do_one(dest_addr, timeout) + delay = do_one(dest_addr, timeout) except socket.gaierror, e: print "failed. (socket error: '%s')" % e[1] break - if delay == None: + if delay == None: print "failed. (timeout within %ssec.)" % timeout else: - delay = delay * 1000 + delay = delay * 1000 print "get ping in %0.4fms" % delay print if __name__ == '__main__': - verbose_ping("localhost") verbose_ping("heise.de") verbose_ping("google.com") - verbose_ping("a-test-url-taht-is-not-available.com") \ No newline at end of file + verbose_ping("a-test-url-taht-is-not-available.com") + verbose_ping("192.168.1.1") From 1a7b6369c67017463b5d48927bfddc552d071fc0 Mon Sep 17 00:00:00 2001 From: JensDiemer Date: Mon, 12 Sep 2011 12:34:18 +0200 Subject: [PATCH 06/61] Add enhancements by Martin Falatic: http://www.falatic.com/index.php/39/pinging-with-python --- ping.py | 441 +++++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 325 insertions(+), 116 deletions(-) diff --git a/ping.py b/ping.py index cf328e2..eea2097 100755 --- a/ping.py +++ b/ping.py @@ -1,53 +1,69 @@ #!/usr/bin/env python - """ - A pure python ping implementation using raw socket. - - - Note that ICMP messages can only be sent from processes running as root. - - + 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'). + 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. - + certainly word - size dependencies here. + Copyright (c) Matthew Dixon Cowles, . 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/ - + + Enhancements by Martin Falatic: + -> http://www.falatic.com/index.php/39/pinging-with-python + Revision history ~~~~~~~~~~~~~~~~ - + + September 6, 2011 + -------------- + 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. + + August 3, 2011 + -------------- + 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. + November 8, 2009 ---------------- Improved compatibility with GNU/Linux systems. - + Fixes by: * George Notaras -- http://www.g-loaded.eu Reported by: * Chris Hallman -- http://cdhallman.blogspot.com - + 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: @@ -55,173 +71,366 @@ - replace time.time() with time.clock() - delete "return None" (or change to "return" only) - in checksum() rename "str" to "source_string" - + + December 4, 2000 + ---------------- + Changed the struct.pack() calls to pack the checksum and ID as + unsigned. My thanks to Jerome Poincheval for the fix. + 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: $ + + =========================================================================== + IP header info from RFC791 + -> http://tools.ietf.org/html/rfc791) + + 0 1 2 3 + 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + |Version| IHL |Type of Service| Total Length | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + | Identification |Flags| Fragment Offset | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + | Time to Live | Protocol | Header Checksum | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + | Source Address | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + | Destination Address | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + | Options | Padding | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + + =========================================================================== + ICMP Echo / Echo Reply Message header info from RFC792 + -> http://tools.ietf.org/html/rfc792 + + 0 1 2 3 + 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + | Type | Code | Checksum | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + | Identifier | Sequence Number | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + | Data ... + +-+-+-+-+- + + =========================================================================== + ICMP parameter info: + -> http://www.iana.org/assignments/icmp-parameters/icmp-parameters.xml + + =========================================================================== + An example of ping's typical output: + + PING heise.de (193.99.144.80): 56 data bytes + 64 bytes from 193.99.144.80: icmp_seq=0 ttl=240 time=127 ms + 64 bytes from 193.99.144.80: icmp_seq=1 ttl=240 time=127 ms + 64 bytes from 193.99.144.80: icmp_seq=2 ttl=240 time=126 ms + 64 bytes from 193.99.144.80: icmp_seq=3 ttl=240 time=126 ms + 64 bytes from 193.99.144.80: icmp_seq=4 ttl=240 time=127 ms + + ----heise.de PING Statistics---- + 5 packets transmitted, 5 packets received, 0.0% packet loss + round-trip (ms) min/avg/max/med = 126/127/127/127 + + =========================================================================== """ +#=============================================================================# +import os, sys, socket, struct, select, time, signal + +#=============================================================================# +# ICMP parameters -import os, sys, socket, struct, select, time +ICMP_ECHOREPLY = 0 # Echo reply (per RFC792) +ICMP_ECHO = 8 # Echo request (per RFC792) +ICMP_MAX_RECV = 2048 # Max size of incoming buffer -# From /usr/include/linux/icmp.h; your milage may vary. -ICMP_ECHO_REQUEST = 8 # Seems to be the same on Solaris. +MAX_SLEEP = 1000 +class MyStats: + thisIP = "0.0.0.0" + pktsSent = 0 + pktsRcvd = 0 + minTime = 999999999 + maxTime = 0 + totTime = 0 + fracLoss = 1.0 +myStats = MyStats # Used globally + +#=============================================================================# def 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 + A port of the functionality of in_cksum() from ping.c + Ideally this would act on the string as a series of 16-bit ints (host + packed), but this works. + Network data is big-endian, hosts are typically little-endian """ + countTo = (int(len(source_string) / 2)) * 2 sum = 0 - countTo = (len(source_string) / 2) * 2 count = 0 + + # Handle bytes in pairs (decoding as short ints) + loByte = 0 + hiByte = 0 while count < countTo: - thisVal = ord(source_string[count + 1]) * 256 + ord(source_string[count]) - sum = sum + thisVal - sum = sum & 0xffffffff # Necessary? - count = count + 2 + if (sys.byteorder == "little"): + loByte = source_string[count] + hiByte = source_string[count + 1] + else: + loByte = source_string[count + 1] + hiByte = source_string[count] + sum = sum + (hiByte * 256 + loByte) + count += 2 - if countTo < len(source_string): - sum = sum + ord(source_string[len(source_string) - 1]) - sum = sum & 0xffffffff # Necessary? + # Handle last byte if applicable (odd-number of bytes) + # Endianness should be irrelevant in this case + if countTo < len(source_string): # Check for odd length + loByte = source_string[len(source_string) - 1] + sum += loByte - sum = (sum >> 16) + (sum & 0xffff) - sum = sum + (sum >> 16) - answer = ~sum - answer = answer & 0xffff + sum &= 0xffffffff # Truncate sum to 32 bits (a variance from ping.c, which + # uses signed ints, but overflow is unlikely in ping) - # Swap bytes. Bugger me if I know why. - answer = answer >> 8 | (answer << 8 & 0xff00) + sum = (sum >> 16) + (sum & 0xffff) # Add high 16 bits to low 16 bits + sum += (sum >> 16) # Add carry from above (if any) + answer = ~sum & 0xffff # Invert and truncate to 16 bits + answer = socket.htons(answer) return answer - -def receive_one_ping(my_socket, ID, timeout): +#=============================================================================# +def do_one(destIP, timeout, mySeqNumber, numDataBytes): """ - receive the ping from the socket. + Returns either the delay (in ms) or None on timeout. """ - timeLeft = timeout - while True: - startedSelect = time.time() - whatReady = select.select([my_socket], [], [], timeLeft) - howLongInSelect = (time.time() - startedSelect) - if whatReady[0] == []: # Timeout - return + global myStats - timeReceived = time.time() - recPacket, addr = my_socket.recvfrom(1024) - icmpHeader = recPacket[20:28] - type, code, checksum, packetID, sequence = struct.unpack( - "bbHHh", icmpHeader - ) - if packetID == ID: - bytesInDouble = struct.calcsize("d") - timeSent = struct.unpack("d", recPacket[28:28 + bytesInDouble])[0] - return timeReceived - timeSent + delay = None - timeLeft = timeLeft - howLongInSelect - if timeLeft <= 0: - return + try: # One could use UDP here, but it's obscure + mySocket = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.getprotobyname("icmp")) + except socket.error as e: + print("failed. (socket error: '%s')" % e.args[1]) + raise # raise the original error + + my_ID = os.getpid() & 0xFFFF + + sentTime = send_one_ping(mySocket, destIP, my_ID, mySeqNumber, numDataBytes) + if sentTime == None: + mySocket.close() + return delay + + myStats.pktsSent += 1; + recvTime, dataSize, iphSrcIP, icmpSeqNumber, iphTTL = receive_one_ping(mySocket, my_ID, timeout) -def send_one_ping(my_socket, dest_addr, ID): + mySocket.close() + + if recvTime: + delay = (recvTime - sentTime) * 1000 + print("%d bytes from %s: icmp_seq=%d ttl=%d time=%d ms" % ( + dataSize, socket.inet_ntoa(struct.pack("!I", iphSrcIP)), icmpSeqNumber, iphTTL, delay) + ) + myStats.pktsRcvd += 1; + myStats.totTime += delay + if myStats.minTime > delay: + myStats.minTime = delay + if myStats.maxTime < delay: + myStats.maxTime = delay + else: + delay = None + print("Request timed out.") + + return delay + +#=============================================================================# +def send_one_ping(mySocket, destIP, myID, mySeqNumber, numDataBytes): """ - Send one ping to the given >dest_addr<. + Send one ping to the given >destIP<. """ - dest_addr = socket.gethostbyname(dest_addr) + destIP = socket.gethostbyname(destIP) # Header is type (8), code (8), checksum (16), id (16), sequence (16) - my_checksum = 0 + myChecksum = 0 # Make a dummy heder with a 0 checksum. - header = struct.pack("bbHHh", ICMP_ECHO_REQUEST, 0, my_checksum, ID, 1) - bytesInDouble = struct.calcsize("d") - data = (192 - bytesInDouble) * "Q" - data = struct.pack("d", time.time()) + data + header = struct.pack( + "!BBHHH", ICMP_ECHO, 0, myChecksum, myID, mySeqNumber + ) + + padBytes = [] + startVal = 0x42 + for i in range(startVal, startVal + (numDataBytes)): + padBytes += [(i & 0xff)] # Keep chars in the 0-255 range + data = bytes(padBytes) # Calculate the checksum on the data and the dummy header. - my_checksum = checksum(header + data) + myChecksum = checksum(header + data) # Checksum is in network order # Now that we have the right checksum, we put that in. It's just easier # to make up a new header than to stuff it into the dummy. header = struct.pack( - "bbHHh", ICMP_ECHO_REQUEST, 0, socket.htons(my_checksum), ID, 1 + "!BBHHH", ICMP_ECHO, 0, myChecksum, myID, mySeqNumber ) + packet = header + data - my_socket.sendto(packet, (dest_addr, 1)) # Don't know about the 1 + sendTime = time.time() + + try: + mySocket.sendto(packet, (destIP, 1)) # Port number is irrelevant for ICMP + except socket.error as e: + print("General failure (%s)" % (e.args[1])) + return -def do_one(dest_addr, timeout): + return sendTime + +#=============================================================================# +def receive_one_ping(mySocket, myID, timeout): """ - Returns either the delay (in seconds) or none on timeout. + Receive the ping from the socket. Timeout = in ms """ - icmp = socket.getprotobyname("icmp") - try: - my_socket = socket.socket(socket.AF_INET, socket.SOCK_RAW, icmp) - except socket.error, (errno, msg): - if errno == 1: - # Operation not permitted - msg = msg + ( - " - Note that ICMP messages can only be sent from processes" - " running as root." - ) - raise socket.error(msg) - raise # raise the original error + timeLeft = timeout / 1000 - my_ID = os.getpid() & 0xFFFF + while True: # Loop while waiting for packet or timeout + startedSelect = time.time() + whatReady = select.select([mySocket], [], [], timeLeft) + howLongInSelect = (time.time() - startedSelect) + if whatReady[0] == []: # Timeout + return None, 0, 0, 0, 0 - send_one_ping(my_socket, dest_addr, my_ID) - delay = receive_one_ping(my_socket, my_ID, timeout) + timeReceived = time.time() - my_socket.close() - return delay + recPacket, addr = mySocket.recvfrom(ICMP_MAX_RECV) + + ipHeader = recPacket[:20] + iphVersion, iphTypeOfSvc, iphLength, \ + iphID, iphFlags, iphTTL, iphProtocol, \ + iphChecksum, iphSrcIP, iphDestIP = struct.unpack( + "!BBHHHBBHII", ipHeader + ) + + icmpHeader = recPacket[20:28] + icmpType, icmpCode, icmpChecksum, \ + icmpPacketID, icmpSeqNumber = struct.unpack( + "!BBHHH", icmpHeader + ) + if icmpPacketID == myID: # Our packet + dataSize = len(recPacket) - 28 + return timeReceived, dataSize, iphSrcIP, icmpSeqNumber, iphTTL + + timeLeft = timeLeft - howLongInSelect + if timeLeft <= 0: + return None, 0, 0, 0, 0 -def verbose_ping(dest_addr, timeout=2, count=4): +#=============================================================================# +def dump_stats(): """ - Send >count< ping to >dest_addr< with the given >timeout< and display + Show stats when pings are done + """ + global myStats + + print("\n----%s MYPING Statistics----" % (myStats.thisIP)) + + if myStats.pktsSent > 0: + myStats.fracLoss = (myStats.pktsSent - myStats.pktsRcvd) / myStats.pktsSent + + print("%d packets transmitted, %d packets received, %0.1f%% packet loss" % ( + myStats.pktsSent, myStats.pktsRcvd, 100.0 * myStats.fracLoss + )) + + if myStats.pktsRcvd > 0: + print("round-trip (ms) min/avg/max = %d/%0.1f/%d" % ( + myStats.minTime, myStats.totTime / myStats.pktsRcvd, myStats.maxTime + )) + + print() + return + +#=============================================================================# +def signal_handler(signum, frame): + """ + Handle exit via signals + """ + dump_stats() + print("\n(Terminated with signal %d)\n" % (signum)) + sys.exit(0) + +#=============================================================================# +def verbose_ping(hostname, timeout=1000, count=3, numDataBytes=55): + """ + Send >count< ping to >destIP< with the given >timeout< and display the result. """ - for i in xrange(count): - print "ping %s..." % dest_addr, - try: - delay = do_one(dest_addr, timeout) - except socket.gaierror, e: - print "failed. (socket error: '%s')" % e[1] - break + global myStats + + signal.signal(signal.SIGINT, signal_handler) # Handle Ctrl-C + signal.signal(signal.SIGBREAK, signal_handler) # Handle Windows Ctrl-Break + + myStats = MyStats() # Reset the stats + + mySeqNumber = 0 # Starting value + + try: + destIP = socket.gethostbyname(hostname) + print("\nMYPING %s (%s): %d data bytes" % (hostname, destIP, numDataBytes)) + except socket.gaierror as e: + print("\nMYPING: Unknown host: %s (%s)" % (hostname, e.args[1])) + print() + return + + myStats.thisIP = destIP + + for i in range(count): + delay = do_one(destIP, timeout, mySeqNumber, numDataBytes) if delay == None: - print "failed. (timeout within %ssec.)" % timeout - else: - delay = delay * 1000 - print "get ping in %0.4fms" % delay - print + delay = 0 + + mySeqNumber += 1 + + # Pause for the remainder of the MAX_SLEEP period (if applicable) + if (MAX_SLEEP > delay): + time.sleep((MAX_SLEEP - delay) / 1000) + dump_stats() +#=============================================================================# if __name__ == '__main__': + + # These should work: verbose_ping("heise.de") verbose_ping("google.com") - verbose_ping("a-test-url-taht-is-not-available.com") - verbose_ping("192.168.1.1") + + # Inconsistent on Windows w/ ActivePython (Python 3.2 resolves correctly + # to the local host, but 2.7 tries to resolve to the local *gateway*) + verbose_ping("localhost") + + # Should fail with 'getaddrinfo failed': + verbose_ping("foobar_url.foobar") + + # Should fail (timeout), but it depends on the local network: + verbose_ping("192.168.255.254") + + # Should fails with 'The requested address is not valid in its context': + verbose_ping("0.0.0.0") + +#=============================================================================# From 72883fbcf5018aa015b4e35a1661abc9fda97e16 Mon Sep 17 00:00:00 2001 From: JensDiemer Date: Mon, 12 Sep 2011 12:34:55 +0200 Subject: [PATCH 07/61] cleanup --- ping.py | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/ping.py b/ping.py index eea2097..039b158 100755 --- a/ping.py +++ b/ping.py @@ -1,4 +1,6 @@ #!/usr/bin/env python +# coding: utf-8 + """ A pure python ping implementation using raw sockets. @@ -90,12 +92,6 @@ Linux x86. Since I don't know just what's wrong, I'll swap the bytes always and then do an htons(). - Last commit info: - ~~~~~~~~~~~~~~~~~ - $LastChangedDate: $ - $Rev: $ - $Author: $ - =========================================================================== IP header info from RFC791 -> http://tools.ietf.org/html/rfc791) From f4225739226e520f64acda8c1bd83444185653a0 Mon Sep 17 00:00:00 2001 From: JensDiemer Date: Mon, 12 Sep 2011 12:52:50 +0200 Subject: [PATCH 08/61] Bugfixes + cleanup, tests with Ubuntu + Windows 7 --- ping.py | 54 ++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 34 insertions(+), 20 deletions(-) diff --git a/ping.py b/ping.py index 039b158..8b7ab91 100755 --- a/ping.py +++ b/ping.py @@ -35,6 +35,11 @@ Revision history ~~~~~~~~~~~~~~~~ + September 12, 2011 + -------------- + Bugfixes + cleanup by Jens Diemer + Tested with Ubuntu + Windows 7 + September 6, 2011 -------------- Cleanup by Martin Falatic. Restored lost comments and docs. Improved @@ -147,10 +152,10 @@ =========================================================================== """ -#=============================================================================# + import os, sys, socket, struct, select, time, signal -#=============================================================================# + # ICMP parameters ICMP_ECHOREPLY = 0 # Echo reply (per RFC792) @@ -170,7 +175,7 @@ class MyStats: myStats = MyStats # Used globally -#=============================================================================# + def checksum(source_string): """ A port of the functionality of in_cksum() from ping.c @@ -192,14 +197,14 @@ def checksum(source_string): else: loByte = source_string[count + 1] hiByte = source_string[count] - sum = sum + (hiByte * 256 + loByte) + sum = sum + (ord(hiByte) * 256 + ord(loByte)) count += 2 # Handle last byte if applicable (odd-number of bytes) # Endianness should be irrelevant in this case if countTo < len(source_string): # Check for odd length loByte = source_string[len(source_string) - 1] - sum += loByte + sum += ord(loByte) sum &= 0xffffffff # Truncate sum to 32 bits (a variance from ping.c, which # uses signed ints, but overflow is unlikely in ping) @@ -211,7 +216,7 @@ def checksum(source_string): return answer -#=============================================================================# + def do_one(destIP, timeout, mySeqNumber, numDataBytes): """ Returns either the delay (in ms) or None on timeout. @@ -222,8 +227,16 @@ def do_one(destIP, timeout, mySeqNumber, numDataBytes): try: # One could use UDP here, but it's obscure mySocket = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.getprotobyname("icmp")) - except socket.error as e: - print("failed. (socket error: '%s')" % e.args[1]) + except socket.error, (errno, msg): + if errno == 1: + # Operation not permitted - Add more information to traceback + etype, evalue, etb = sys.exc_info() + evalue = etype( + "%s - Note that ICMP messages can only be sent from processes running as root." % evalue + ) + raise etype, evalue, etb + + print("failed. (socket error: '%s')" % msg) raise # raise the original error my_ID = os.getpid() & 0xFFFF @@ -256,7 +269,7 @@ def do_one(destIP, timeout, mySeqNumber, numDataBytes): return delay -#=============================================================================# + def send_one_ping(mySocket, destIP, myID, mySeqNumber, numDataBytes): """ Send one ping to the given >destIP<. @@ -298,7 +311,7 @@ def send_one_ping(mySocket, destIP, myID, mySeqNumber, numDataBytes): return sendTime -#=============================================================================# + def receive_one_ping(mySocket, myID, timeout): """ Receive the ping from the socket. Timeout = in ms @@ -337,14 +350,14 @@ def receive_one_ping(mySocket, myID, timeout): if timeLeft <= 0: return None, 0, 0, 0, 0 -#=============================================================================# + def dump_stats(): """ Show stats when pings are done """ global myStats - print("\n----%s MYPING Statistics----" % (myStats.thisIP)) + print("\n----%s PYTHON PING Statistics----" % (myStats.thisIP)) if myStats.pktsSent > 0: myStats.fracLoss = (myStats.pktsSent - myStats.pktsRcvd) / myStats.pktsSent @@ -361,7 +374,7 @@ def dump_stats(): print() return -#=============================================================================# + def signal_handler(signum, frame): """ Handle exit via signals @@ -370,7 +383,7 @@ def signal_handler(signum, frame): print("\n(Terminated with signal %d)\n" % (signum)) sys.exit(0) -#=============================================================================# + def verbose_ping(hostname, timeout=1000, count=3, numDataBytes=55): """ Send >count< ping to >destIP< with the given >timeout< and display @@ -379,7 +392,9 @@ def verbose_ping(hostname, timeout=1000, count=3, numDataBytes=55): global myStats signal.signal(signal.SIGINT, signal_handler) # Handle Ctrl-C - signal.signal(signal.SIGBREAK, signal_handler) # Handle Windows Ctrl-Break + if hasattr(signal, "SIGBREAK"): + # Handle Ctrl-Break e.g. under Windows + signal.signal(signal.SIGBREAK, signal_handler) myStats = MyStats() # Reset the stats @@ -387,9 +402,9 @@ def verbose_ping(hostname, timeout=1000, count=3, numDataBytes=55): try: destIP = socket.gethostbyname(hostname) - print("\nMYPING %s (%s): %d data bytes" % (hostname, destIP, numDataBytes)) + print("\nPYTHON-PING %s (%s): %d data bytes" % (hostname, destIP, numDataBytes)) except socket.gaierror as e: - print("\nMYPING: Unknown host: %s (%s)" % (hostname, e.args[1])) + print("\nPYTHON-PING: Unknown host: %s (%s)" % (hostname, e.args[1])) print() return @@ -409,9 +424,8 @@ def verbose_ping(hostname, timeout=1000, count=3, numDataBytes=55): dump_stats() -#=============================================================================# -if __name__ == '__main__': +if __name__ == '__main__': # These should work: verbose_ping("heise.de") verbose_ping("google.com") @@ -429,4 +443,4 @@ def verbose_ping(hostname, timeout=1000, count=3, numDataBytes=55): # Should fails with 'The requested address is not valid in its context': verbose_ping("0.0.0.0") -#=============================================================================# + From 1b596651e61cad83c1d8e06a93493e0cdde5a644 Mon Sep 17 00:00:00 2001 From: JensDiemer Date: Wed, 12 Oct 2011 14:45:24 +0200 Subject: [PATCH 09/61] Add eclipse config --- .project | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 .project diff --git a/.project b/.project new file mode 100644 index 0000000..6695946 --- /dev/null +++ b/.project @@ -0,0 +1,17 @@ + + + python-ping + + + + + + org.python.pydev.PyDevBuilder + + + + + + org.python.pydev.pythonNature + + From 4c24a783616d6f2319e6f72288ae8ae1bd97563d Mon Sep 17 00:00:00 2001 From: JensDiemer Date: Wed, 12 Oct 2011 14:45:31 +0200 Subject: [PATCH 10/61] change README --- README | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/README b/README index 1488af0..5ebe26a 100644 --- a/README +++ b/README @@ -1,2 +1,4 @@ -Originally from http://svn.pylucid.net/pylucid/CodeSnippets/ping.py -This version maintained at http://github.com/samuel/python-ping \ No newline at end of file +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'). \ No newline at end of file From c43597d88d89e5e32999cc82a3139ade99a65098 Mon Sep 17 00:00:00 2001 From: JensDiemer Date: Wed, 12 Oct 2011 15:59:07 +0200 Subject: [PATCH 11/61] * move Stuff from DocString into seperate files * add a simple CLI - TODO: create a real one --- AUTHORS | 12 +++ HISTORY | 83 ++++++++++++++++++++ LICENSE | 11 +++ ping.py | 181 ++++++------------------------------------- ping_header_info.txt | 50 ++++++++++++ 5 files changed, 179 insertions(+), 158 deletions(-) create mode 100644 AUTHORS create mode 100644 HISTORY create mode 100644 LICENSE create mode 100644 ping_header_info.txt diff --git a/AUTHORS b/AUTHORS new file mode 100644 index 0000000..fbaa2f6 --- /dev/null +++ b/AUTHORS @@ -0,0 +1,12 @@ + +AUTHORS / CONTRIBUTORS (alphabetic order): + + * Cowles, Matthew Dixon -- ftp://ftp.visi.com/users/mdc/ping.py + * Diemer, Jens -- http://www.jensdiemer.de + * Falatic, Martin -- http://www.falatic.com + * Hallman, Chris -- http://cdhallman.blogspot.com + * Notaras, George -- http://www.g-loaded.eu + * Poincheval, Jerome + * Stauffer, Samuel + * Zach Ware + diff --git a/HISTORY b/HISTORY new file mode 100644 index 0000000..cfba9c4 --- /dev/null +++ b/HISTORY @@ -0,0 +1,83 @@ +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/ + +Enhancements by Martin Falatic: + -> http://www.falatic.com/index.php/39/pinging-with-python + + +Revision history +~~~~~~~~~~~~~~~~ + +September 12, 2011 +-------------- +Bugfixes + cleanup by Jens Diemer +Tested with Ubuntu + Windows 7 + +September 6, 2011 +-------------- +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. + +August 3, 2011 +-------------- +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. + +March 11, 2010 +-------------- +changes by Samuel Stauffer: +- replaced time.clock with default_timer which is set to + time.clock on windows and time.time on other systems. + +November 8, 2009 +---------------- +Improved compatibility with GNU/Linux systems. + +Fixes by: + * George Notaras -- http://www.g-loaded.eu +Reported by: + * Chris Hallman -- http://cdhallman.blogspot.com + +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" + +December 4, 2000 +---------------- +Changed the struct.pack() calls to pack the checksum and ID as +unsigned. My thanks to Jerome Poincheval for the fix. + +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(). \ No newline at end of file diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..9cc1267 --- /dev/null +++ b/LICENSE @@ -0,0 +1,11 @@ +The original code 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. + +Copyright (c) Matthew Dixon Cowles, . +Distributable under the terms of the GNU General Public License +version 2. Provided with no warranties of any sort. + +See AUTHORS for complete list of authors and contributors. \ No newline at end of file diff --git a/ping.py b/ping.py index 19550a1..e9e05f1 100755 --- a/ping.py +++ b/ping.py @@ -7,155 +7,12 @@ Note that ICMP messages can only be sent from processes running as root (in Windows, you must run this script as 'Administrator'). - 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 dependencies here. - - Copyright (c) Matthew Dixon Cowles, . - 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/ - - Enhancements by Martin Falatic: - -> http://www.falatic.com/index.php/39/pinging-with-python - - Revision history - ~~~~~~~~~~~~~~~~ - - September 12, 2011 - -------------- - Bugfixes + cleanup by Jens Diemer - Tested with Ubuntu + Windows 7 - September 6, 2011 - -------------- - 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. - - August 3, 2011 - -------------- - 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. - - March 11, 2010 - -------------- - changes by Samuel Stauffer: - - replaced time.clock with default_timer which is set to - time.clock on windows and time.time on other systems. - - November 8, 2009 - ---------------- - Improved compatibility with GNU/Linux systems. - - Fixes by: - * George Notaras -- http://www.g-loaded.eu - Reported by: - * Chris Hallman -- http://cdhallman.blogspot.com - - 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" - - December 4, 2000 - ---------------- - Changed the struct.pack() calls to pack the checksum and ID as - unsigned. My thanks to Jerome Poincheval for the fix. - - 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(). - - =========================================================================== - IP header info from RFC791 - -> http://tools.ietf.org/html/rfc791) - - 0 1 2 3 - 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 - +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ - |Version| IHL |Type of Service| Total Length | - +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ - | Identification |Flags| Fragment Offset | - +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ - | Time to Live | Protocol | Header Checksum | - +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ - | Source Address | - +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ - | Destination Address | - +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ - | Options | Padding | - +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ - - =========================================================================== - ICMP Echo / Echo Reply Message header info from RFC792 - -> http://tools.ietf.org/html/rfc792 - - 0 1 2 3 - 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 - +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ - | Type | Code | Checksum | - +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ - | Identifier | Sequence Number | - +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ - | Data ... - +-+-+-+-+- - - =========================================================================== - ICMP parameter info: - -> http://www.iana.org/assignments/icmp-parameters/icmp-parameters.xml - - =========================================================================== - An example of ping's typical output: - - PING heise.de (193.99.144.80): 56 data bytes - 64 bytes from 193.99.144.80: icmp_seq=0 ttl=240 time=127 ms - 64 bytes from 193.99.144.80: icmp_seq=1 ttl=240 time=127 ms - 64 bytes from 193.99.144.80: icmp_seq=2 ttl=240 time=126 ms - 64 bytes from 193.99.144.80: icmp_seq=3 ttl=240 time=126 ms - 64 bytes from 193.99.144.80: icmp_seq=4 ttl=240 time=127 ms - - ----heise.de PING Statistics---- - 5 packets transmitted, 5 packets received, 0.0% packet loss - round-trip (ms) min/avg/max/med = 126/127/127/127 - - =========================================================================== + :homepage: https://github.com/jedie/python-ping/ + :copyleft: 1989-2011 by the python-ping team, see AUTHORS for more details. + :license: GNU GPL v2, see LICENSE for more details. """ @@ -440,21 +297,29 @@ def verbose_ping(hostname, timeout=1000, count=3, numDataBytes=55): if __name__ == '__main__': - # These should work: - verbose_ping("heise.de") - verbose_ping("google.com") + # FIXME: Add a real CLI + if len(sys.argv) == 0: + print "DEMO" - # Inconsistent on Windows w/ ActivePython (Python 3.2 resolves correctly - # to the local host, but 2.7 tries to resolve to the local *gateway*) - verbose_ping("localhost") + # These should work: + verbose_ping("heise.de") + verbose_ping("google.com") - # Should fail with 'getaddrinfo failed': - verbose_ping("foobar_url.foobar") + # Inconsistent on Windows w/ ActivePython (Python 3.2 resolves correctly + # to the local host, but 2.7 tries to resolve to the local *gateway*) + verbose_ping("localhost") - # Should fail (timeout), but it depends on the local network: - verbose_ping("192.168.255.254") + # Should fail with 'getaddrinfo failed': + verbose_ping("foobar_url.foobar") - # Should fails with 'The requested address is not valid in its context': - verbose_ping("0.0.0.0") + # Should fail (timeout), but it depends on the local network: + verbose_ping("192.168.255.254") + + # Should fails with 'The requested address is not valid in its context': + verbose_ping("0.0.0.0") + elif len(sys.argv) == 2: + verbose_ping(sys.argv[1]) + else: + print "Error: call ./ping.py domain.tld" diff --git a/ping_header_info.txt b/ping_header_info.txt new file mode 100644 index 0000000..371c729 --- /dev/null +++ b/ping_header_info.txt @@ -0,0 +1,50 @@ +IP header info from RFC791 + -> http://tools.ietf.org/html/rfc791) + +0 1 2 3 +0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 ++-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ +|Version| IHL |Type of Service| Total Length | ++-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ +| Identification |Flags| Fragment Offset | ++-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ +| Time to Live | Protocol | Header Checksum | ++-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ +| Source Address | ++-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ +| Destination Address | ++-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ +| Options | Padding | ++-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + +=========================================================================== +ICMP Echo / Echo Reply Message header info from RFC792 + -> http://tools.ietf.org/html/rfc792 + + 0 1 2 3 + 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + | Type | Code | Checksum | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + | Identifier | Sequence Number | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + | Data ... + +-+-+-+-+- + +=========================================================================== +ICMP parameter info: + -> http://www.iana.org/assignments/icmp-parameters/icmp-parameters.xml + +=========================================================================== +An example of ping's typical output: + +PING heise.de (193.99.144.80): 56 data bytes +64 bytes from 193.99.144.80: icmp_seq=0 ttl=240 time=127 ms +64 bytes from 193.99.144.80: icmp_seq=1 ttl=240 time=127 ms +64 bytes from 193.99.144.80: icmp_seq=2 ttl=240 time=126 ms +64 bytes from 193.99.144.80: icmp_seq=3 ttl=240 time=126 ms +64 bytes from 193.99.144.80: icmp_seq=4 ttl=240 time=127 ms + +----heise.de PING Statistics---- +5 packets transmitted, 5 packets received, 0.0% packet loss +round-trip (ms) min/avg/max/med = 126/127/127/127 \ No newline at end of file From 0485d31001617e173f52dd498e1fea01c7ecce39 Mon Sep 17 00:00:00 2001 From: JensDiemer Date: Wed, 12 Oct 2011 15:59:21 +0200 Subject: [PATCH 12/61] add some information into README --- README | 4 ---- README.creole | 25 +++++++++++++++++++++++++ 2 files changed, 25 insertions(+), 4 deletions(-) delete mode 100644 README create mode 100644 README.creole diff --git a/README b/README deleted file mode 100644 index 5ebe26a..0000000 --- a/README +++ /dev/null @@ -1,4 +0,0 @@ -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'). \ No newline at end of file diff --git a/README.creole b/README.creole new file mode 100644 index 0000000..aad53fb --- /dev/null +++ b/README.creole @@ -0,0 +1,25 @@ +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'). + +=== usage === + +{{{ +~/python-ping$ sudo ./ping.py google.com + +PYTHON-PING google.com (74.125.39.147): 55 data bytes +64 bytes from 74.125.39.147: icmp_seq=0 ttl=53 time=23 ms +64 bytes from 74.125.39.147: icmp_seq=1 ttl=52 time=20 ms +64 bytes from 74.125.39.147: icmp_seq=2 ttl=53 time=22 ms + +----74.125.39.147 PYTHON PING Statistics---- +3 packets transmitted, 3 packets received, 0.0% packet loss +round-trip (ms) min/avg/max = 20/22.4/23 +}}} + +=== TODOs === + +* refactor ping.py +* create a CLI interface +* add a "suprocess ping", with output parser \ No newline at end of file From a876bacd7a72490fd578bf340fce5c4fdab5b4b7 Mon Sep 17 00:00:00 2001 From: JensDiemer Date: Wed, 12 Oct 2011 15:59:31 +0200 Subject: [PATCH 13/61] create a setup.py --- MANIFEST.in | 4 ++ setup.py | 126 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 130 insertions(+) create mode 100644 MANIFEST.in create mode 100755 setup.py diff --git a/MANIFEST.in b/MANIFEST.in new file mode 100644 index 0000000..cb46d09 --- /dev/null +++ b/MANIFEST.in @@ -0,0 +1,4 @@ +include AUTHORS HISTORY LICENSE MANIFEST.in README.creole +recursive-include *.py +recursive-exclude * *.pyc +recursive-exclude * *.pyo \ No newline at end of file diff --git a/setup.py b/setup.py new file mode 100755 index 0000000..8dba88e --- /dev/null +++ b/setup.py @@ -0,0 +1,126 @@ +#!/usr/bin/env python +# coding: utf-8 + +""" + distutils setup + ~~~~~~~~~~~~~~~ + + :homepage: https://github.com/jedie/python-ping/ + :copyleft: 1989-2011 by the python-ping team, see AUTHORS for more details. + :license: GNU GPL v2, see LICENSE for more details. +""" + +import os +import subprocess +import sys +import time +import warnings + +from setuptools import setup, find_packages, Command + +PACKAGE_ROOT = os.path.dirname(os.path.abspath(__file__)) + + +#VERBOSE = True +VERBOSE = False + +def _error(msg): + if VERBOSE: + warnings.warn(msg) + return "" + +def get_version_from_git(): + try: + process = subprocess.Popen( + # %ct: committer date, UNIX timestamp + ["/usr/bin/git", "log", "--pretty=format:%ct-%h", "-1", "HEAD"], + shell=False, cwd=PACKAGE_ROOT, + stdout=subprocess.PIPE, stderr=subprocess.PIPE, + ) + except Exception, err: + return _error("Can't get git hash: %s" % err) + + process.wait() + returncode = process.returncode + if returncode != 0: + return _error( + "Can't get git hash, returncode was: %r" + " - git stdout: %r" + " - git stderr: %r" + % (returncode, process.stdout.readline(), process.stderr.readline()) + ) + + output = process.stdout.readline().strip() + try: + raw_timestamp, hash = output.split("-", 1) + timestamp = int(raw_timestamp) + except Exception, err: + return _error("Error in git log output! Output was: %r" % output) + + try: + timestamp_formatted = time.strftime("%Y.%m.%d", time.gmtime(timestamp)) + except Exception, err: + return _error("can't convert %r to time string: %s" % (timestamp, err)) + + return "%s.%s" % (timestamp_formatted, hash) + + +# convert creole to ReSt on-the-fly, see also: +# https://code.google.com/p/python-creole/wiki/UseInSetup +try: + from creole.setup_utils import get_long_description +except ImportError: + if "register" in sys.argv or "sdist" in sys.argv or "--long-description" in sys.argv: + etype, evalue, etb = sys.exc_info() + evalue = etype("%s - Please install python-creole >= v0.8 - e.g.: pip install python-creole" % evalue) + raise etype, evalue, etb + long_description = None +else: + long_description = get_long_description(PACKAGE_ROOT) + + +def get_authors(): + authors = [] + try: + f = file(os.path.join(PACKAGE_ROOT, "AUTHORS"), "r") + for line in f: + if not line.strip().startswith("*"): + continue + if "--" in line: + line = line.split("--", 1)[0] + authors.append(line.strip(" *\r\n")) + f.close() + authors.sort() + except Exception, err: + authors = "[Error: %s]" % err + return authors + + +setup( + name='python-ping', + version=get_version_from_git(), + description='A pure python ICMP ping implementation using raw sockets.', + long_description=long_description, + author=get_authors(), + maintainer="Jens Diemer", + maintainer_email="python-ping@jensdiemer.de", + url='https://github.com/jedie/python-ping/', + packages=find_packages(), + include_package_data=True, # include package data under svn source control + zip_safe=False, + classifiers=[ + # http://pypi.python.org/pypi?%3Aaction=list_classifiers +# "Development Status :: 4 - Beta", + "Development Status :: 5 - Production/Stable", + "Environment :: Web Environment", + "Intended Audience :: Developers", + "Intended Audience :: Education", + "Intended Audience :: End Users/Desktop", + "License :: OSI Approved :: GNU General Public License (GPL)", + "Operating System :: OS Independent", + "Programming Language :: Python", + "Topic :: Internet", + "Topic :: Software Development :: Libraries :: Python Modules", + "Topic :: System :: Networking :: Monitoring", + ], +) From cf3c716605a80c4e70567abbabeb80b23244b103 Mon Sep 17 00:00:00 2001 From: JensDiemer Date: Wed, 12 Oct 2011 16:11:41 +0200 Subject: [PATCH 14/61] put HISTORY into README --- HISTORY | 83 --------------------------------------------------- README.creole | 76 ++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 74 insertions(+), 85 deletions(-) delete mode 100644 HISTORY diff --git a/HISTORY b/HISTORY deleted file mode 100644 index cfba9c4..0000000 --- a/HISTORY +++ /dev/null @@ -1,83 +0,0 @@ -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/ - -Enhancements by Martin Falatic: - -> http://www.falatic.com/index.php/39/pinging-with-python - - -Revision history -~~~~~~~~~~~~~~~~ - -September 12, 2011 --------------- -Bugfixes + cleanup by Jens Diemer -Tested with Ubuntu + Windows 7 - -September 6, 2011 --------------- -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. - -August 3, 2011 --------------- -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. - -March 11, 2010 --------------- -changes by Samuel Stauffer: -- replaced time.clock with default_timer which is set to - time.clock on windows and time.time on other systems. - -November 8, 2009 ----------------- -Improved compatibility with GNU/Linux systems. - -Fixes by: - * George Notaras -- http://www.g-loaded.eu -Reported by: - * Chris Hallman -- http://cdhallman.blogspot.com - -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" - -December 4, 2000 ----------------- -Changed the struct.pack() calls to pack the checksum and ID as -unsigned. My thanks to Jerome Poincheval for the fix. - -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(). \ No newline at end of file diff --git a/README.creole b/README.creole index aad53fb..3c4a2bf 100644 --- a/README.creole +++ b/README.creole @@ -3,6 +3,12 @@ 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 [[ftp://ftp.visi.com/users/mdc/ping.py|Matthew Dixon Cowles]] + +* copyleft 1989-2011 by the python-ping team, see [[https://github.com/jedie/python-ping/blob/master/AUTHORS|AUTHORS]] for more details. +* license: GNU GPL v2, see [[https://github.com/jedie/python-ping/blob/master/LICENSE|LICENSE]] for more details. + + === usage === {{{ @@ -18,8 +24,74 @@ PYTHON-PING google.com (74.125.39.147): 55 data bytes round-trip (ms) min/avg/max = 20/22.4/23 }}} -=== TODOs === + +== TODOs == * refactor ping.py * create a CLI interface -* add a "suprocess ping", with output parser \ No newline at end of file +* add a "suprocess ping", with output parser + +== Revision history == + +==== Oct. 12, 2011 ==== +Merge sources and create a seperate github repository: +* https://github.com/jedie/python-ping + +Add a simple CLI interface. + +==== September 12, 2011 ==== +Bugfixes + cleanup by Jens Diemer +Tested with Ubuntu + Windows 7 + +==== September 6, 2011 ==== +[[http://www.falatic.com/index.php/39/pinging-with-python|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. + +==== August 3, 2011 ==== +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. + +==== March 11, 2010 ==== +changes by Samuel Stauffer: +replaced time.clock with default_timer which is set to +time.clock on windows and time.time on other systems. + +==== November 8, 2009 ==== +Fixes by [[http://www.g-loaded.eu/2009/10/30/python-ping/|George Notaras]], +reported by [[http://cdhallman.blogspot.com|Chris Hallman]]: + +Improved compatibility with GNU/Linux systems. + +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 [[http://docs.python.org/library/time.html#time.clock|the two OSes]]. + +==== May 30, 2007 ==== +little [[http://www.python-forum.de/post-69122.html#69122|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" + +==== December 4, 2000 ==== +Changed the struct.pack() calls to pack the checksum and ID as +unsigned. My thanks to Jerome Poincheval for the fix. + +==== 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(). From 11fc9f7c7f3b6ce07092060c3d2cf3f2a8783ea4 Mon Sep 17 00:00:00 2001 From: JensDiemer Date: Wed, 12 Oct 2011 16:11:55 +0200 Subject: [PATCH 15/61] remove --- MANIFEST.in | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/MANIFEST.in b/MANIFEST.in index cb46d09..244d0be 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -1,4 +1,4 @@ -include AUTHORS HISTORY LICENSE MANIFEST.in README.creole +include AUTHORS LICENSE MANIFEST.in README.creole recursive-include *.py recursive-exclude * *.pyc recursive-exclude * *.pyo \ No newline at end of file From 40a509efb7a865528022c29a4cee3aff34453b83 Mon Sep 17 00:00:00 2001 From: zed Date: Wed, 12 Oct 2011 18:11:58 +0400 Subject: [PATCH 16/61] actually use defined default_timer() instead of time.time() --- ping.py | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/ping.py b/ping.py index e9e05f1..739cdf1 100755 --- a/ping.py +++ b/ping.py @@ -172,7 +172,7 @@ def send_one_ping(mySocket, destIP, myID, mySeqNumber, numDataBytes): packet = header + data - sendTime = time.time() + sendTime = default_timer() try: mySocket.sendto(packet, (destIP, 1)) # Port number is irrelevant for ICMP @@ -190,13 +190,13 @@ def receive_one_ping(mySocket, myID, timeout): timeLeft = timeout / 1000 while True: # Loop while waiting for packet or timeout - startedSelect = time.time() + startedSelect = default_timer() whatReady = select.select([mySocket], [], [], timeLeft) - howLongInSelect = (time.time() - startedSelect) + howLongInSelect = (default_timer() - startedSelect) if whatReady[0] == []: # Timeout return None, 0, 0, 0, 0 - timeReceived = time.time() + timeReceived = default_timer() recPacket, addr = mySocket.recvfrom(ICMP_MAX_RECV) @@ -321,5 +321,3 @@ def verbose_ping(hostname, timeout=1000, count=3, numDataBytes=55): verbose_ping(sys.argv[1]) else: print "Error: call ./ping.py domain.tld" - - From a100a2dfa3425061196069192a0cc590563be9bd Mon Sep 17 00:00:00 2001 From: JensDiemer Date: Wed, 12 Oct 2011 16:19:45 +0200 Subject: [PATCH 17/61] add \"zed\" see: https://github.com/jedie/python-ping/pull/2 --- AUTHORS | 1 + 1 file changed, 1 insertion(+) diff --git a/AUTHORS b/AUTHORS index fbaa2f6..1418452 100644 --- a/AUTHORS +++ b/AUTHORS @@ -9,4 +9,5 @@ AUTHORS / CONTRIBUTORS (alphabetic order): * Poincheval, Jerome * Stauffer, Samuel * Zach Ware + * zed -- https://github.com/zed From 4d170546a7aefd46cd3ec64006bb9c0101dcb9b2 Mon Sep 17 00:00:00 2001 From: Jens Diemer Date: Wed, 12 Oct 2011 19:04:37 +0300 Subject: [PATCH 18/61] add FIXME: Don't use global --- ping.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ping.py b/ping.py index 739cdf1..380653b 100755 --- a/ping.py +++ b/ping.py @@ -44,7 +44,7 @@ class MyStats: totTime = 0 fracLoss = 1.0 -myStats = MyStats # Used globally +myStats = MyStats # Used globally FIXME: Don't use global def checksum(source_string): From 51f5b32d549d6ab664a7417ec432ea5c7bd055ea Mon Sep 17 00:00:00 2001 From: Jens Diemer Date: Wed, 12 Oct 2011 19:13:24 +0300 Subject: [PATCH 19/61] IMHO not needed. --- ping.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/ping.py b/ping.py index 380653b..bd42911 100755 --- a/ping.py +++ b/ping.py @@ -145,8 +145,6 @@ def send_one_ping(mySocket, destIP, myID, mySeqNumber, numDataBytes): """ Send one ping to the given >destIP<. """ - destIP = socket.gethostbyname(destIP) - # Header is type (8), code (8), checksum (16), id (16), sequence (16) myChecksum = 0 @@ -273,6 +271,7 @@ def verbose_ping(hostname, timeout=1000, count=3, numDataBytes=55): try: destIP = socket.gethostbyname(hostname) + # FIXME: Use destIP only for display this line here? see: https://github.com/jedie/python-ping/issues/3 print("\nPYTHON-PING %s (%s): %d data bytes" % (hostname, destIP, numDataBytes)) except socket.gaierror as e: print("\nPYTHON-PING: Unknown host: %s (%s)" % (hostname, e.args[1])) From dbf7d09da288626f202da6f22f2a8eaab0336e17 Mon Sep 17 00:00:00 2001 From: Jens Diemer Date: Wed, 12 Oct 2011 19:14:53 +0300 Subject: [PATCH 20/61] print() -> print("") --- ping.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/ping.py b/ping.py index bd42911..7a8b028 100755 --- a/ping.py +++ b/ping.py @@ -240,8 +240,7 @@ def dump_stats(): myStats.minTime, myStats.totTime / myStats.pktsRcvd, myStats.maxTime )) - print() - return + print("") def signal_handler(signum, frame): @@ -275,7 +274,7 @@ def verbose_ping(hostname, timeout=1000, count=3, numDataBytes=55): print("\nPYTHON-PING %s (%s): %d data bytes" % (hostname, destIP, numDataBytes)) except socket.gaierror as e: print("\nPYTHON-PING: Unknown host: %s (%s)" % (hostname, e.args[1])) - print() + print("") return myStats.thisIP = destIP From 5fae12da02d0ccee33d6dbe6724f1fd804661add Mon Sep 17 00:00:00 2001 From: Jens Diemer Date: Wed, 12 Oct 2011 19:19:15 +0300 Subject: [PATCH 21/61] Bugfix in stupid CLI solution. -> https://github.com/jedie/python-ping/issues/5 --- ping.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ping.py b/ping.py index 7a8b028..ddab858 100755 --- a/ping.py +++ b/ping.py @@ -296,7 +296,7 @@ def verbose_ping(hostname, timeout=1000, count=3, numDataBytes=55): if __name__ == '__main__': # FIXME: Add a real CLI - if len(sys.argv) == 0: + if len(sys.argv) == 1: print "DEMO" # These should work: From 7aa3e9cda7d4351c92618509683518582fab2646 Mon Sep 17 00:00:00 2001 From: JensDiemer Date: Wed, 12 Oct 2011 18:21:54 +0200 Subject: [PATCH 22/61] add .gitignore --- .gitignore | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..611e05b --- /dev/null +++ b/.gitignore @@ -0,0 +1,7 @@ +*.py[co] +*~ +*.egg-info +/dist +/build +.pydevproject +/.settings \ No newline at end of file From 1db163ba81ed49b2d924fd92072ed183bcc271f9 Mon Sep 17 00:00:00 2001 From: Jens Diemer Date: Wed, 12 Oct 2011 19:26:24 +0300 Subject: [PATCH 23/61] add contribute --- README.creole | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/README.creole b/README.creole index 3c4a2bf..964e6a1 100644 --- a/README.creole +++ b/README.creole @@ -31,6 +31,12 @@ round-trip (ms) min/avg/max = 20/22.4/23 * create a CLI interface * add a "suprocess ping", with output parser + +== contribute == + +[[http://help.github.com/fork-a-repo/|Fork this repo]] on [[https://github.com/jedie/python-ping/|GitHub]] and [[http://help.github.com/send-pull-requests/|send pull requests]]. Thank you. + + == Revision history == ==== Oct. 12, 2011 ==== From bf82e931ba78693e1c97ac878b6386e9ea3a60a1 Mon Sep 17 00:00:00 2001 From: JensDiemer Date: Wed, 12 Oct 2011 20:41:33 +0200 Subject: [PATCH 24/61] * refactor variable names * display some stats as float --- ping.py | 168 +++++++++++++++++++++++++++++--------------------------- 1 file changed, 86 insertions(+), 82 deletions(-) diff --git a/ping.py b/ping.py index ddab858..621ba60 100755 --- a/ping.py +++ b/ping.py @@ -4,7 +4,7 @@ """ A pure python ping implementation using raw sockets. - Note that ICMP messages can only be sent from processes running as root + Note that ICMP messages can only be send from processes running as root (in Windows, you must run this script as 'Administrator'). Bugs are naturally mine. I'd be glad to hear about them. There are @@ -35,16 +35,16 @@ MAX_SLEEP = 1000 -class MyStats: - thisIP = "0.0.0.0" - pktsSent = 0 - pktsRcvd = 0 - minTime = 999999999 - maxTime = 0 - totTime = 0 - fracLoss = 1.0 +class PingStats: + dest_ip = "0.0.0.0" + send_count = 0 + receive_count = 0 + min_time = 999999999 + max_time = 0 + total_time = 0 + lost_count = 1.0 -myStats = MyStats # Used globally FIXME: Don't use global +current_stats = PingStats # Used globally FIXME: Don't use global def checksum(source_string): @@ -87,53 +87,57 @@ def checksum(source_string): return answer +class PingBase(object): + def __init__(self, dest_ip): + self.dest_ip = dest_ip -def do_one(destIP, timeout, mySeqNumber, numDataBytes): + +def do_one(dest_ip, deadline, seq_number, packet_size): """ - Returns either the delay (in ms) or None on timeout. + Returns either the delay (in ms) or None on deadline. """ - global myStats + global current_stats delay = None try: # One could use UDP here, but it's obscure - mySocket = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.getprotobyname("icmp")) + current_socket = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.getprotobyname("icmp")) except socket.error, (errno, msg): if errno == 1: # Operation not permitted - Add more information to traceback etype, evalue, etb = sys.exc_info() evalue = etype( - "%s - Note that ICMP messages can only be sent from processes running as root." % evalue + "%s - Note that ICMP messages can only be send from processes running as root." % evalue ) raise etype, evalue, etb print("failed. (socket error: '%s')" % msg) raise # raise the original error - my_ID = os.getpid() & 0xFFFF + own_id = os.getpid() & 0xFFFF - sentTime = send_one_ping(mySocket, destIP, my_ID, mySeqNumber, numDataBytes) - if sentTime == None: - mySocket.close() + send_time = send_one_ping(current_socket, dest_ip, own_id, seq_number, packet_size) + if send_time == None: + current_socket.close() return delay - myStats.pktsSent += 1; + current_stats.send_count += 1; - recvTime, dataSize, iphSrcIP, icmpSeqNumber, iphTTL = receive_one_ping(mySocket, my_ID, timeout) + receive_time, dataSize, ip_src_ip, icmp_seq_number, ip_ttl = receive_one_ping(current_socket, own_id, deadline) - mySocket.close() + current_socket.close() - if recvTime: - delay = (recvTime - sentTime) * 1000 - print("%d bytes from %s: icmp_seq=%d ttl=%d time=%d ms" % ( - dataSize, socket.inet_ntoa(struct.pack("!I", iphSrcIP)), icmpSeqNumber, iphTTL, delay) + if receive_time: + delay = (receive_time - send_time) * 1000.0 + print("%d bytes from %s: icmp_seq=%d ttl=%d time=%.1f ms" % ( + dataSize, socket.inet_ntoa(struct.pack("!I", ip_src_ip)), icmp_seq_number, ip_ttl, delay) ) - myStats.pktsRcvd += 1; - myStats.totTime += delay - if myStats.minTime > delay: - myStats.minTime = delay - if myStats.maxTime < delay: - myStats.maxTime = delay + current_stats.receive_count += 1; + current_stats.total_time += delay + if current_stats.min_time > delay: + current_stats.min_time = delay + if current_stats.max_time < delay: + current_stats.max_time = delay else: delay = None print("Request timed out.") @@ -141,21 +145,21 @@ def do_one(destIP, timeout, mySeqNumber, numDataBytes): return delay -def send_one_ping(mySocket, destIP, myID, mySeqNumber, numDataBytes): +def send_one_ping(current_socket, dest_ip, own_id, seq_number, packet_size): """ - Send one ping to the given >destIP<. + Send one ping to the given >dest_ip<. """ # Header is type (8), code (8), checksum (16), id (16), sequence (16) myChecksum = 0 # Make a dummy heder with a 0 checksum. header = struct.pack( - "!BBHHH", ICMP_ECHO, 0, myChecksum, myID, mySeqNumber + "!BBHHH", ICMP_ECHO, 0, myChecksum, own_id, seq_number ) padBytes = [] startVal = 0x42 - for i in range(startVal, startVal + (numDataBytes)): + for i in range(startVal, startVal + (packet_size)): padBytes += [(i & 0xff)] # Keep chars in the 0-255 range data = bytes(padBytes) @@ -165,7 +169,7 @@ def send_one_ping(mySocket, destIP, myID, mySeqNumber, numDataBytes): # Now that we have the right checksum, we put that in. It's just easier # to make up a new header than to stuff it into the dummy. header = struct.pack( - "!BBHHH", ICMP_ECHO, 0, myChecksum, myID, mySeqNumber + "!BBHHH", ICMP_ECHO, 0, myChecksum, own_id, seq_number ) packet = header + data @@ -173,7 +177,7 @@ def send_one_ping(mySocket, destIP, myID, mySeqNumber, numDataBytes): sendTime = default_timer() try: - mySocket.sendto(packet, (destIP, 1)) # Port number is irrelevant for ICMP + current_socket.sendto(packet, (dest_ip, 1)) # Port number is irrelevant for ICMP except socket.error as e: print("General failure (%s)" % (e.args[1])) return @@ -181,42 +185,42 @@ def send_one_ping(mySocket, destIP, myID, mySeqNumber, numDataBytes): return sendTime -def receive_one_ping(mySocket, myID, timeout): +def receive_one_ping(current_socket, own_id, deadline): """ - Receive the ping from the socket. Timeout = in ms + Receive the ping from the socket. deadline = in ms """ - timeLeft = timeout / 1000 + timeout = deadline / 1000 - while True: # Loop while waiting for packet or timeout - startedSelect = default_timer() - whatReady = select.select([mySocket], [], [], timeLeft) - howLongInSelect = (default_timer() - startedSelect) - if whatReady[0] == []: # Timeout + while True: # Loop while waiting for packet or deadline + select_start = default_timer() + inputready, outputready, exceptready = select.select([current_socket], [], [], timeout) + select_duration = (default_timer() - select_start) + if inputready == []: # deadline return None, 0, 0, 0, 0 - timeReceived = default_timer() + receive_time = default_timer() - recPacket, addr = mySocket.recvfrom(ICMP_MAX_RECV) + packet_data, address = current_socket.recvfrom(ICMP_MAX_RECV) - ipHeader = recPacket[:20] - iphVersion, iphTypeOfSvc, iphLength, \ - iphID, iphFlags, iphTTL, iphProtocol, \ - iphChecksum, iphSrcIP, iphDestIP = struct.unpack( - "!BBHHHBBHII", ipHeader + ip_header = packet_data[:20] + ip_version, ip_type, ip_length, \ + ip_id, ip_flags, ip_ttl, ip_protocol, \ + ip_checksum, ip_src_ip, ip_dest_ip = struct.unpack( + "!BBHHHBBHII", ip_header ) - icmpHeader = recPacket[20:28] - icmpType, icmpCode, icmpChecksum, \ - icmpPacketID, icmpSeqNumber = struct.unpack( - "!BBHHH", icmpHeader + icmp_header = packet_data[20:28] + icmp_type, icmp_code, icmp_checksum, \ + icmp_packet_id, icmp_seq_number = struct.unpack( + "!BBHHH", icmp_header ) - if icmpPacketID == myID: # Our packet - dataSize = len(recPacket) - 28 - return timeReceived, dataSize, iphSrcIP, icmpSeqNumber, iphTTL + if icmp_packet_id == own_id: # Our packet + dataSize = len(packet_data) - 28 + return receive_time, dataSize, ip_src_ip, icmp_seq_number, ip_ttl - timeLeft = timeLeft - howLongInSelect - if timeLeft <= 0: + timeout = timeout - select_duration + if timeout <= 0: return None, 0, 0, 0, 0 @@ -224,20 +228,20 @@ def dump_stats(): """ Show stats when pings are done """ - global myStats + global current_stats - print("\n----%s PYTHON PING Statistics----" % (myStats.thisIP)) + print("\n----%s PYTHON PING Statistics----" % (current_stats.dest_ip)) - if myStats.pktsSent > 0: - myStats.fracLoss = (myStats.pktsSent - myStats.pktsRcvd) / myStats.pktsSent + if current_stats.send_count > 0: + current_stats.lost_count = (current_stats.send_count - current_stats.receive_count) / current_stats.send_count print("%d packets transmitted, %d packets received, %0.1f%% packet loss" % ( - myStats.pktsSent, myStats.pktsRcvd, 100.0 * myStats.fracLoss + current_stats.send_count, current_stats.receive_count, 100.0 * current_stats.lost_count )) - if myStats.pktsRcvd > 0: - print("round-trip (ms) min/avg/max = %d/%0.1f/%d" % ( - myStats.minTime, myStats.totTime / myStats.pktsRcvd, myStats.maxTime + if current_stats.receive_count > 0: + print("round-trip (ms) min/avg/max = %0.3f/%0.3f/%0.3f" % ( + current_stats.min_time, current_stats.total_time / current_stats.receive_count, current_stats.max_time )) print("") @@ -252,40 +256,40 @@ def signal_handler(signum, frame): sys.exit(0) -def verbose_ping(hostname, timeout=1000, count=3, numDataBytes=55): +def verbose_ping(hostname, deadline=1000, count=3, packet_size=55): """ - Send >count< ping to >destIP< with the given >timeout< and display + Send >count< ping to >dest_ip< with the given >deadline< and display the result. """ - global myStats + global current_stats signal.signal(signal.SIGINT, signal_handler) # Handle Ctrl-C if hasattr(signal, "SIGBREAK"): # Handle Ctrl-Break e.g. under Windows signal.signal(signal.SIGBREAK, signal_handler) - myStats = MyStats() # Reset the stats + current_stats = PingStats() # Reset the stats - mySeqNumber = 0 # Starting value + seq_number = 0 # Starting value try: - destIP = socket.gethostbyname(hostname) - # FIXME: Use destIP only for display this line here? see: https://github.com/jedie/python-ping/issues/3 - print("\nPYTHON-PING %s (%s): %d data bytes" % (hostname, destIP, numDataBytes)) + dest_ip = socket.gethostbyname(hostname) + # FIXME: Use dest_ip only for display this line here? see: https://github.com/jedie/python-ping/issues/3 + print("\nPYTHON-PING %s (%s): %d data bytes" % (hostname, dest_ip, packet_size)) except socket.gaierror as e: print("\nPYTHON-PING: Unknown host: %s (%s)" % (hostname, e.args[1])) print("") return - myStats.thisIP = destIP + current_stats.dest_ip = dest_ip for i in range(count): - delay = do_one(destIP, timeout, mySeqNumber, numDataBytes) + delay = do_one(dest_ip, deadline, seq_number, packet_size) if delay == None: delay = 0 - mySeqNumber += 1 + seq_number += 1 # Pause for the remainder of the MAX_SLEEP period (if applicable) if (MAX_SLEEP > delay): @@ -310,7 +314,7 @@ def verbose_ping(hostname, timeout=1000, count=3, numDataBytes=55): # Should fail with 'getaddrinfo failed': verbose_ping("foobar_url.foobar") - # Should fail (timeout), but it depends on the local network: + # Should fail (deadline), but it depends on the local network: verbose_ping("192.168.255.254") # Should fails with 'The requested address is not valid in its context': From 55833fdee59c7fb4e9dc1ad29337941c3b7fcf86 Mon Sep 17 00:00:00 2001 From: JensDiemer Date: Wed, 12 Oct 2011 21:53:36 +0200 Subject: [PATCH 25/61] move into a class --- ping.py | 375 +++++++++++++++++++++++++++++--------------------------- 1 file changed, 194 insertions(+), 181 deletions(-) diff --git a/ping.py b/ping.py index 621ba60..db59aa8 100755 --- a/ping.py +++ b/ping.py @@ -47,7 +47,7 @@ class PingStats: current_stats = PingStats # Used globally FIXME: Don't use global -def checksum(source_string): +def calculate_checksum(source_string): """ A port of the functionality of in_cksum() from ping.c Ideally this would act on the string as a series of 16-bit ints (host @@ -87,215 +87,228 @@ def checksum(source_string): return answer -class PingBase(object): - def __init__(self, dest_ip): - self.dest_ip = dest_ip - - -def do_one(dest_ip, deadline, seq_number, packet_size): - """ - Returns either the delay (in ms) or None on deadline. - """ - global current_stats - - delay = None - - try: # One could use UDP here, but it's obscure - current_socket = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.getprotobyname("icmp")) - except socket.error, (errno, msg): - if errno == 1: - # Operation not permitted - Add more information to traceback - etype, evalue, etb = sys.exc_info() - evalue = etype( - "%s - Note that ICMP messages can only be send from processes running as root." % evalue - ) - raise etype, evalue, etb - - print("failed. (socket error: '%s')" % msg) - raise # raise the original error - own_id = os.getpid() & 0xFFFF - send_time = send_one_ping(current_socket, dest_ip, own_id, seq_number, packet_size) - if send_time == None: - current_socket.close() - return delay - - current_stats.send_count += 1; - - receive_time, dataSize, ip_src_ip, icmp_seq_number, ip_ttl = receive_one_ping(current_socket, own_id, deadline) - - current_socket.close() - - if receive_time: - delay = (receive_time - send_time) * 1000.0 +class Ping(object): + def __init__(self, dest_ip, timeout=1000, packet_size=55, own_id=None): + self.dest_ip = dest_ip + self.timeout = timeout + self.packet_size = packet_size + if own_id is None: + self.own_id = os.getpid() & 0xFFFF + else: + self.own_id = own_id + + self.seq_number = 0 + self.send_count = 0 + self.receive_count = 0 + self.min_time = 999999999 + self.max_time = 0.0 + self.total_time = 0.0 + + #-------------------------------------------------------------------------- + + def start(self): + try: + ip = socket.gethostbyname(self.dest_ip) + # FIXME: Use dest_ip only for display this line here? see: https://github.com/jedie/python-ping/issues/3 + print("\nPYTHON-PING %s (%s): %d data bytes" % (self.dest_ip, ip, self.packet_size)) + except socket.gaierror as e: + print("\nPYTHON-PING: Unknown host: %s (%s)" % (self.dest_ip, e.args[1])) + print("") + sys.exit(-1) + + def success(self, delay, from_info, packet_size, ip_src_ip, icmp_seq_number, ip_ttl): print("%d bytes from %s: icmp_seq=%d ttl=%d time=%.1f ms" % ( - dataSize, socket.inet_ntoa(struct.pack("!I", ip_src_ip)), icmp_seq_number, ip_ttl, delay) + packet_size, from_info, icmp_seq_number, ip_ttl, delay) ) - current_stats.receive_count += 1; - current_stats.total_time += delay - if current_stats.min_time > delay: - current_stats.min_time = delay - if current_stats.max_time < delay: - current_stats.max_time = delay - else: - delay = None - print("Request timed out.") - - return delay - - -def send_one_ping(current_socket, dest_ip, own_id, seq_number, packet_size): - """ - Send one ping to the given >dest_ip<. - """ - # Header is type (8), code (8), checksum (16), id (16), sequence (16) - myChecksum = 0 - - # Make a dummy heder with a 0 checksum. - header = struct.pack( - "!BBHHH", ICMP_ECHO, 0, myChecksum, own_id, seq_number - ) - - padBytes = [] - startVal = 0x42 - for i in range(startVal, startVal + (packet_size)): - padBytes += [(i & 0xff)] # Keep chars in the 0-255 range - data = bytes(padBytes) - - # Calculate the checksum on the data and the dummy header. - myChecksum = checksum(header + data) # Checksum is in network order - - # Now that we have the right checksum, we put that in. It's just easier - # to make up a new header than to stuff it into the dummy. - header = struct.pack( - "!BBHHH", ICMP_ECHO, 0, myChecksum, own_id, seq_number - ) - - packet = header + data - - sendTime = default_timer() - try: - current_socket.sendto(packet, (dest_ip, 1)) # Port number is irrelevant for ICMP - except socket.error as e: - print("General failure (%s)" % (e.args[1])) - return - - return sendTime + def failed(self): + print("Request timed out.") + def exit(self): + print("\n----%s PYTHON PING Statistics----" % (self.dest_ip)) -def receive_one_ping(current_socket, own_id, deadline): - """ - Receive the ping from the socket. deadline = in ms - """ - timeout = deadline / 1000 + if self.send_count > 0: + lost_rate = (self.send_count - self.receive_count) / self.send_count * 100.0 - while True: # Loop while waiting for packet or deadline - select_start = default_timer() - inputready, outputready, exceptready = select.select([current_socket], [], [], timeout) - select_duration = (default_timer() - select_start) - if inputready == []: # deadline - return None, 0, 0, 0, 0 + print("%d packets transmitted, %d packets received, %0.1f%% packet loss" % ( + self.send_count, self.receive_count, lost_rate + )) - receive_time = default_timer() + if self.receive_count > 0: + print("round-trip (ms) min/avg/max = %0.3f/%0.3f/%0.3f" % ( + self.min_time, self.total_time / self.receive_count, self.max_time + )) - packet_data, address = current_socket.recvfrom(ICMP_MAX_RECV) + print("") - ip_header = packet_data[:20] - ip_version, ip_type, ip_length, \ - ip_id, ip_flags, ip_ttl, ip_protocol, \ - ip_checksum, ip_src_ip, ip_dest_ip = struct.unpack( - "!BBHHHBBHII", ip_header - ) + #-------------------------------------------------------------------------- + + def signal_handler(self, signum, frame): + """ + Handle exit via signals + """ + self.exit() + print("\n(Terminated with signal %d)\n" % (signum)) + sys.exit(0) + + def setup_signal_handler(self): + signal.signal(signal.SIGINT, self.signal_handler) # Handle Ctrl-C + if hasattr(signal, "SIGBREAK"): + # Handle Ctrl-Break e.g. under Windows + signal.signal(signal.SIGBREAK, self.signal_handler) + + #-------------------------------------------------------------------------- + + def run(self, count=None, deadline=None): + """ + send and receive pings in a loop. Stop if count or until deadline. + """ + self.setup_signal_handler() + + while True: + delay = self.do() + + self.seq_number += 1 + if count and self.seq_number >= count: + break + if deadline and self.total_time >= deadline: + break + + if delay == None: + delay = 0 + + # Pause for the remainder of the MAX_SLEEP period (if applicable) + if (MAX_SLEEP > delay): + time.sleep((MAX_SLEEP - delay) / 1000.0) + + self.exit() + + def do(self): + """ + Send one ICMP ECHO_REQUEST and receive the response until self.timeout + """ + if self.seq_number == 0: + self.start() + + try: # One could use UDP here, but it's obscure + current_socket = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.getprotobyname("icmp")) + except socket.error, (errno, msg): + if errno == 1: + # Operation not permitted - Add more information to traceback + etype, evalue, etb = sys.exc_info() + evalue = etype( + "%s - Note that ICMP messages can only be send from processes running as root." % evalue + ) + raise etype, evalue, etb + raise # raise the original error + + send_time = self.send_one_ping(current_socket) + if send_time == None: + return + self.send_count += 1 + + receive_time, packet_size, ip_src_ip, icmp_seq_number, ip_ttl = self.receive_one_ping(current_socket) + current_socket.close() - icmp_header = packet_data[20:28] - icmp_type, icmp_code, icmp_checksum, \ - icmp_packet_id, icmp_seq_number = struct.unpack( - "!BBHHH", icmp_header + if receive_time: + self.receive_count += 1 + delay = (receive_time - send_time) * 1000.0 + self.total_time += delay + if self.min_time > delay: + self.min_time = delay + if self.max_time < delay: + self.max_time = delay + + from_info = socket.inet_ntoa(struct.pack("!I", ip_src_ip)) + self.success(delay, from_info, packet_size, ip_src_ip, icmp_seq_number, ip_ttl) + return delay + else: + self.failed() + + def send_one_ping(self, current_socket): + """ + Send one ICMP ECHO_REQUEST + """ + # Header is type (8), code (8), checksum (16), id (16), sequence (16) + checksum = 0 + + # Make a dummy header with a 0 checksum. + header = struct.pack( + "!BBHHH", ICMP_ECHO, 0, checksum, self.own_id, self.seq_number ) - if icmp_packet_id == own_id: # Our packet - dataSize = len(packet_data) - 28 - return receive_time, dataSize, ip_src_ip, icmp_seq_number, ip_ttl - - timeout = timeout - select_duration - if timeout <= 0: - return None, 0, 0, 0, 0 - - -def dump_stats(): - """ - Show stats when pings are done - """ - global current_stats - - print("\n----%s PYTHON PING Statistics----" % (current_stats.dest_ip)) + padBytes = [] + startVal = 0x42 + for i in range(startVal, startVal + (self.packet_size)): + padBytes += [(i & 0xff)] # Keep chars in the 0-255 range + data = bytes(padBytes) - if current_stats.send_count > 0: - current_stats.lost_count = (current_stats.send_count - current_stats.receive_count) / current_stats.send_count + # Calculate the checksum on the data and the dummy header. + checksum = calculate_checksum(header + data) # Checksum is in network order - print("%d packets transmitted, %d packets received, %0.1f%% packet loss" % ( - current_stats.send_count, current_stats.receive_count, 100.0 * current_stats.lost_count - )) + # Now that we have the right checksum, we put that in. It's just easier + # to make up a new header than to stuff it into the dummy. + header = struct.pack( + "!BBHHH", ICMP_ECHO, 0, checksum, self.own_id, self.seq_number + ) - if current_stats.receive_count > 0: - print("round-trip (ms) min/avg/max = %0.3f/%0.3f/%0.3f" % ( - current_stats.min_time, current_stats.total_time / current_stats.receive_count, current_stats.max_time - )) + packet = header + data - print("") + send_time = default_timer() + try: + current_socket.sendto(packet, (self.dest_ip, 1)) # Port number is irrelevant for ICMP + except socket.error as e: + print("General failure (%s)" % (e.args[1])) + current_socket.close() + return -def signal_handler(signum, frame): - """ - Handle exit via signals - """ - dump_stats() - print("\n(Terminated with signal %d)\n" % (signum)) - sys.exit(0) + return send_time + def receive_one_ping(self, current_socket): + """ + Receive the ping from the socket. timeout = in ms + """ + timeout = self.timeout / 1000.0 -def verbose_ping(hostname, deadline=1000, count=3, packet_size=55): - """ - Send >count< ping to >dest_ip< with the given >deadline< and display - the result. - """ - global current_stats - - signal.signal(signal.SIGINT, signal_handler) # Handle Ctrl-C - if hasattr(signal, "SIGBREAK"): - # Handle Ctrl-Break e.g. under Windows - signal.signal(signal.SIGBREAK, signal_handler) - - current_stats = PingStats() # Reset the stats + while True: # Loop while waiting for packet or timeout + select_start = default_timer() + inputready, outputready, exceptready = select.select([current_socket], [], [], timeout) + select_duration = (default_timer() - select_start) + if inputready == []: # timeout + return None, 0, 0, 0, 0 - seq_number = 0 # Starting value + receive_time = default_timer() - try: - dest_ip = socket.gethostbyname(hostname) - # FIXME: Use dest_ip only for display this line here? see: https://github.com/jedie/python-ping/issues/3 - print("\nPYTHON-PING %s (%s): %d data bytes" % (hostname, dest_ip, packet_size)) - except socket.gaierror as e: - print("\nPYTHON-PING: Unknown host: %s (%s)" % (hostname, e.args[1])) - print("") - return + packet_data, address = current_socket.recvfrom(ICMP_MAX_RECV) - current_stats.dest_ip = dest_ip + ip_header = packet_data[:20] + ip_version, ip_type, ip_length, \ + ip_id, ip_flags, ip_ttl, ip_protocol, \ + ip_checksum, ip_src_ip, ip_dest_ip = struct.unpack( + "!BBHHHBBHII", ip_header + ) - for i in range(count): - delay = do_one(dest_ip, deadline, seq_number, packet_size) + icmp_header = packet_data[20:28] + icmp_type, icmp_code, icmp_checksum, \ + icmp_packet_id, icmp_seq_number = struct.unpack( + "!BBHHH", icmp_header + ) - if delay == None: - delay = 0 + if icmp_packet_id == self.own_id: # Our packet + packet_size = len(packet_data) - 28 + return receive_time, packet_size, ip_src_ip, icmp_seq_number, ip_ttl - seq_number += 1 + timeout = timeout - select_duration + if timeout <= 0: + return None, 0, 0, 0, 0 - # Pause for the remainder of the MAX_SLEEP period (if applicable) - if (MAX_SLEEP > delay): - time.sleep((MAX_SLEEP - delay) / 1000) - dump_stats() +def verbose_ping(hostname, timeout=1000, count=3, packet_size=55): + p = Ping(hostname, timeout, packet_size) + p.run(count) if __name__ == '__main__': @@ -314,7 +327,7 @@ def verbose_ping(hostname, deadline=1000, count=3, packet_size=55): # Should fail with 'getaddrinfo failed': verbose_ping("foobar_url.foobar") - # Should fail (deadline), but it depends on the local network: + # Should fail (timeout), but it depends on the local network: verbose_ping("192.168.255.254") # Should fails with 'The requested address is not valid in its context': From 6e9cf379008cfd2b39c1e3fc58d946ae4565fc4a Mon Sep 17 00:00:00 2001 From: JensDiemer Date: Wed, 12 Oct 2011 22:18:22 +0200 Subject: [PATCH 26/61] * rename print callback methods * use hostname and ip for display information --- ping.py | 76 ++++++++++++++++++++++++++------------------------------- 1 file changed, 35 insertions(+), 41 deletions(-) diff --git a/ping.py b/ping.py index db59aa8..8ea011f 100755 --- a/ping.py +++ b/ping.py @@ -28,24 +28,12 @@ # ICMP parameters - ICMP_ECHOREPLY = 0 # Echo reply (per RFC792) ICMP_ECHO = 8 # Echo request (per RFC792) ICMP_MAX_RECV = 2048 # Max size of incoming buffer MAX_SLEEP = 1000 -class PingStats: - dest_ip = "0.0.0.0" - send_count = 0 - receive_count = 0 - min_time = 999999999 - max_time = 0 - total_time = 0 - lost_count = 1.0 - -current_stats = PingStats # Used globally FIXME: Don't use global - def calculate_checksum(source_string): """ @@ -88,10 +76,9 @@ def calculate_checksum(source_string): return answer - class Ping(object): - def __init__(self, dest_ip, timeout=1000, packet_size=55, own_id=None): - self.dest_ip = dest_ip + def __init__(self, destination, timeout=1000, packet_size=55, own_id=None): + self.destination = destination self.timeout = timeout self.packet_size = packet_size if own_id is None: @@ -99,6 +86,15 @@ def __init__(self, dest_ip, timeout=1000, packet_size=55, own_id=None): else: self.own_id = own_id + try: + # FIXME: Use destination only for display this line here? see: https://github.com/jedie/python-ping/issues/3 + self.dest_ip = socket.gethostbyname(self.destination) + except socket.gaierror as e: + self.print_unknown_host(e) + sys.exit(-1) + else: + self.print_start() + self.seq_number = 0 self.send_count = 0 self.receive_count = 0 @@ -108,26 +104,27 @@ def __init__(self, dest_ip, timeout=1000, packet_size=55, own_id=None): #-------------------------------------------------------------------------- - def start(self): - try: - ip = socket.gethostbyname(self.dest_ip) - # FIXME: Use dest_ip only for display this line here? see: https://github.com/jedie/python-ping/issues/3 - print("\nPYTHON-PING %s (%s): %d data bytes" % (self.dest_ip, ip, self.packet_size)) - except socket.gaierror as e: - print("\nPYTHON-PING: Unknown host: %s (%s)" % (self.dest_ip, e.args[1])) - print("") - sys.exit(-1) + def print_start(self): + print("\nPYTHON-PING %s (%s): %d data bytes" % (self.destination, self.dest_ip, self.packet_size)) + + def print_unknwon_host(self, e): + print("\nPYTHON-PING: Unknown host: %s (%s)\n" % (self.destination, e.args[1])) + + def print_success(self, delay, ip, packet_size, icmp_seq_number, ip_ttl): + if ip == self.destination: + from_info = ip + else: + from_info = "%s (%s)" % (self.destination, ip) - def success(self, delay, from_info, packet_size, ip_src_ip, icmp_seq_number, ip_ttl): print("%d bytes from %s: icmp_seq=%d ttl=%d time=%.1f ms" % ( packet_size, from_info, icmp_seq_number, ip_ttl, delay) ) - def failed(self): + def print_failed(self): print("Request timed out.") - def exit(self): - print("\n----%s PYTHON PING Statistics----" % (self.dest_ip)) + def print_exit(self): + print("\n----%s PYTHON PING Statistics----" % (self.destination)) if self.send_count > 0: lost_rate = (self.send_count - self.receive_count) / self.send_count * 100.0 @@ -147,9 +144,9 @@ def exit(self): def signal_handler(self, signum, frame): """ - Handle exit via signals + Handle print_exit via signals """ - self.exit() + self.print_exit() print("\n(Terminated with signal %d)\n" % (signum)) sys.exit(0) @@ -183,15 +180,12 @@ def run(self, count=None, deadline=None): if (MAX_SLEEP > delay): time.sleep((MAX_SLEEP - delay) / 1000.0) - self.exit() + self.print_exit() def do(self): """ Send one ICMP ECHO_REQUEST and receive the response until self.timeout """ - if self.seq_number == 0: - self.start() - try: # One could use UDP here, but it's obscure current_socket = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.getprotobyname("icmp")) except socket.error, (errno, msg): @@ -209,7 +203,7 @@ def do(self): return self.send_count += 1 - receive_time, packet_size, ip_src_ip, icmp_seq_number, ip_ttl = self.receive_one_ping(current_socket) + receive_time, packet_size, ip, icmp_seq_number, ip_ttl = self.receive_one_ping(current_socket) current_socket.close() if receive_time: @@ -221,11 +215,10 @@ def do(self): if self.max_time < delay: self.max_time = delay - from_info = socket.inet_ntoa(struct.pack("!I", ip_src_ip)) - self.success(delay, from_info, packet_size, ip_src_ip, icmp_seq_number, ip_ttl) + self.print_success(delay, ip, packet_size, icmp_seq_number, ip_ttl) return delay else: - self.failed() + self.print_failed() def send_one_ping(self, current_socket): """ @@ -259,7 +252,7 @@ def send_one_ping(self, current_socket): send_time = default_timer() try: - current_socket.sendto(packet, (self.dest_ip, 1)) # Port number is irrelevant for ICMP + current_socket.sendto(packet, (self.destination, 1)) # Port number is irrelevant for ICMP except socket.error as e: print("General failure (%s)" % (e.args[1])) current_socket.close() @@ -299,7 +292,8 @@ def receive_one_ping(self, current_socket): if icmp_packet_id == self.own_id: # Our packet packet_size = len(packet_data) - 28 - return receive_time, packet_size, ip_src_ip, icmp_seq_number, ip_ttl + ip = socket.inet_ntoa(struct.pack("!I", ip_src_ip)) + return receive_time, packet_size, ip, icmp_seq_number, ip_ttl timeout = timeout - select_duration if timeout <= 0: @@ -324,7 +318,7 @@ def verbose_ping(hostname, timeout=1000, count=3, packet_size=55): # to the local host, but 2.7 tries to resolve to the local *gateway*) verbose_ping("localhost") - # Should fail with 'getaddrinfo failed': + # Should fail with 'getaddrinfo print_failed': verbose_ping("foobar_url.foobar") # Should fail (timeout), but it depends on the local network: From f4e191f21b0fc5425f8fbcd17f939040029ee57e Mon Sep 17 00:00:00 2001 From: JensDiemer Date: Wed, 12 Oct 2011 22:49:53 +0200 Subject: [PATCH 27/61] * Bugfix: calculate packet lost count * Put received IP and ICMP header into a dict, so we can display different information in print_success() --- ping.py | 56 ++++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 36 insertions(+), 20 deletions(-) diff --git a/ping.py b/ping.py index 8ea011f..9e08417 100755 --- a/ping.py +++ b/ping.py @@ -76,6 +76,13 @@ def calculate_checksum(source_string): return answer +class HeaderInformation(dict): + """ Simple storage received IP and ICMP header informations """ + def __init__(self, names, struct_format, data): + unpacked_data = struct.unpack(struct_format, data) + dict.__init__(self, dict(zip(names, unpacked_data))) + + class Ping(object): def __init__(self, destination, timeout=1000, packet_size=55, own_id=None): self.destination = destination @@ -110,15 +117,17 @@ def print_start(self): def print_unknwon_host(self, e): print("\nPYTHON-PING: Unknown host: %s (%s)\n" % (self.destination, e.args[1])) - def print_success(self, delay, ip, packet_size, icmp_seq_number, ip_ttl): + def print_success(self, delay, ip, packet_size, ip_header, icmp_header): if ip == self.destination: from_info = ip else: from_info = "%s (%s)" % (self.destination, ip) print("%d bytes from %s: icmp_seq=%d ttl=%d time=%.1f ms" % ( - packet_size, from_info, icmp_seq_number, ip_ttl, delay) + packet_size, from_info, icmp_header["seq_number"], ip_header["ttl"], delay) ) + #print("IP header: %r" % ip_header) + #print("ICMP header: %r" % icmp_header) def print_failed(self): print("Request timed out.") @@ -126,8 +135,9 @@ def print_failed(self): def print_exit(self): print("\n----%s PYTHON PING Statistics----" % (self.destination)) - if self.send_count > 0: - lost_rate = (self.send_count - self.receive_count) / self.send_count * 100.0 + lost_count = self.send_count - self.receive_count + #print("%i packets lost" % lost_count) + lost_rate = float(lost_count) / self.send_count * 100.0 print("%d packets transmitted, %d packets received, %0.1f%% packet loss" % ( self.send_count, self.receive_count, lost_rate @@ -203,7 +213,7 @@ def do(self): return self.send_count += 1 - receive_time, packet_size, ip, icmp_seq_number, ip_ttl = self.receive_one_ping(current_socket) + receive_time, packet_size, ip, ip_header, icmp_header = self.receive_one_ping(current_socket) current_socket.close() if receive_time: @@ -215,7 +225,7 @@ def do(self): if self.max_time < delay: self.max_time = delay - self.print_success(delay, ip, packet_size, icmp_seq_number, ip_ttl) + self.print_success(delay, ip, packet_size, ip_header, icmp_header) return delay else: self.print_failed() @@ -277,23 +287,29 @@ def receive_one_ping(self, current_socket): packet_data, address = current_socket.recvfrom(ICMP_MAX_RECV) - ip_header = packet_data[:20] - ip_version, ip_type, ip_length, \ - ip_id, ip_flags, ip_ttl, ip_protocol, \ - ip_checksum, ip_src_ip, ip_dest_ip = struct.unpack( - "!BBHHHBBHII", ip_header + icmp_header = HeaderInformation( + names=[ + "type", "code", "checksum", + "packet_id", "seq_number" + ], + struct_format="!BBHHH", + data=packet_data[20:28] ) - icmp_header = packet_data[20:28] - icmp_type, icmp_code, icmp_checksum, \ - icmp_packet_id, icmp_seq_number = struct.unpack( - "!BBHHH", icmp_header - ) - - if icmp_packet_id == self.own_id: # Our packet + if icmp_header["packet_id"] == self.own_id: # Our packet + ip_header = HeaderInformation( + names=[ + "version", "type", "length", + "id", "flags", "ttl", "protocol", + "checksum", "src_ip", "dest_ip" + ], + struct_format="!BBHHHBBHII", + data=packet_data[:20] + ) packet_size = len(packet_data) - 28 - ip = socket.inet_ntoa(struct.pack("!I", ip_src_ip)) - return receive_time, packet_size, ip, icmp_seq_number, ip_ttl + ip = socket.inet_ntoa(struct.pack("!I", ip_header["src_ip"])) + # XXX: Why not ip = address[0] ??? + return receive_time, packet_size, ip, ip_header, icmp_header timeout = timeout - select_duration if timeout <= 0: From 1d8e6004c839a1797fa1984365b819a299b3d86e Mon Sep 17 00:00:00 2001 From: JensDiemer Date: Wed, 12 Oct 2011 22:51:18 +0200 Subject: [PATCH 28/61] Update example output --- README.creole | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/README.creole b/README.creole index 964e6a1..8bd93ce 100644 --- a/README.creole +++ b/README.creole @@ -14,14 +14,14 @@ Original Version from [[ftp://ftp.visi.com/users/mdc/ping.py|Matthew Dixon Cowle {{{ ~/python-ping$ sudo ./ping.py google.com -PYTHON-PING google.com (74.125.39.147): 55 data bytes -64 bytes from 74.125.39.147: icmp_seq=0 ttl=53 time=23 ms -64 bytes from 74.125.39.147: icmp_seq=1 ttl=52 time=20 ms -64 bytes from 74.125.39.147: icmp_seq=2 ttl=53 time=22 ms +PYTHON-PING google.com (209.85.148.99): 55 data bytes +64 bytes from google.com (209.85.148.99): icmp_seq=0 ttl=54 time=56.2 ms +64 bytes from google.com (209.85.148.99): icmp_seq=1 ttl=54 time=55.7 ms +64 bytes from google.com (209.85.148.99): icmp_seq=2 ttl=54 time=55.5 ms -----74.125.39.147 PYTHON PING Statistics---- +----google.com PYTHON PING Statistics---- 3 packets transmitted, 3 packets received, 0.0% packet loss -round-trip (ms) min/avg/max = 20/22.4/23 +round-trip (ms) min/avg/max = 55.468/55.795/56.232 }}} From 12050a533166444234f8dc841ef82e6415d68d79 Mon Sep 17 00:00:00 2001 From: JensDiemer Date: Thu, 13 Oct 2011 10:17:13 +0200 Subject: [PATCH 29/61] install \"ping.py\" as script --- MANIFEST.in | 1 - setup.py | 1 + 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/MANIFEST.in b/MANIFEST.in index 244d0be..2cf163c 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -1,4 +1,3 @@ include AUTHORS LICENSE MANIFEST.in README.creole -recursive-include *.py recursive-exclude * *.pyc recursive-exclude * *.pyo \ No newline at end of file diff --git a/setup.py b/setup.py index 8dba88e..5e117a6 100755 --- a/setup.py +++ b/setup.py @@ -108,6 +108,7 @@ def get_authors(): packages=find_packages(), include_package_data=True, # include package data under svn source control zip_safe=False, + scripts=["ping.py"], classifiers=[ # http://pypi.python.org/pypi?%3Aaction=list_classifiers # "Development Status :: 4 - Beta", From e8036e13cbc420cf830437adc9dc6085b7c9f0ab Mon Sep 17 00:00:00 2001 From: incidence Date: Sun, 16 Oct 2011 00:39:07 +0300 Subject: [PATCH 30/61] Fixed a typo in a method name threw a "Has no Attribute exception" --- ping.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ping.py b/ping.py index 9e08417..490458b 100755 --- a/ping.py +++ b/ping.py @@ -114,7 +114,7 @@ def __init__(self, destination, timeout=1000, packet_size=55, own_id=None): def print_start(self): print("\nPYTHON-PING %s (%s): %d data bytes" % (self.destination, self.dest_ip, self.packet_size)) - def print_unknwon_host(self, e): + def print_unknown_host(self, e): print("\nPYTHON-PING: Unknown host: %s (%s)\n" % (self.destination, e.args[1])) def print_success(self, delay, ip, packet_size, ip_header, icmp_header): From 9e1ca0f0fc7aafc612b2cc602dc6eb5bc4c91842 Mon Sep 17 00:00:00 2001 From: Jens Diemer Date: Mon, 17 Oct 2011 09:56:04 +0300 Subject: [PATCH 31/61] Update AUTHORS --- AUTHORS | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/AUTHORS b/AUTHORS index 1418452..8c3b4b3 100644 --- a/AUTHORS +++ b/AUTHORS @@ -1,13 +1,14 @@ - AUTHORS / CONTRIBUTORS (alphabetic order): * Cowles, Matthew Dixon -- ftp://ftp.visi.com/users/mdc/ping.py * Diemer, Jens -- http://www.jensdiemer.de * Falatic, Martin -- http://www.falatic.com * Hallman, Chris -- http://cdhallman.blogspot.com + * incidence -- https://github.com/incidence * Notaras, George -- http://www.g-loaded.eu * Poincheval, Jerome * Stauffer, Samuel * Zach Ware * zed -- https://github.com/zed + From 376a01930ed943a73b316208515d7351f6846c6d Mon Sep 17 00:00:00 2001 From: JensDiemer Date: Mon, 17 Oct 2011 09:06:31 +0200 Subject: [PATCH 32/61] Update history --- README.creole | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.creole b/README.creole index 8bd93ce..a605156 100644 --- a/README.creole +++ b/README.creole @@ -39,6 +39,9 @@ round-trip (ms) min/avg/max = 55.468/55.795/56.232 == Revision history == +==== Oct. 17, 2011 ==== +* [[https://github.com/jedie/python-ping/pull/6|Bugfix if host is unknown]] + ==== Oct. 12, 2011 ==== Merge sources and create a seperate github repository: * https://github.com/jedie/python-ping From a6015128a1dbaa1f18ce136aab890801f32bd4fa Mon Sep 17 00:00:00 2001 From: Jens Diemer Date: Mon, 17 Oct 2011 10:10:36 +0300 Subject: [PATCH 33/61] Update README.creole --- README.creole | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/README.creole b/README.creole index a605156..b86e961 100644 --- a/README.creole +++ b/README.creole @@ -104,3 +104,9 @@ 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(). + +== Links == + +| Sourcecode at GitHub | https://github.com/jedie/python-ping | +| Python Package Index | http://pypi.python.org/pypi/python-ping/ | +| IRC | [[http://www.pylucid.org/permalink/304/irc-channel|#pylucid on freenode.net]] From bca063c152eeaad34dc405e2118da005229000ef Mon Sep 17 00:00:00 2001 From: JensDiemer Date: Mon, 17 Oct 2011 09:14:09 +0200 Subject: [PATCH 34/61] fix indent --- setup.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/setup.py b/setup.py index 5e117a6..91730ea 100755 --- a/setup.py +++ b/setup.py @@ -84,11 +84,11 @@ def get_authors(): try: f = file(os.path.join(PACKAGE_ROOT, "AUTHORS"), "r") for line in f: - if not line.strip().startswith("*"): - continue - if "--" in line: - line = line.split("--", 1)[0] - authors.append(line.strip(" *\r\n")) + if not line.strip().startswith("*"): + continue + if "--" in line: + line = line.split("--", 1)[0] + authors.append(line.strip(" *\r\n")) f.close() authors.sort() except Exception, err: From bd9d558e735fc1d6f4aea1d0c041d87277ac1463 Mon Sep 17 00:00:00 2001 From: Kunal Sarkhel Date: Thu, 27 Oct 2011 19:20:32 -0300 Subject: [PATCH 35/61] Changed platform checking code to use the startswith idiom http://docs.python.org/library/sys.html#sys.platform recommends using startswith() instead of == --- ping.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ping.py b/ping.py index 490458b..4adfb42 100755 --- a/ping.py +++ b/ping.py @@ -19,7 +19,7 @@ import os, sys, socket, struct, select, time, signal -if sys.platform == "win32": +if sys.platform.startswith("win32"): # On Windows, the best timer is time.clock() default_timer = time.clock else: From 58d7cad47c97e914eb248158631cd4aa03e48363 Mon Sep 17 00:00:00 2001 From: Jens Diemer Date: Fri, 28 Oct 2011 10:10:28 +0300 Subject: [PATCH 36/61] add Sarkhel, Kunal -- https://github.com/techwizrd -- https://github.com/jedie/python-ping/pull/7 --- AUTHORS | 2 ++ 1 file changed, 2 insertions(+) diff --git a/AUTHORS b/AUTHORS index 8c3b4b3..34be436 100644 --- a/AUTHORS +++ b/AUTHORS @@ -7,8 +7,10 @@ AUTHORS / CONTRIBUTORS (alphabetic order): * incidence -- https://github.com/incidence * Notaras, George -- http://www.g-loaded.eu * Poincheval, Jerome + * Sarkhel, Kunal -- https://github.com/techwizrd * Stauffer, Samuel * Zach Ware * zed -- https://github.com/zed + From 05a434bac75ec4e64124e765c28cf9839583b68b Mon Sep 17 00:00:00 2001 From: JensDiemer Date: Tue, 15 Nov 2011 10:49:06 +0100 Subject: [PATCH 37/61] add setup.py keywords --- setup.py | 1 + 1 file changed, 1 insertion(+) diff --git a/setup.py b/setup.py index 91730ea..3736335 100755 --- a/setup.py +++ b/setup.py @@ -105,6 +105,7 @@ def get_authors(): maintainer="Jens Diemer", maintainer_email="python-ping@jensdiemer.de", url='https://github.com/jedie/python-ping/', + keywords="ping icmp network latency", packages=find_packages(), include_package_data=True, # include package data under svn source control zip_safe=False, From 3f01bf82aff00dce91a74c97d87ae46b573f7456 Mon Sep 17 00:00:00 2001 From: JensDiemer Date: Tue, 15 Nov 2011 10:50:17 +0100 Subject: [PATCH 38/61] refactor imports --- ping.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/ping.py b/ping.py index 4adfb42..65c74ff 100755 --- a/ping.py +++ b/ping.py @@ -16,7 +16,13 @@ """ -import os, sys, socket, struct, select, time, signal +import os +import select +import signal +import socket +import struct +import sys +import time if sys.platform.startswith("win32"): From 120d46d9ccb6624c963bf04ac8bbdadad3105c3a Mon Sep 17 00:00:00 2001 From: JensDiemer Date: Tue, 15 Nov 2011 10:51:20 +0100 Subject: [PATCH 39/61] change HeaderInformation from class to a function. (Thanks jcborras for the idea: https://github.com/jcborras/python-ping/commit/2cac6f3c8f0d59f1c771809296587aa0c9aff6e2 ) --- ping.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/ping.py b/ping.py index 65c74ff..234a6b2 100755 --- a/ping.py +++ b/ping.py @@ -82,13 +82,6 @@ def calculate_checksum(source_string): return answer -class HeaderInformation(dict): - """ Simple storage received IP and ICMP header informations """ - def __init__(self, names, struct_format, data): - unpacked_data = struct.unpack(struct_format, data) - dict.__init__(self, dict(zip(names, unpacked_data))) - - class Ping(object): def __init__(self, destination, timeout=1000, packet_size=55, own_id=None): self.destination = destination @@ -174,6 +167,13 @@ def setup_signal_handler(self): #-------------------------------------------------------------------------- + def header2dict(self, names, struct_format, data): + """ unpack the raw received IP and ICMP header informations to a dict """ + unpacked_data = struct.unpack(struct_format, data) + return dict(zip(names, unpacked_data)) + + #-------------------------------------------------------------------------- + def run(self, count=None, deadline=None): """ send and receive pings in a loop. Stop if count or until deadline. @@ -293,7 +293,7 @@ def receive_one_ping(self, current_socket): packet_data, address = current_socket.recvfrom(ICMP_MAX_RECV) - icmp_header = HeaderInformation( + icmp_header = self.header2dict( names=[ "type", "code", "checksum", "packet_id", "seq_number" @@ -303,7 +303,7 @@ def receive_one_ping(self, current_socket): ) if icmp_header["packet_id"] == self.own_id: # Our packet - ip_header = HeaderInformation( + ip_header = self.header2dict( names=[ "version", "type", "length", "id", "flags", "ttl", "protocol", From ba987c96f1c04f089faf44cd6bd5432775d5cf65 Mon Sep 17 00:00:00 2001 From: JensDiemer Date: Tue, 15 Nov 2011 11:59:34 +0100 Subject: [PATCH 40/61] add: jcborras -- https://github.com/jcborras --- AUTHORS | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/AUTHORS b/AUTHORS index 34be436..c0b9980 100644 --- a/AUTHORS +++ b/AUTHORS @@ -5,12 +5,10 @@ AUTHORS / CONTRIBUTORS (alphabetic order): * Falatic, Martin -- http://www.falatic.com * Hallman, Chris -- http://cdhallman.blogspot.com * incidence -- https://github.com/incidence + * jcborras -- https://github.com/jcborras * Notaras, George -- http://www.g-loaded.eu * Poincheval, Jerome * Sarkhel, Kunal -- https://github.com/techwizrd * Stauffer, Samuel * Zach Ware * zed -- https://github.com/zed - - - From 32bed5da87c2098a34e5f30ce4935149f2797178 Mon Sep 17 00:00:00 2001 From: JensDiemer Date: Tue, 15 Nov 2011 12:01:01 +0100 Subject: [PATCH 41/61] add the idea from https://github.com/jedie/python-ping/pull/8/files#L1R81 : Use socket.gethostbyname() only, if given address is not a valid IPv4 Address --- ping.py | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/ping.py b/ping.py index 234a6b2..89cff36 100755 --- a/ping.py +++ b/ping.py @@ -82,6 +82,25 @@ def calculate_checksum(source_string): return answer +def is_valid_ip4_address(addr): + parts = addr.split(".") + if not len(parts) == 4: + return False + for part in parts: + try: + number = int(part) + except ValueError: + return False + if number > 255: + return False + return True + +def to_ip(addr): + if is_valid_ip4_address(addr): + return addr + return socket.gethostbyname(addr) + + class Ping(object): def __init__(self, destination, timeout=1000, packet_size=55, own_id=None): self.destination = destination @@ -94,10 +113,9 @@ def __init__(self, destination, timeout=1000, packet_size=55, own_id=None): try: # FIXME: Use destination only for display this line here? see: https://github.com/jedie/python-ping/issues/3 - self.dest_ip = socket.gethostbyname(self.destination) + self.dest_ip = to_ip(self.destination) except socket.gaierror as e: self.print_unknown_host(e) - sys.exit(-1) else: self.print_start() @@ -115,6 +133,7 @@ def print_start(self): def print_unknown_host(self, e): print("\nPYTHON-PING: Unknown host: %s (%s)\n" % (self.destination, e.args[1])) + sys.exit(-1) def print_success(self, delay, ip, packet_size, ip_header, icmp_header): if ip == self.destination: From 24a6a03418762e51613446ba313bfeeeef4c67d0 Mon Sep 17 00:00:00 2001 From: JensDiemer Date: Tue, 15 Nov 2011 12:03:21 +0100 Subject: [PATCH 42/61] Add unittests with some ideas from https://github.com/jedie/python-ping/pull/8/ --- setup.py | 1 + tests.py | 130 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 131 insertions(+) create mode 100644 tests.py diff --git a/setup.py b/setup.py index 3736335..a5ecc8c 100755 --- a/setup.py +++ b/setup.py @@ -125,4 +125,5 @@ def get_authors(): "Topic :: Software Development :: Libraries :: Python Modules", "Topic :: System :: Networking :: Monitoring", ], + test_suite="tests", ) diff --git a/tests.py b/tests.py new file mode 100644 index 0000000..74d6431 --- /dev/null +++ b/tests.py @@ -0,0 +1,130 @@ +#!/usr/bin/env python +# coding: utf-8 + +""" + python-ping unittests + ~~~~~~~~~~~~~~~~~~~~~ + + Note that ICMP messages can only be send from processes running as root. + So you must run this tests also as root, e.g.: + + .../python-ping$ sudo python tests.py + + :homepage: https://github.com/jedie/python-ping/ + :copyleft: 1989-2011 by the python-ping team, see AUTHORS for more details. + :license: GNU GPL v2, see LICENSE for more details. +""" + +import socket +import unittest + +from ping import Ping, is_valid_ip4_address, to_ip + + +class PingTest(Ping): + """ + Used in TestPythonPing for check if print methods are called. + This is also a way how to subclass Ping ;) + """ + def __init__(self, *args, **kwargs): + self.start_call_count = 0 + self.unknown_host_call_count = 0 + self.success_call_count = 0 + self.failed_call_count = 0 + self.exit_call_count = 0 + super(PingTest, self).__init__(*args, **kwargs) + + def print_start(self): + self.start_call_count += 1 + + def print_unknown_host(self, e): + self.unknown_host_call_count += 1 + + def print_success(self, delay, ip, packet_size, ip_header, icmp_header): + self.success_call_count += 1 + + def print_failed(self): + self.failed_call_count += 1 + + def print_exit(self): + self.exit_call_count += 1 + + +class TestPythonPing(unittest.TestCase): + def testIp4AddrPositives(self): + self.assertTrue(is_valid_ip4_address('0.0.0.0')) + self.assertTrue(is_valid_ip4_address('1.2.3.4')) + self.assertTrue(is_valid_ip4_address('12.34.56.78')) + self.assertTrue(is_valid_ip4_address('255.255.255.255')) + + def testIp4AddrNegatives(self): + self.assertFalse(is_valid_ip4_address('0.0.0.0.0')) + self.assertFalse(is_valid_ip4_address('1.2.3')) + self.assertFalse(is_valid_ip4_address('a2.34.56.78')) + self.assertFalse(is_valid_ip4_address('255.255.255.256')) + + def testDestAddr1(self): + self.assertTrue(is_valid_ip4_address(to_ip('www.wikipedia.org'))) + self.assertRaises(socket.gaierror, to_ip, ('www.doesntexist.tld')) + + def testDestAddr2(self): + self.assertTrue(to_ip('10.10.10.1')) + self.assertTrue(to_ip('10.10.010.01')) + self.assertTrue(to_ip('10.010.10.1')) + + def test_init_only(self): + p = PingTest("www.google.com") + self.assertEqual(p.start_call_count, 1) + self.assertEqual(p.unknown_host_call_count, 0) + self.assertEqual(p.success_call_count, 0) + self.assertEqual(p.failed_call_count, 0) + self.assertEqual(p.exit_call_count, 0) + + def test_do_one_ping(self): + p = PingTest("www.google.com") + p.do() + self.assertEqual(p.send_count, 1) + self.assertEqual(p.receive_count, 1) + + self.assertEqual(p.start_call_count, 1) + self.assertEqual(p.unknown_host_call_count, 0) + self.assertEqual(p.success_call_count, 1) + self.assertEqual(p.failed_call_count, 0) + self.assertEqual(p.exit_call_count, 0) + + def test_do_one_failed_ping(self): + p = PingTest("www.doesntexist.tld") + self.assertEqual(p.start_call_count, 0) + self.assertEqual(p.unknown_host_call_count, 1) + self.assertEqual(p.success_call_count, 0) + self.assertEqual(p.failed_call_count, 0) + self.assertEqual(p.exit_call_count, 0) + + def test_run_ping(self): + p = PingTest("www.google.com") + p.run(count=2) + self.assertEqual(p.send_count, 2) + self.assertEqual(p.receive_count, 2) + + self.assertEqual(p.start_call_count, 1) + self.assertEqual(p.unknown_host_call_count, 0) + self.assertEqual(p.success_call_count, 2) + self.assertEqual(p.failed_call_count, 0) + self.assertEqual(p.exit_call_count, 1) + + def test_run_failed_pings(self): + p = PingTest("www.google.com", timeout=0.01) + p.run(count=2) + self.assertEqual(p.send_count, 2) + self.assertEqual(p.receive_count, 0) + + self.assertEqual(p.start_call_count, 1) + self.assertEqual(p.unknown_host_call_count, 0) + self.assertEqual(p.success_call_count, 0) + self.assertEqual(p.failed_call_count, 2) + self.assertEqual(p.exit_call_count, 1) + + +if __name__ == '__main__': + unittest.main() + From cc400008e2048ab4d9ece845aece145ea422b1fe Mon Sep 17 00:00:00 2001 From: Ben Toews Date: Tue, 10 Jul 2012 02:08:56 -0500 Subject: [PATCH 43/61] first commit. cleaning out stuff. fixing setup.py --- .project | 17 --- AUTHORS | 1 + MANIFEST.in | 3 - README.creole | 112 --------------- ping.py | 373 -------------------------------------------------- setup.py | 99 ++------------ tests.py | 130 ------------------ 7 files changed, 9 insertions(+), 726 deletions(-) delete mode 100644 .project delete mode 100644 MANIFEST.in delete mode 100644 README.creole delete mode 100755 ping.py delete mode 100644 tests.py diff --git a/.project b/.project deleted file mode 100644 index 6695946..0000000 --- a/.project +++ /dev/null @@ -1,17 +0,0 @@ - - - python-ping - - - - - - org.python.pydev.PyDevBuilder - - - - - - org.python.pydev.pythonNature - - diff --git a/AUTHORS b/AUTHORS index c0b9980..3456c48 100644 --- a/AUTHORS +++ b/AUTHORS @@ -10,5 +10,6 @@ AUTHORS / CONTRIBUTORS (alphabetic order): * Poincheval, Jerome * Sarkhel, Kunal -- https://github.com/techwizrd * Stauffer, Samuel + * Toews, Ben --- http://btoe.ws * Zach Ware * zed -- https://github.com/zed diff --git a/MANIFEST.in b/MANIFEST.in deleted file mode 100644 index 2cf163c..0000000 --- a/MANIFEST.in +++ /dev/null @@ -1,3 +0,0 @@ -include AUTHORS LICENSE MANIFEST.in README.creole -recursive-exclude * *.pyc -recursive-exclude * *.pyo \ No newline at end of file diff --git a/README.creole b/README.creole deleted file mode 100644 index b86e961..0000000 --- a/README.creole +++ /dev/null @@ -1,112 +0,0 @@ -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 [[ftp://ftp.visi.com/users/mdc/ping.py|Matthew Dixon Cowles]] - -* copyleft 1989-2011 by the python-ping team, see [[https://github.com/jedie/python-ping/blob/master/AUTHORS|AUTHORS]] for more details. -* license: GNU GPL v2, see [[https://github.com/jedie/python-ping/blob/master/LICENSE|LICENSE]] for more details. - - -=== usage === - -{{{ -~/python-ping$ sudo ./ping.py google.com - -PYTHON-PING google.com (209.85.148.99): 55 data bytes -64 bytes from google.com (209.85.148.99): icmp_seq=0 ttl=54 time=56.2 ms -64 bytes from google.com (209.85.148.99): icmp_seq=1 ttl=54 time=55.7 ms -64 bytes from google.com (209.85.148.99): icmp_seq=2 ttl=54 time=55.5 ms - -----google.com PYTHON PING Statistics---- -3 packets transmitted, 3 packets received, 0.0% packet loss -round-trip (ms) min/avg/max = 55.468/55.795/56.232 -}}} - - -== TODOs == - -* refactor ping.py -* create a CLI interface -* add a "suprocess ping", with output parser - - -== contribute == - -[[http://help.github.com/fork-a-repo/|Fork this repo]] on [[https://github.com/jedie/python-ping/|GitHub]] and [[http://help.github.com/send-pull-requests/|send pull requests]]. Thank you. - - -== Revision history == - -==== Oct. 17, 2011 ==== -* [[https://github.com/jedie/python-ping/pull/6|Bugfix if host is unknown]] - -==== Oct. 12, 2011 ==== -Merge sources and create a seperate github repository: -* https://github.com/jedie/python-ping - -Add a simple CLI interface. - -==== September 12, 2011 ==== -Bugfixes + cleanup by Jens Diemer -Tested with Ubuntu + Windows 7 - -==== September 6, 2011 ==== -[[http://www.falatic.com/index.php/39/pinging-with-python|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. - -==== August 3, 2011 ==== -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. - -==== March 11, 2010 ==== -changes by Samuel Stauffer: -replaced time.clock with default_timer which is set to -time.clock on windows and time.time on other systems. - -==== November 8, 2009 ==== -Fixes by [[http://www.g-loaded.eu/2009/10/30/python-ping/|George Notaras]], -reported by [[http://cdhallman.blogspot.com|Chris Hallman]]: - -Improved compatibility with GNU/Linux systems. - -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 [[http://docs.python.org/library/time.html#time.clock|the two OSes]]. - -==== May 30, 2007 ==== -little [[http://www.python-forum.de/post-69122.html#69122|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" - -==== December 4, 2000 ==== -Changed the struct.pack() calls to pack the checksum and ID as -unsigned. My thanks to Jerome Poincheval for the fix. - -==== 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(). - -== Links == - -| Sourcecode at GitHub | https://github.com/jedie/python-ping | -| Python Package Index | http://pypi.python.org/pypi/python-ping/ | -| IRC | [[http://www.pylucid.org/permalink/304/irc-channel|#pylucid on freenode.net]] diff --git a/ping.py b/ping.py deleted file mode 100755 index 89cff36..0000000 --- a/ping.py +++ /dev/null @@ -1,373 +0,0 @@ -#!/usr/bin/env python -# coding: utf-8 - -""" - A pure python ping implementation using raw sockets. - - Note that ICMP messages can only be send from processes running as root - (in Windows, you must run this script as 'Administrator'). - - Bugs are naturally mine. I'd be glad to hear about them. There are - certainly word - size dependencies here. - - :homepage: https://github.com/jedie/python-ping/ - :copyleft: 1989-2011 by the python-ping team, see AUTHORS for more details. - :license: GNU GPL v2, see LICENSE for more details. -""" - - -import os -import select -import signal -import socket -import struct -import sys -import time - - -if sys.platform.startswith("win32"): - # On Windows, the best timer is time.clock() - default_timer = time.clock -else: - # On most other platforms the best timer is time.time() - default_timer = time.time - - -# ICMP parameters -ICMP_ECHOREPLY = 0 # Echo reply (per RFC792) -ICMP_ECHO = 8 # Echo request (per RFC792) -ICMP_MAX_RECV = 2048 # Max size of incoming buffer - -MAX_SLEEP = 1000 - - -def calculate_checksum(source_string): - """ - A port of the functionality of in_cksum() from ping.c - Ideally this would act on the string as a series of 16-bit ints (host - packed), but this works. - Network data is big-endian, hosts are typically little-endian - """ - countTo = (int(len(source_string) / 2)) * 2 - sum = 0 - count = 0 - - # Handle bytes in pairs (decoding as short ints) - loByte = 0 - hiByte = 0 - while count < countTo: - if (sys.byteorder == "little"): - loByte = source_string[count] - hiByte = source_string[count + 1] - else: - loByte = source_string[count + 1] - hiByte = source_string[count] - sum = sum + (ord(hiByte) * 256 + ord(loByte)) - count += 2 - - # Handle last byte if applicable (odd-number of bytes) - # Endianness should be irrelevant in this case - if countTo < len(source_string): # Check for odd length - loByte = source_string[len(source_string) - 1] - sum += ord(loByte) - - sum &= 0xffffffff # Truncate sum to 32 bits (a variance from ping.c, which - # uses signed ints, but overflow is unlikely in ping) - - sum = (sum >> 16) + (sum & 0xffff) # Add high 16 bits to low 16 bits - sum += (sum >> 16) # Add carry from above (if any) - answer = ~sum & 0xffff # Invert and truncate to 16 bits - answer = socket.htons(answer) - - return answer - - -def is_valid_ip4_address(addr): - parts = addr.split(".") - if not len(parts) == 4: - return False - for part in parts: - try: - number = int(part) - except ValueError: - return False - if number > 255: - return False - return True - -def to_ip(addr): - if is_valid_ip4_address(addr): - return addr - return socket.gethostbyname(addr) - - -class Ping(object): - def __init__(self, destination, timeout=1000, packet_size=55, own_id=None): - self.destination = destination - self.timeout = timeout - self.packet_size = packet_size - if own_id is None: - self.own_id = os.getpid() & 0xFFFF - else: - self.own_id = own_id - - try: - # FIXME: Use destination only for display this line here? see: https://github.com/jedie/python-ping/issues/3 - self.dest_ip = to_ip(self.destination) - except socket.gaierror as e: - self.print_unknown_host(e) - else: - self.print_start() - - self.seq_number = 0 - self.send_count = 0 - self.receive_count = 0 - self.min_time = 999999999 - self.max_time = 0.0 - self.total_time = 0.0 - - #-------------------------------------------------------------------------- - - def print_start(self): - print("\nPYTHON-PING %s (%s): %d data bytes" % (self.destination, self.dest_ip, self.packet_size)) - - def print_unknown_host(self, e): - print("\nPYTHON-PING: Unknown host: %s (%s)\n" % (self.destination, e.args[1])) - sys.exit(-1) - - def print_success(self, delay, ip, packet_size, ip_header, icmp_header): - if ip == self.destination: - from_info = ip - else: - from_info = "%s (%s)" % (self.destination, ip) - - print("%d bytes from %s: icmp_seq=%d ttl=%d time=%.1f ms" % ( - packet_size, from_info, icmp_header["seq_number"], ip_header["ttl"], delay) - ) - #print("IP header: %r" % ip_header) - #print("ICMP header: %r" % icmp_header) - - def print_failed(self): - print("Request timed out.") - - def print_exit(self): - print("\n----%s PYTHON PING Statistics----" % (self.destination)) - - lost_count = self.send_count - self.receive_count - #print("%i packets lost" % lost_count) - lost_rate = float(lost_count) / self.send_count * 100.0 - - print("%d packets transmitted, %d packets received, %0.1f%% packet loss" % ( - self.send_count, self.receive_count, lost_rate - )) - - if self.receive_count > 0: - print("round-trip (ms) min/avg/max = %0.3f/%0.3f/%0.3f" % ( - self.min_time, self.total_time / self.receive_count, self.max_time - )) - - print("") - - #-------------------------------------------------------------------------- - - def signal_handler(self, signum, frame): - """ - Handle print_exit via signals - """ - self.print_exit() - print("\n(Terminated with signal %d)\n" % (signum)) - sys.exit(0) - - def setup_signal_handler(self): - signal.signal(signal.SIGINT, self.signal_handler) # Handle Ctrl-C - if hasattr(signal, "SIGBREAK"): - # Handle Ctrl-Break e.g. under Windows - signal.signal(signal.SIGBREAK, self.signal_handler) - - #-------------------------------------------------------------------------- - - def header2dict(self, names, struct_format, data): - """ unpack the raw received IP and ICMP header informations to a dict """ - unpacked_data = struct.unpack(struct_format, data) - return dict(zip(names, unpacked_data)) - - #-------------------------------------------------------------------------- - - def run(self, count=None, deadline=None): - """ - send and receive pings in a loop. Stop if count or until deadline. - """ - self.setup_signal_handler() - - while True: - delay = self.do() - - self.seq_number += 1 - if count and self.seq_number >= count: - break - if deadline and self.total_time >= deadline: - break - - if delay == None: - delay = 0 - - # Pause for the remainder of the MAX_SLEEP period (if applicable) - if (MAX_SLEEP > delay): - time.sleep((MAX_SLEEP - delay) / 1000.0) - - self.print_exit() - - def do(self): - """ - Send one ICMP ECHO_REQUEST and receive the response until self.timeout - """ - try: # One could use UDP here, but it's obscure - current_socket = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.getprotobyname("icmp")) - except socket.error, (errno, msg): - if errno == 1: - # Operation not permitted - Add more information to traceback - etype, evalue, etb = sys.exc_info() - evalue = etype( - "%s - Note that ICMP messages can only be send from processes running as root." % evalue - ) - raise etype, evalue, etb - raise # raise the original error - - send_time = self.send_one_ping(current_socket) - if send_time == None: - return - self.send_count += 1 - - receive_time, packet_size, ip, ip_header, icmp_header = self.receive_one_ping(current_socket) - current_socket.close() - - if receive_time: - self.receive_count += 1 - delay = (receive_time - send_time) * 1000.0 - self.total_time += delay - if self.min_time > delay: - self.min_time = delay - if self.max_time < delay: - self.max_time = delay - - self.print_success(delay, ip, packet_size, ip_header, icmp_header) - return delay - else: - self.print_failed() - - def send_one_ping(self, current_socket): - """ - Send one ICMP ECHO_REQUEST - """ - # Header is type (8), code (8), checksum (16), id (16), sequence (16) - checksum = 0 - - # Make a dummy header with a 0 checksum. - header = struct.pack( - "!BBHHH", ICMP_ECHO, 0, checksum, self.own_id, self.seq_number - ) - - padBytes = [] - startVal = 0x42 - for i in range(startVal, startVal + (self.packet_size)): - padBytes += [(i & 0xff)] # Keep chars in the 0-255 range - data = bytes(padBytes) - - # Calculate the checksum on the data and the dummy header. - checksum = calculate_checksum(header + data) # Checksum is in network order - - # Now that we have the right checksum, we put that in. It's just easier - # to make up a new header than to stuff it into the dummy. - header = struct.pack( - "!BBHHH", ICMP_ECHO, 0, checksum, self.own_id, self.seq_number - ) - - packet = header + data - - send_time = default_timer() - - try: - current_socket.sendto(packet, (self.destination, 1)) # Port number is irrelevant for ICMP - except socket.error as e: - print("General failure (%s)" % (e.args[1])) - current_socket.close() - return - - return send_time - - def receive_one_ping(self, current_socket): - """ - Receive the ping from the socket. timeout = in ms - """ - timeout = self.timeout / 1000.0 - - while True: # Loop while waiting for packet or timeout - select_start = default_timer() - inputready, outputready, exceptready = select.select([current_socket], [], [], timeout) - select_duration = (default_timer() - select_start) - if inputready == []: # timeout - return None, 0, 0, 0, 0 - - receive_time = default_timer() - - packet_data, address = current_socket.recvfrom(ICMP_MAX_RECV) - - icmp_header = self.header2dict( - names=[ - "type", "code", "checksum", - "packet_id", "seq_number" - ], - struct_format="!BBHHH", - data=packet_data[20:28] - ) - - if icmp_header["packet_id"] == self.own_id: # Our packet - ip_header = self.header2dict( - names=[ - "version", "type", "length", - "id", "flags", "ttl", "protocol", - "checksum", "src_ip", "dest_ip" - ], - struct_format="!BBHHHBBHII", - data=packet_data[:20] - ) - packet_size = len(packet_data) - 28 - ip = socket.inet_ntoa(struct.pack("!I", ip_header["src_ip"])) - # XXX: Why not ip = address[0] ??? - return receive_time, packet_size, ip, ip_header, icmp_header - - timeout = timeout - select_duration - if timeout <= 0: - return None, 0, 0, 0, 0 - - -def verbose_ping(hostname, timeout=1000, count=3, packet_size=55): - p = Ping(hostname, timeout, packet_size) - p.run(count) - - -if __name__ == '__main__': - # FIXME: Add a real CLI - if len(sys.argv) == 1: - print "DEMO" - - # These should work: - verbose_ping("heise.de") - verbose_ping("google.com") - - # Inconsistent on Windows w/ ActivePython (Python 3.2 resolves correctly - # to the local host, but 2.7 tries to resolve to the local *gateway*) - verbose_ping("localhost") - - # Should fail with 'getaddrinfo print_failed': - verbose_ping("foobar_url.foobar") - - # Should fail (timeout), but it depends on the local network: - verbose_ping("192.168.255.254") - - # Should fails with 'The requested address is not valid in its context': - verbose_ping("0.0.0.0") - elif len(sys.argv) == 2: - verbose_ping(sys.argv[1]) - else: - print "Error: call ./ping.py domain.tld" diff --git a/setup.py b/setup.py index a5ecc8c..aabcf26 100755 --- a/setup.py +++ b/setup.py @@ -5,80 +5,15 @@ distutils setup ~~~~~~~~~~~~~~~ - :homepage: https://github.com/jedie/python-ping/ + :homepage: https://github.com/mastahyeti/gping/ :copyleft: 1989-2011 by the python-ping team, see AUTHORS for more details. :license: GNU GPL v2, see LICENSE for more details. """ import os -import subprocess -import sys -import time -import warnings from setuptools import setup, find_packages, Command -PACKAGE_ROOT = os.path.dirname(os.path.abspath(__file__)) - - -#VERBOSE = True -VERBOSE = False - -def _error(msg): - if VERBOSE: - warnings.warn(msg) - return "" - -def get_version_from_git(): - try: - process = subprocess.Popen( - # %ct: committer date, UNIX timestamp - ["/usr/bin/git", "log", "--pretty=format:%ct-%h", "-1", "HEAD"], - shell=False, cwd=PACKAGE_ROOT, - stdout=subprocess.PIPE, stderr=subprocess.PIPE, - ) - except Exception, err: - return _error("Can't get git hash: %s" % err) - - process.wait() - returncode = process.returncode - if returncode != 0: - return _error( - "Can't get git hash, returncode was: %r" - " - git stdout: %r" - " - git stderr: %r" - % (returncode, process.stdout.readline(), process.stderr.readline()) - ) - - output = process.stdout.readline().strip() - try: - raw_timestamp, hash = output.split("-", 1) - timestamp = int(raw_timestamp) - except Exception, err: - return _error("Error in git log output! Output was: %r" % output) - - try: - timestamp_formatted = time.strftime("%Y.%m.%d", time.gmtime(timestamp)) - except Exception, err: - return _error("can't convert %r to time string: %s" % (timestamp, err)) - - return "%s.%s" % (timestamp_formatted, hash) - - -# convert creole to ReSt on-the-fly, see also: -# https://code.google.com/p/python-creole/wiki/UseInSetup -try: - from creole.setup_utils import get_long_description -except ImportError: - if "register" in sys.argv or "sdist" in sys.argv or "--long-description" in sys.argv: - etype, evalue, etb = sys.exc_info() - evalue = etype("%s - Please install python-creole >= v0.8 - e.g.: pip install python-creole" % evalue) - raise etype, evalue, etb - long_description = None -else: - long_description = get_long_description(PACKAGE_ROOT) - - def get_authors(): authors = [] try: @@ -97,33 +32,15 @@ def get_authors(): setup( - name='python-ping', - version=get_version_from_git(), - description='A pure python ICMP ping implementation using raw sockets.', - long_description=long_description, + name='gping', + version="0.1", + description='A gevent fork of python-ping.', author=get_authors(), - maintainer="Jens Diemer", - maintainer_email="python-ping@jensdiemer.de", - url='https://github.com/jedie/python-ping/', - keywords="ping icmp network latency", + maintainer="Ben Toews", + maintainer_email="mastahyeti@gmail.com", + url='https://github.com/mastahyeti/gping', + keywords="ping icmp network latency gevent", packages=find_packages(), include_package_data=True, # include package data under svn source control zip_safe=False, - scripts=["ping.py"], - classifiers=[ - # http://pypi.python.org/pypi?%3Aaction=list_classifiers -# "Development Status :: 4 - Beta", - "Development Status :: 5 - Production/Stable", - "Environment :: Web Environment", - "Intended Audience :: Developers", - "Intended Audience :: Education", - "Intended Audience :: End Users/Desktop", - "License :: OSI Approved :: GNU General Public License (GPL)", - "Operating System :: OS Independent", - "Programming Language :: Python", - "Topic :: Internet", - "Topic :: Software Development :: Libraries :: Python Modules", - "Topic :: System :: Networking :: Monitoring", - ], - test_suite="tests", ) diff --git a/tests.py b/tests.py deleted file mode 100644 index 74d6431..0000000 --- a/tests.py +++ /dev/null @@ -1,130 +0,0 @@ -#!/usr/bin/env python -# coding: utf-8 - -""" - python-ping unittests - ~~~~~~~~~~~~~~~~~~~~~ - - Note that ICMP messages can only be send from processes running as root. - So you must run this tests also as root, e.g.: - - .../python-ping$ sudo python tests.py - - :homepage: https://github.com/jedie/python-ping/ - :copyleft: 1989-2011 by the python-ping team, see AUTHORS for more details. - :license: GNU GPL v2, see LICENSE for more details. -""" - -import socket -import unittest - -from ping import Ping, is_valid_ip4_address, to_ip - - -class PingTest(Ping): - """ - Used in TestPythonPing for check if print methods are called. - This is also a way how to subclass Ping ;) - """ - def __init__(self, *args, **kwargs): - self.start_call_count = 0 - self.unknown_host_call_count = 0 - self.success_call_count = 0 - self.failed_call_count = 0 - self.exit_call_count = 0 - super(PingTest, self).__init__(*args, **kwargs) - - def print_start(self): - self.start_call_count += 1 - - def print_unknown_host(self, e): - self.unknown_host_call_count += 1 - - def print_success(self, delay, ip, packet_size, ip_header, icmp_header): - self.success_call_count += 1 - - def print_failed(self): - self.failed_call_count += 1 - - def print_exit(self): - self.exit_call_count += 1 - - -class TestPythonPing(unittest.TestCase): - def testIp4AddrPositives(self): - self.assertTrue(is_valid_ip4_address('0.0.0.0')) - self.assertTrue(is_valid_ip4_address('1.2.3.4')) - self.assertTrue(is_valid_ip4_address('12.34.56.78')) - self.assertTrue(is_valid_ip4_address('255.255.255.255')) - - def testIp4AddrNegatives(self): - self.assertFalse(is_valid_ip4_address('0.0.0.0.0')) - self.assertFalse(is_valid_ip4_address('1.2.3')) - self.assertFalse(is_valid_ip4_address('a2.34.56.78')) - self.assertFalse(is_valid_ip4_address('255.255.255.256')) - - def testDestAddr1(self): - self.assertTrue(is_valid_ip4_address(to_ip('www.wikipedia.org'))) - self.assertRaises(socket.gaierror, to_ip, ('www.doesntexist.tld')) - - def testDestAddr2(self): - self.assertTrue(to_ip('10.10.10.1')) - self.assertTrue(to_ip('10.10.010.01')) - self.assertTrue(to_ip('10.010.10.1')) - - def test_init_only(self): - p = PingTest("www.google.com") - self.assertEqual(p.start_call_count, 1) - self.assertEqual(p.unknown_host_call_count, 0) - self.assertEqual(p.success_call_count, 0) - self.assertEqual(p.failed_call_count, 0) - self.assertEqual(p.exit_call_count, 0) - - def test_do_one_ping(self): - p = PingTest("www.google.com") - p.do() - self.assertEqual(p.send_count, 1) - self.assertEqual(p.receive_count, 1) - - self.assertEqual(p.start_call_count, 1) - self.assertEqual(p.unknown_host_call_count, 0) - self.assertEqual(p.success_call_count, 1) - self.assertEqual(p.failed_call_count, 0) - self.assertEqual(p.exit_call_count, 0) - - def test_do_one_failed_ping(self): - p = PingTest("www.doesntexist.tld") - self.assertEqual(p.start_call_count, 0) - self.assertEqual(p.unknown_host_call_count, 1) - self.assertEqual(p.success_call_count, 0) - self.assertEqual(p.failed_call_count, 0) - self.assertEqual(p.exit_call_count, 0) - - def test_run_ping(self): - p = PingTest("www.google.com") - p.run(count=2) - self.assertEqual(p.send_count, 2) - self.assertEqual(p.receive_count, 2) - - self.assertEqual(p.start_call_count, 1) - self.assertEqual(p.unknown_host_call_count, 0) - self.assertEqual(p.success_call_count, 2) - self.assertEqual(p.failed_call_count, 0) - self.assertEqual(p.exit_call_count, 1) - - def test_run_failed_pings(self): - p = PingTest("www.google.com", timeout=0.01) - p.run(count=2) - self.assertEqual(p.send_count, 2) - self.assertEqual(p.receive_count, 0) - - self.assertEqual(p.start_call_count, 1) - self.assertEqual(p.unknown_host_call_count, 0) - self.assertEqual(p.success_call_count, 0) - self.assertEqual(p.failed_call_count, 2) - self.assertEqual(p.exit_call_count, 1) - - -if __name__ == '__main__': - unittest.main() - From 743b4e2a1ec44d9fc02e5ad24394f08f9eb60697 Mon Sep 17 00:00:00 2001 From: Ben Toews Date: Tue, 10 Jul 2012 02:09:51 -0500 Subject: [PATCH 44/61] adding readme --- README.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..dcf03c8 --- /dev/null +++ b/README.md @@ -0,0 +1,18 @@ +# GPing # + +This is a fork of the python-ping project that strips out most everything and replaces it with gevent. The point is to have an event driven ping utility for doing many concurrent pings... + +## Example Usage ## + + gp = GPing() + gp.send("127.0.0.1",test_callback) + gp.join() + +## License and Credits ## + +The python-ping project was started by Matthew Dixon Cowles. + + - copyleft 1989-2011 by the python-ping team, see AUTHORS for more details. + - license: GNU GPL v2, see LICENSE for more details. + +I have left the license and authors information intact, but this project seems to have a long history or forks and such so I am probably somehow pissing someone off or violating some license. Let me know if this is the case and I will be better. \ No newline at end of file From 517440bf55b91f99bfff38b357a388ca7f210b2b Mon Sep 17 00:00:00 2001 From: Ben Toews Date: Thu, 12 Jul 2012 04:20:05 -0500 Subject: [PATCH 45/61] changed __main__ action. fixed id overflow issue --- setup.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/setup.py b/setup.py index aabcf26..7fce409 100755 --- a/setup.py +++ b/setup.py @@ -40,7 +40,5 @@ def get_authors(): maintainer_email="mastahyeti@gmail.com", url='https://github.com/mastahyeti/gping', keywords="ping icmp network latency gevent", - packages=find_packages(), - include_package_data=True, # include package data under svn source control - zip_safe=False, + py_modules=['gping'] ) From 3583670b915b02ab79307543bd0cabeb370db240 Mon Sep 17 00:00:00 2001 From: Ben Toews Date: Tue, 17 Jul 2012 15:58:50 -0500 Subject: [PATCH 46/61] forgot to add the main file :D --- gping.py | 226 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 226 insertions(+) create mode 100644 gping.py diff --git a/gping.py b/gping.py new file mode 100644 index 0000000..54c6221 --- /dev/null +++ b/gping.py @@ -0,0 +1,226 @@ +""" + This part is a fork of the python-ping project that makes + things work with gevent. +""" + +import os +import struct +import sys +import time + +import gevent +from gevent import socket +from gevent.pool import Pool +from gevent.event import Event + +# From /usr/include/linux/icmp.h; your milage may vary. +ICMP_ECHO_REQUEST = 8 # Seems to be the same on Solaris. + + +def 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 + """ + sum = 0 + count_to = (len(source_string) / 2) * 2 + for count in xrange(0, count_to, 2): + this = ord(source_string[count + 1]) * 256 + ord(source_string[count]) + sum = sum + this + sum = sum & 0xffffffff # Necessary? + + if count_to < len(source_string): + sum = sum + ord(source_string[len(source_string) - 1]) + sum = sum & 0xffffffff # Necessary? + + sum = (sum >> 16) + (sum & 0xffff) + sum = sum + (sum >> 16) + answer = ~sum + answer = answer & 0xffff + + # Swap bytes. Bugger me if I know why. + answer = answer >> 8 | (answer << 8 & 0xff00) + + return answer + +def test_callback(ping): + print ping + + +class GPing: + """ + This class, when instantiated will start listening for ICMP responses. + Then call its send method to send pings. Callbacks will be sent ping + details + """ + def __init__(self,timeout=2,max_outstanding=10): + """ + :timeout - amount of time a ICMP echo request can be outstanding + :max_outstanding - maximum number of outstanding ICMP echo requests without responses (limits traffic) + """ + self.timeout = timeout + self.max_outstanding = max_outstanding + + # id we will increment with each ping + self.id = 0 + + # object to hold and keep track of all of our self.pings + self.pings = {} + + # event to file when we want to shut down + self.die_event = Event() + + # setup socket + icmp = socket.getprotobyname("icmp") + try: + self.socket = socket.socket(socket.AF_INET, socket.SOCK_RAW, icmp) + except socket.error, (errno, msg): + if errno == 1: + # Operation not permitted + msg = msg + ( + " - Note that ICMP messages can only be sent from processes" + " running as root." + ) + raise socket.error(msg) + raise # raise the original error + + self.receive_glet = gevent.spawn(self.__receive__) + self.processto_glet = gevent.spawn(self.__process_timeouts__) + + + def die(self): + """ + try to shut everything down gracefully + """ + print "shutting down" + self.die_event.set() + socket.cancel_wait() + gevent.joinall([self.receive_glet,self.processto_glet]) + + + def join(self): + """ + does a lot of nothing until self.pings is empty + """ + while len(self.pings): + gevent.sleep() + + + def send(self, dest_addr, callback, psize=64): + """ + Send a ICMP echo request. + :dest_addr - where to send it + :callback - what to call when we get a response + :psize - how much data to send with it + """ + # make sure we dont have too many outstanding requests + while len(self.pings) >= self.max_outstanding: + gevent.sleep() + + #resolve hostnames + dest_addr = socket.gethostbyname(dest_addr) + + # figure out our id + packet_id = self.id + + # increment our id, but wrap if we go over the max size for USHORT + self.id = (self.id + 1) % 2 ** 16 + + + # make a spot for this ping in self.pings + self.pings[packet_id] = {'sent':False,'success':False,'error':False,'dest_addr':dest_addr,'callback':callback} + + # Remove header size from packet size + psize = psize - 8 + + # Header is type (8), code (8), checksum (16), id (16), sequence (16) + my_checksum = 0 + + # Make a dummy heder with a 0 checksum. + header = struct.pack("bbHHh", ICMP_ECHO_REQUEST, 0, my_checksum, packet_id, 1) + bytes = struct.calcsize("d") + data = (psize - bytes) * "Q" + data = struct.pack("d", time.time()) + data + + # Calculate the checksum on the data and the dummy header. + my_checksum = checksum(header + data) + + # Now that we have the right checksum, we put that in. It's just easier + # to make up a new header than to stuff it into the dummy. + header = struct.pack( + "bbHHh", ICMP_ECHO_REQUEST, 0, socket.htons(my_checksum), packet_id, 1 + ) + packet = header + data + # note the send_time for checking for timeouts + self.pings[packet_id]['send_time'] = time.time() + + # send the packet + self.socket.sendto(packet, (dest_addr, 1)) # Don't know about the 1 + + #mark the packet as sent + self.pings[packet_id]['sent'] = True + + + def __process_timeouts__(self): + """ + check to see if any of our pings have timed out + """ + while not self.die_event.is_set(): + for i in self.pings: + if self.pings[i]['sent'] and time.time() - self.pings[i]['send_time'] > self.timeout: + self.pings[i]['error'] = True + self.pings[i]['callback'](self.pings[i]) + del(self.pings[i]) + break + gevent.sleep() + + + def __receive__(self): + """ + receive response packets + """ + while not self.die_event.is_set(): + # wait till we can recv + try: + socket.wait_read(self.socket.fileno()) + except socket.error, (errno,msg): + if errno == socket.EBADF: + print "interrupting wait_read" + return + # reraise original exceptions + print "re-throwing socket exception on wait_read()" + raise + + time_received = time.time() + received_packet, addr = self.socket.recvfrom(1024) + icmpHeader = received_packet[20:28] + type, code, checksum, packet_id, sequence = struct.unpack( + "bbHHh", icmpHeader + ) + + if packet_id in self.pings: + bytes_received = struct.calcsize("d") + time_sent = struct.unpack("d", received_packet[28:28 + bytes_received])[0] + self.pings[packet_id]['delay'] = time_received - time_sent + + # i'd call that a success + self.pings[packet_id]['success'] = True + + # call our callback if we've got one + self.pings[packet_id]['callback'](self.pings[packet_id]) + + # delete the ping + del(self.pings[packet_id]) + + else: + # should we raise an exception? + print "we received a ping we weren't expecting (bad id)" + + + +if __name__ == '__main__': + top_100_domains = ['google.com','facebook.com','youtube.com','yahoo.com','baidu.com','wikipedia.org','live.com','qq.com','twitter.com','amazon.com','linkedin.com','blogspot.com','google.co.in','taobao.com','sina.com.cn','yahoo.co.jp','msn.com','google.com.hk','wordpress.com','google.de','google.co.jp','google.co.uk','ebay.com','yandex.ru','163.com','google.fr','weibo.com','googleusercontent.com','bing.com','microsoft.com','google.com.br','babylon.com','soso.com','apple.com','mail.ru','t.co','tumblr.com','vk.com','google.ru','sohu.com','google.es','pinterest.com','google.it','craigslist.org','bbc.co.uk','livejasmin.com','tudou.com','paypal.com','blogger.com','xhamster.com','ask.com','youku.com','fc2.com','google.com.mx','xvideos.com','google.ca','imdb.com','flickr.com','go.com','tmall.com','avg.com','ifeng.com','hao123.com','zedo.com','conduit.com','google.co.id','pornhub.com','adobe.com','blogspot.in','odnoklassniki.ru','google.com.tr','cnn.com','aol.com','360buy.com','google.com.au','rakuten.co.jp','about.com','mediafire.com','alibaba.com','ebay.de','espn.go.com','wordpress.org','chinaz.com','google.pl','stackoverflow.com','netflix.com','ebay.co.uk','uol.com.br','amazon.de','ameblo.jp','adf.ly','godaddy.com','huffingtonpost.com','amazon.co.jp','cnet.com','globo.com','youporn.com','4shared.com','thepiratebay.se','renren.com'] + gp = GPing() + for domain in top_100_domains: + gp.send(domain,test_callback) + gp.join() From 09ec17536c7c87cf9e5d9615234ebfdae0a3f67c Mon Sep 17 00:00:00 2001 From: Ben Date: Thu, 2 Aug 2012 15:46:05 -0500 Subject: [PATCH 47/61] removing this shit getting a bunch of 'unexpected' ids when another scan in going. duh... --- gping.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/gping.py b/gping.py index 54c6221..7592db0 100644 --- a/gping.py +++ b/gping.py @@ -212,10 +212,6 @@ def __receive__(self): # delete the ping del(self.pings[packet_id]) - else: - # should we raise an exception? - print "we received a ping we weren't expecting (bad id)" - if __name__ == '__main__': From cadffc89597676fd05962919628cf60723bf832a Mon Sep 17 00:00:00 2001 From: btoews Date: Tue, 21 Aug 2012 08:35:29 -0500 Subject: [PATCH 48/61] fixed setup.py deps --- setup.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/setup.py b/setup.py index 7fce409..f64fd96 100755 --- a/setup.py +++ b/setup.py @@ -40,5 +40,7 @@ def get_authors(): maintainer_email="mastahyeti@gmail.com", url='https://github.com/mastahyeti/gping', keywords="ping icmp network latency gevent", - py_modules=['gping'] + py_modules=['gping'], + requires=['gevent'], + install_requires=['gevent'] ) From b4b60776cf302238dfe0da64a93498d6f6d10328 Mon Sep 17 00:00:00 2001 From: btoews Date: Tue, 21 Aug 2012 08:36:53 -0500 Subject: [PATCH 49/61] adding install instructions to README --- README.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index dcf03c8..a6e69a2 100644 --- a/README.md +++ b/README.md @@ -8,6 +8,10 @@ This is a fork of the python-ping project that strips out most everything and re gp.send("127.0.0.1",test_callback) gp.join() +## Install ## + +This *should* be easy on your average \*nix box (raw sockets are needed). Just type `sudo pip install gping` + ## License and Credits ## The python-ping project was started by Matthew Dixon Cowles. @@ -15,4 +19,4 @@ The python-ping project was started by Matthew Dixon Cowles. - copyleft 1989-2011 by the python-ping team, see AUTHORS for more details. - license: GNU GPL v2, see LICENSE for more details. -I have left the license and authors information intact, but this project seems to have a long history or forks and such so I am probably somehow pissing someone off or violating some license. Let me know if this is the case and I will be better. \ No newline at end of file +I have left the license and authors information intact, but this project seems to have a long history or forks and such so I am probably somehow pissing someone off or violating some license. Let me know if this is the case and I will be better. From 25984bfde43a60e7fe760ea223be1957a6de41ec Mon Sep 17 00:00:00 2001 From: Andreas Motl Date: Fri, 4 Nov 2016 01:57:38 +0100 Subject: [PATCH 50/61] Prepare version 0.2 --- .gitignore | 4 +++- setup.py | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 611e05b..73f11b6 100644 --- a/.gitignore +++ b/.gitignore @@ -4,4 +4,6 @@ /dist /build .pydevproject -/.settings \ No newline at end of file +/.settings +/.venv* +/.idea diff --git a/setup.py b/setup.py index f64fd96..12072fa 100755 --- a/setup.py +++ b/setup.py @@ -33,7 +33,7 @@ def get_authors(): setup( name='gping', - version="0.1", + version="0.2dev1", description='A gevent fork of python-ping.', author=get_authors(), maintainer="Ben Toews", From 2c604cfb87fe15cd0b61db991b8eb27e6e1a3df9 Mon Sep 17 00:00:00 2001 From: Andreas Motl Date: Fri, 4 Nov 2016 02:00:54 +0100 Subject: [PATCH 51/61] Add TODO.rst and CHANGES.rst. Update AUTHORS. Fix spaces. --- AUTHORS | 7 ++++--- CHANGES.rst | 25 +++++++++++++++++++++++++ TODO.rst | 22 ++++++++++++++++++++++ gping.py | 12 ++++++------ 4 files changed, 57 insertions(+), 9 deletions(-) create mode 100644 CHANGES.rst create mode 100644 TODO.rst diff --git a/AUTHORS b/AUTHORS index 3456c48..d73318c 100644 --- a/AUTHORS +++ b/AUTHORS @@ -3,13 +3,14 @@ AUTHORS / CONTRIBUTORS (alphabetic order): * Cowles, Matthew Dixon -- ftp://ftp.visi.com/users/mdc/ping.py * Diemer, Jens -- http://www.jensdiemer.de * Falatic, Martin -- http://www.falatic.com - * Hallman, Chris -- http://cdhallman.blogspot.com + * Hallman, Chris -- http://cdhallman.blogspot.com * incidence -- https://github.com/incidence * jcborras -- https://github.com/jcborras + * Motl, Andreas -- https://github.com/amotl * Notaras, George -- http://www.g-loaded.eu - * Poincheval, Jerome + * Poincheval, Jerome * Sarkhel, Kunal -- https://github.com/techwizrd * Stauffer, Samuel * Toews, Ben --- http://btoe.ws - * Zach Ware + * Zach Ware * zed -- https://github.com/zed diff --git a/CHANGES.rst b/CHANGES.rst new file mode 100644 index 0000000..7adb784 --- /dev/null +++ b/CHANGES.rst @@ -0,0 +1,25 @@ +*************** +gping changelog +*************** + +In progress +=========== +- Prepare version 0.2 + +2012-08-21 0.0 +============== +- Fix setup.py deps +- Add install instructions to README + +2012-07-17 0.0 +============== +- Add main gping.py + +2012-07-12 0.0 +============== +- Change __main__ action. Fix id overflow issue. + +2012-07-10 0.0 +============== +- First commit. Clean out stuff. Fix setup.py. +- Add README.md diff --git a/TODO.rst b/TODO.rst new file mode 100644 index 0000000..c71a6a2 --- /dev/null +++ b/TODO.rst @@ -0,0 +1,22 @@ +********** +gping todo +********** + +Tasks +===== +- [o] Add entrypoint ``gping`` and argument parsing + +Upstream changes +================ +- Add "Make it possible to ping without root access.": https://github.com/markotibold/gping/commit/b75fa2d41ec10073f894676fc3f3ec6b46693c05 +- Add "Allow binding to an interface": https://github.com/danharvey/pyping/commit/cb8489762d3171fb0b69c0fb9aaa8968d5e439b4 +- Add statsd support: https://github.com/ChristianKniep/python-ping/commit/a3653526e5857f346e2a244988df3849574ba385 +- Check "fix packet numbering bug": https://github.com/matesito/gping/commit/498c93500e35c8f5ad9c682df1e6c66fb5c5998a +- Check improvements from Vijayananda Reddy: https://github.com/VijayanandaReddy/gping/commit/25ac7c8efb6cf394b6c36ed2ed5b6712116b6c53 +- Check "For some reason, die was not working": https://github.com/biplav/gping/commit/377f0f4e85535fd3b6f812eb29d49ad8bf9a24fa +- Check "Changes to harden gping.py for usecases like die, ping of devices, timeouts": https://github.com/biplav/gping/commit/6fb9008d4ae63f954f6d59cfbf4bb445964c1351 +- Check features of leading non-gevent repositories (IPv6, etc.): + + - https://github.com/l4m3rx/python-ping + - https://github.com/tomwilkie/pyping + diff --git a/gping.py b/gping.py index 7592db0..e9a7b26 100644 --- a/gping.py +++ b/gping.py @@ -1,5 +1,5 @@ """ - This part is a fork of the python-ping project that makes + This part is a fork of the python-ping project that makes things work with gevent. """ @@ -162,8 +162,8 @@ def send(self, dest_addr, callback, psize=64): def __process_timeouts__(self): - """ - check to see if any of our pings have timed out + """ + check to see if any of our pings have timed out """ while not self.die_event.is_set(): for i in self.pings: @@ -176,8 +176,8 @@ def __process_timeouts__(self): def __receive__(self): - """ - receive response packets + """ + receive response packets """ while not self.die_event.is_set(): # wait till we can recv @@ -197,7 +197,7 @@ def __receive__(self): type, code, checksum, packet_id, sequence = struct.unpack( "bbHHh", icmpHeader ) - + if packet_id in self.pings: bytes_received = struct.calcsize("d") time_sent = struct.unpack("d", received_packet[28:28 + bytes_received])[0] From 40fc7e9268bacbfda07c28c2fcbc1e439b1a81fd Mon Sep 17 00:00:00 2001 From: Andreas Motl Date: Fri, 4 Nov 2016 02:49:03 +0100 Subject: [PATCH 52/61] Be graceful to hostname resolution failures --- CHANGES.rst | 1 + gping.py | 22 ++++++++++++++++++---- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/CHANGES.rst b/CHANGES.rst index 7adb784..f3c6e57 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -5,6 +5,7 @@ gping changelog In progress =========== - Prepare version 0.2 +- Be graceful to hostname resolution failures 2012-08-21 0.0 ============== diff --git a/gping.py b/gping.py index e9a7b26..6568ec6 100644 --- a/gping.py +++ b/gping.py @@ -117,9 +117,6 @@ def send(self, dest_addr, callback, psize=64): while len(self.pings) >= self.max_outstanding: gevent.sleep() - #resolve hostnames - dest_addr = socket.gethostbyname(dest_addr) - # figure out our id packet_id = self.id @@ -130,6 +127,16 @@ def send(self, dest_addr, callback, psize=64): # make a spot for this ping in self.pings self.pings[packet_id] = {'sent':False,'success':False,'error':False,'dest_addr':dest_addr,'callback':callback} + # Resolve hostname + try: + dest_ip = socket.gethostbyname(dest_addr) + self.pings[packet_id]['dest_ip'] = dest_ip + except socket.gaierror as ex: + self.pings[packet_id]['error'] = True + self.pings[packet_id]['message'] = str(ex) + return + + # Remove header size from packet size psize = psize - 8 @@ -155,7 +162,7 @@ def send(self, dest_addr, callback, psize=64): self.pings[packet_id]['send_time'] = time.time() # send the packet - self.socket.sendto(packet, (dest_addr, 1)) # Don't know about the 1 + self.socket.sendto(packet, (dest_ip, 1)) # Don't know about the 1 #mark the packet as sent self.pings[packet_id]['sent'] = True @@ -167,11 +174,18 @@ def __process_timeouts__(self): """ while not self.die_event.is_set(): for i in self.pings: + + # Detect timeout if self.pings[i]['sent'] and time.time() - self.pings[i]['send_time'] > self.timeout: self.pings[i]['error'] = True + self.pings[i]['message'] = 'Timeout after {} seconds'.format(self.timeout) + + # Handle all failures + if self.pings[i]['error'] == True: self.pings[i]['callback'](self.pings[i]) del(self.pings[i]) break + gevent.sleep() From d0ecb864af2eb26eb81e09a4e3826aa4f47a7ad4 Mon Sep 17 00:00:00 2001 From: Andreas Motl Date: Fri, 4 Nov 2016 03:15:04 +0100 Subject: [PATCH 53/61] Improve console output formatting --- CHANGES.rst | 1 + gping.py | 31 ++++++++++++++++++++++++++++--- 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/CHANGES.rst b/CHANGES.rst index f3c6e57..718fbcd 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -6,6 +6,7 @@ In progress =========== - Prepare version 0.2 - Be graceful to hostname resolution failures +- Improve console output formatting 2012-08-21 0.0 ============== diff --git a/gping.py b/gping.py index 6568ec6..2f13bbf 100644 --- a/gping.py +++ b/gping.py @@ -44,7 +44,15 @@ def checksum(source_string): return answer def test_callback(ping): - print ping + template = '{ip:20s}{delay:15s}{hostname:40s}{message}' + message = template.format( + hostname = ping['dest_addr'], + ip = ping['dest_ip'], + delay = ping['success'] and str(round(ping['delay'], 6)) or '', + message = 'message' in ping and ping['message'] or '' + ) + message = message.strip() + print >>sys.stderr, message class GPing: @@ -67,13 +75,16 @@ def __init__(self,timeout=2,max_outstanding=10): # object to hold and keep track of all of our self.pings self.pings = {} + # Hold failures + self.failures = [] + # event to file when we want to shut down self.die_event = Event() # setup socket icmp = socket.getprotobyname("icmp") try: - self.socket = socket.socket(socket.AF_INET, socket.SOCK_RAW, icmp) + self.socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, icmp) except socket.error, (errno, msg): if errno == 1: # Operation not permitted @@ -125,7 +136,7 @@ def send(self, dest_addr, callback, psize=64): # make a spot for this ping in self.pings - self.pings[packet_id] = {'sent':False,'success':False,'error':False,'dest_addr':dest_addr,'callback':callback} + self.pings[packet_id] = {'sent':False,'success':False,'error':False,'dest_addr':dest_addr,'dest_ip':None,'callback':callback} # Resolve hostname try: @@ -183,6 +194,7 @@ def __process_timeouts__(self): # Handle all failures if self.pings[i]['error'] == True: self.pings[i]['callback'](self.pings[i]) + self.failures.append(self.pings[i]) del(self.pings[i]) break @@ -226,11 +238,24 @@ def __receive__(self): # delete the ping del(self.pings[packet_id]) + def print_failures(self): + print >>sys.stderr + print >>sys.stderr, 'Failures:' + template = '{hostname:45}{message}' + for failure in self.failures: + message = template.format(hostname=failure['dest_addr'], message='message' in failure and failure['message']) + print >>sys.stderr, message if __name__ == '__main__': top_100_domains = ['google.com','facebook.com','youtube.com','yahoo.com','baidu.com','wikipedia.org','live.com','qq.com','twitter.com','amazon.com','linkedin.com','blogspot.com','google.co.in','taobao.com','sina.com.cn','yahoo.co.jp','msn.com','google.com.hk','wordpress.com','google.de','google.co.jp','google.co.uk','ebay.com','yandex.ru','163.com','google.fr','weibo.com','googleusercontent.com','bing.com','microsoft.com','google.com.br','babylon.com','soso.com','apple.com','mail.ru','t.co','tumblr.com','vk.com','google.ru','sohu.com','google.es','pinterest.com','google.it','craigslist.org','bbc.co.uk','livejasmin.com','tudou.com','paypal.com','blogger.com','xhamster.com','ask.com','youku.com','fc2.com','google.com.mx','xvideos.com','google.ca','imdb.com','flickr.com','go.com','tmall.com','avg.com','ifeng.com','hao123.com','zedo.com','conduit.com','google.co.id','pornhub.com','adobe.com','blogspot.in','odnoklassniki.ru','google.com.tr','cnn.com','aol.com','360buy.com','google.com.au','rakuten.co.jp','about.com','mediafire.com','alibaba.com','ebay.de','espn.go.com','wordpress.org','chinaz.com','google.pl','stackoverflow.com','netflix.com','ebay.co.uk','uol.com.br','amazon.de','ameblo.jp','adf.ly','godaddy.com','huffingtonpost.com','amazon.co.jp','cnet.com','globo.com','youporn.com','4shared.com','thepiratebay.se','renren.com'] gp = GPing() + + template = '{ip:20s}{delay:15s}{hostname:40s}{message}' + header = template.format(hostname='Hostname', ip='IP', delay='Delay', message='Message') + print >>sys.stderr, header + for domain in top_100_domains: gp.send(domain,test_callback) gp.join() + gp.print_failures() From 83243ef55c6aab1c5a54a08419fdc2d899517efb Mon Sep 17 00:00:00 2001 From: Andreas Motl Date: Fri, 4 Nov 2016 04:56:20 +0100 Subject: [PATCH 54/61] =?UTF-8?q?Add=20entrypoint=20=E2=80=9Cgping?= =?UTF-8?q?=E2=80=9D=20and=20argument=20parsing?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGES.rst | 1 + README.md | 22 +++++++++++++++++----- TODO.rst | 3 ++- gping.py | 30 ++++++++++++++++++++++++++---- setup.py | 11 +++++++++-- 5 files changed, 55 insertions(+), 12 deletions(-) diff --git a/CHANGES.rst b/CHANGES.rst index 718fbcd..41df00e 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -7,6 +7,7 @@ In progress - Prepare version 0.2 - Be graceful to hostname resolution failures - Improve console output formatting +- Add entrypoint ``gping`` and argument parsing 2012-08-21 0.0 ============== diff --git a/README.md b/README.md index a6e69a2..aafd8ae 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,19 @@ # GPing # -This is a fork of the python-ping project that strips out most everything and replaces it with gevent. The point is to have an event driven ping utility for doing many concurrent pings... +This is a fork of the python-ping project that strips out most everything and replaces it with gevent. +The point is to have an event driven ping utility for doing many concurrent pings... -## Example Usage ## +## Run on the commandline ## + + # Ping some hostnames + gping --hostnames=gnu.org,fsf.org,google.com,microsoft.com,googleusercontent.com,live.com,stackoverflow.com,141.1.1.1,8.8.8.8,192.168.123.1,192.168.999.1 + +## Demo: Top 100 domains ## + + # Ping the top 100 domains + time python gping.py + +## Use as library ## gp = GPing() gp.send("127.0.0.1",test_callback) @@ -16,7 +27,8 @@ This *should* be easy on your average \*nix box (raw sockets are needed). Just t The python-ping project was started by Matthew Dixon Cowles. - - copyleft 1989-2011 by the python-ping team, see AUTHORS for more details. - - license: GNU GPL v2, see LICENSE for more details. + - copyleft 1989-2011 by the python-ping team, see AUTHORS for more details. + - license: GNU GPL v2, see LICENSE for more details. -I have left the license and authors information intact, but this project seems to have a long history or forks and such so I am probably somehow pissing someone off or violating some license. Let me know if this is the case and I will be better. +I have left the license and authors information intact, but this project seems to have a long history or forks and such +so I am probably somehow pissing someone off or violating some license. Let me know if this is the case and I will be better. diff --git a/TODO.rst b/TODO.rst index c71a6a2..cb3607e 100644 --- a/TODO.rst +++ b/TODO.rst @@ -4,7 +4,8 @@ gping todo Tasks ===== -- [o] Add entrypoint ``gping`` and argument parsing +- [x] Add entrypoint ``gping`` and argument parsing +- [o] Add reading from stdin Upstream changes ================ diff --git a/gping.py b/gping.py index 2f13bbf..dfcc09c 100644 --- a/gping.py +++ b/gping.py @@ -7,6 +7,7 @@ import struct import sys import time +from args import args import gevent from gevent import socket @@ -247,15 +248,36 @@ def print_failures(self): print >>sys.stderr, message -if __name__ == '__main__': - top_100_domains = ['google.com','facebook.com','youtube.com','yahoo.com','baidu.com','wikipedia.org','live.com','qq.com','twitter.com','amazon.com','linkedin.com','blogspot.com','google.co.in','taobao.com','sina.com.cn','yahoo.co.jp','msn.com','google.com.hk','wordpress.com','google.de','google.co.jp','google.co.uk','ebay.com','yandex.ru','163.com','google.fr','weibo.com','googleusercontent.com','bing.com','microsoft.com','google.com.br','babylon.com','soso.com','apple.com','mail.ru','t.co','tumblr.com','vk.com','google.ru','sohu.com','google.es','pinterest.com','google.it','craigslist.org','bbc.co.uk','livejasmin.com','tudou.com','paypal.com','blogger.com','xhamster.com','ask.com','youku.com','fc2.com','google.com.mx','xvideos.com','google.ca','imdb.com','flickr.com','go.com','tmall.com','avg.com','ifeng.com','hao123.com','zedo.com','conduit.com','google.co.id','pornhub.com','adobe.com','blogspot.in','odnoklassniki.ru','google.com.tr','cnn.com','aol.com','360buy.com','google.com.au','rakuten.co.jp','about.com','mediafire.com','alibaba.com','ebay.de','espn.go.com','wordpress.org','chinaz.com','google.pl','stackoverflow.com','netflix.com','ebay.co.uk','uol.com.br','amazon.de','ameblo.jp','adf.ly','godaddy.com','huffingtonpost.com','amazon.co.jp','cnet.com','globo.com','youporn.com','4shared.com','thepiratebay.se','renren.com'] +def ping(hostnames): gp = GPing() template = '{ip:20s}{delay:15s}{hostname:40s}{message}' header = template.format(hostname='Hostname', ip='IP', delay='Delay', message='Message') print >>sys.stderr, header - for domain in top_100_domains: - gp.send(domain,test_callback) + for hostname in hostnames: + gp.send(hostname, test_callback) gp.join() gp.print_failures() + +def run(): + + """ + print 'Arguments passed in: ' + str(args.all) + print 'Flags detected: ' + str(args.flags) + print 'Files detected: ' + str(args.files) + print 'NOT files detected: ' + str(args.not_files) + print 'Grouped Arguments: ' + str(args.grouped) + print 'Assignments detected: ' + str(args.assignments) + """ + + if '--hostnames' in args.assignments: + hostnames_raw = args.assignments['--hostnames'].get(0) + hostnames = hostnames_raw.split(',') + ping(hostnames) + + +if __name__ == '__main__': + top_100_domains = ['google.com','facebook.com','youtube.com','yahoo.com','baidu.com','wikipedia.org','live.com','qq.com','twitter.com','amazon.com','linkedin.com','blogspot.com','google.co.in','taobao.com','sina.com.cn','yahoo.co.jp','msn.com','google.com.hk','wordpress.com','google.de','google.co.jp','google.co.uk','ebay.com','yandex.ru','163.com','google.fr','weibo.com','googleusercontent.com','bing.com','microsoft.com','google.com.br','babylon.com','soso.com','apple.com','mail.ru','t.co','tumblr.com','vk.com','google.ru','sohu.com','google.es','pinterest.com','google.it','craigslist.org','bbc.co.uk','livejasmin.com','tudou.com','paypal.com','blogger.com','xhamster.com','ask.com','youku.com','fc2.com','google.com.mx','xvideos.com','google.ca','imdb.com','flickr.com','go.com','tmall.com','avg.com','ifeng.com','hao123.com','zedo.com','conduit.com','google.co.id','pornhub.com','adobe.com','blogspot.in','odnoklassniki.ru','google.com.tr','cnn.com','aol.com','360buy.com','google.com.au','rakuten.co.jp','about.com','mediafire.com','alibaba.com','ebay.de','espn.go.com','wordpress.org','chinaz.com','google.pl','stackoverflow.com','netflix.com','ebay.co.uk','uol.com.br','amazon.de','ameblo.jp','adf.ly','godaddy.com','huffingtonpost.com','amazon.co.jp','cnet.com','globo.com','youporn.com','4shared.com','thepiratebay.se','renren.com'] + ping(top_100_domains) + diff --git a/setup.py b/setup.py index 12072fa..85c15c2 100755 --- a/setup.py +++ b/setup.py @@ -41,6 +41,13 @@ def get_authors(): url='https://github.com/mastahyeti/gping', keywords="ping icmp network latency gevent", py_modules=['gping'], - requires=['gevent'], - install_requires=['gevent'] + install_requires=['gevent', 'args'], + dependency_links=[ + 'https://github.com/kennethreitz/args/tarball/0a6d5eb#egg=args', + ], + entry_points={ + 'console_scripts': [ + 'gping = gping:run', + ], + }, ) From a14f94634515e0efc558a3ed5abfb25c8b6d6284 Mon Sep 17 00:00:00 2001 From: Andreas Motl Date: Fri, 4 Nov 2016 19:47:01 +0100 Subject: [PATCH 55/61] Update documentation --- AUTHORS | 1 + CHANGES.rst | 6 +++++- README.md | 42 ++++++++++++++++++++++++++++++++---------- TODO.rst | 16 ++++++++-------- 4 files changed, 46 insertions(+), 19 deletions(-) diff --git a/AUTHORS b/AUTHORS index d73318c..d6d070b 100644 --- a/AUTHORS +++ b/AUTHORS @@ -11,6 +11,7 @@ AUTHORS / CONTRIBUTORS (alphabetic order): * Poincheval, Jerome * Sarkhel, Kunal -- https://github.com/techwizrd * Stauffer, Samuel + * Tibold, Marko -- https://github.com/markotibold * Toews, Ben --- http://btoe.ws * Zach Ware * zed -- https://github.com/zed diff --git a/CHANGES.rst b/CHANGES.rst index 41df00e..234416c 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -6,8 +6,12 @@ In progress =========== - Prepare version 0.2 - Be graceful to hostname resolution failures +- Record all failures (hostname resolution and timeouts) in ``self.failures`` - Improve console output formatting -- Add entrypoint ``gping`` and argument parsing +- Add shell command entrypoint ``gping`` to ``setup.py`` +- Add argument parsing for interactive use, e.g. ``--hostnames=www.example.net,mail.example.net`` +- Incorporate "Make it possible to ping without root access" using ``socket.SOCK_DGRAM`` instead of ``socket.SOCK_RAW`` + by Marko Tibold: https://github.com/markotibold/gping/commit/b75fa2d4 2012-08-21 0.0 ============== diff --git a/README.md b/README.md index aafd8ae..56bb34f 100644 --- a/README.md +++ b/README.md @@ -1,34 +1,56 @@ # GPing # -This is a fork of the python-ping project that strips out most everything and replaces it with gevent. -The point is to have an event driven ping utility for doing many concurrent pings... +An asynchronous, event-driven, pure-python ping implementation using raw sockets based on gevent. +This is a fork of the python-ping project replacing its internal machinery with gevent. +The point is to have an event driven ping utility for doing a huge number of concurrent pings efficiently. -## Run on the commandline ## + +## Usage ## + +### Run on the commandline ### # Ping some hostnames gping --hostnames=gnu.org,fsf.org,google.com,microsoft.com,googleusercontent.com,live.com,stackoverflow.com,141.1.1.1,8.8.8.8,192.168.123.1,192.168.999.1 -## Demo: Top 100 domains ## +### Demo with 100 concurrent pings ### # Ping the top 100 domains time python gping.py -## Use as library ## +### Use as library ### + + from gping import GPing gp = GPing() - gp.send("127.0.0.1",test_callback) + gp.send("127.0.0.1", test_callback) gp.join() ## Install ## -This *should* be easy on your average \*nix box (raw sockets are needed). Just type `sudo pip install gping` +### From Python Package Index ### +This *should* be easy on your average \*nix box, just type: + + sudo pip install gping + + +### From GitHub ### +This way of installing gping is suitable for hacking on it: + + git clone https://github.com/zerotired/gping.git + cd gping + virtualenv .venv27 + source .venv27/bin/activate + python setup.py develop ## License and Credits ## The python-ping project was started by Matthew Dixon Cowles. - - copyleft 1989-2011 by the python-ping team, see AUTHORS for more details. + - copyleft 1989-2016 by the python-ping team, see AUTHORS for more details. - license: GNU GPL v2, see LICENSE for more details. -I have left the license and authors information intact, but this project seems to have a long history or forks and such -so I am probably somehow pissing someone off or violating some license. Let me know if this is the case and I will be better. +It was forked and ported to `gevent` by Ben Toews. Since then, this program is now called appropriately *GPing*. + +He says: +> I have left the license and authors information intact, but this project seems to have a long history or forks and such +> so I am probably somehow pissing someone off or violating some license. Let me know if this is the case and I will be better. diff --git a/TODO.rst b/TODO.rst index cb3607e..0d3c5a2 100644 --- a/TODO.rst +++ b/TODO.rst @@ -9,14 +9,14 @@ Tasks Upstream changes ================ -- Add "Make it possible to ping without root access.": https://github.com/markotibold/gping/commit/b75fa2d41ec10073f894676fc3f3ec6b46693c05 -- Add "Allow binding to an interface": https://github.com/danharvey/pyping/commit/cb8489762d3171fb0b69c0fb9aaa8968d5e439b4 -- Add statsd support: https://github.com/ChristianKniep/python-ping/commit/a3653526e5857f346e2a244988df3849574ba385 -- Check "fix packet numbering bug": https://github.com/matesito/gping/commit/498c93500e35c8f5ad9c682df1e6c66fb5c5998a -- Check improvements from Vijayananda Reddy: https://github.com/VijayanandaReddy/gping/commit/25ac7c8efb6cf394b6c36ed2ed5b6712116b6c53 -- Check "For some reason, die was not working": https://github.com/biplav/gping/commit/377f0f4e85535fd3b6f812eb29d49ad8bf9a24fa -- Check "Changes to harden gping.py for usecases like die, ping of devices, timeouts": https://github.com/biplav/gping/commit/6fb9008d4ae63f954f6d59cfbf4bb445964c1351 -- Check features of leading non-gevent repositories (IPv6, etc.): +- [x] Add "Make it possible to ping without root access.": https://github.com/markotibold/gping/commit/b75fa2d41ec10073f894676fc3f3ec6b46693c05 +- [o] Add "Allow binding to an interface": https://github.com/danharvey/pyping/commit/cb8489762d3171fb0b69c0fb9aaa8968d5e439b4 +- [o] Add statsd support: https://github.com/ChristianKniep/python-ping/commit/a3653526e5857f346e2a244988df3849574ba385 +- [o] Check "fix packet numbering bug": https://github.com/matesito/gping/commit/498c93500e35c8f5ad9c682df1e6c66fb5c5998a +- [o] Check improvements from Vijayananda Reddy: https://github.com/VijayanandaReddy/gping/commit/25ac7c8efb6cf394b6c36ed2ed5b6712116b6c53 +- [o] Check "For some reason, die was not working": https://github.com/biplav/gping/commit/377f0f4e85535fd3b6f812eb29d49ad8bf9a24fa +- [o] Check "Changes to harden gping.py for usecases like die, ping of devices, timeouts": https://github.com/biplav/gping/commit/6fb9008d4ae63f954f6d59cfbf4bb445964c1351 +- [o] Check features of leading non-gevent repositories (IPv6, etc.): - https://github.com/l4m3rx/python-ping - https://github.com/tomwilkie/pyping From 0b014b5348fe9d68da984fa93c4f5c1dc95ed6db Mon Sep 17 00:00:00 2001 From: Andreas Motl Date: Mon, 21 Nov 2016 20:08:38 +0100 Subject: [PATCH 56/61] Address minor issues requested from the discussion of PR #2 --- README.md | 2 +- gping.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 56bb34f..cf4acd2 100644 --- a/README.md +++ b/README.md @@ -36,7 +36,7 @@ This *should* be easy on your average \*nix box, just type: ### From GitHub ### This way of installing gping is suitable for hacking on it: - git clone https://github.com/zerotired/gping.git + git clone https://github.com/mastahyeti/gping.git cd gping virtualenv .venv27 source .venv27/bin/activate diff --git a/gping.py b/gping.py index dfcc09c..ab9ae00 100644 --- a/gping.py +++ b/gping.py @@ -141,7 +141,7 @@ def send(self, dest_addr, callback, psize=64): # Resolve hostname try: - dest_ip = socket.gethostbyname(dest_addr) + dest_ip = socket.gethostbyname(dest_addr) self.pings[packet_id]['dest_ip'] = dest_ip except socket.gaierror as ex: self.pings[packet_id]['error'] = True @@ -244,7 +244,7 @@ def print_failures(self): print >>sys.stderr, 'Failures:' template = '{hostname:45}{message}' for failure in self.failures: - message = template.format(hostname=failure['dest_addr'], message='message' in failure and failure['message']) + message = template.format(hostname=failure['dest_addr'], message=failure.get('message', 'unknown error')) print >>sys.stderr, message From 1c417a195551a00f504ce43c632882bb8f4a37db Mon Sep 17 00:00:00 2001 From: Andreas Motl Date: Thu, 10 Nov 2016 23:32:19 +0100 Subject: [PATCH 57/61] Incorporate "Allow binding to an interface" by Dan Harvey --- AUTHORS | 1 + CHANGES.rst | 1 + gping.py | 12 +++++++++--- setup.py | 2 +- 4 files changed, 12 insertions(+), 4 deletions(-) diff --git a/AUTHORS b/AUTHORS index d6d070b..b1e1167 100644 --- a/AUTHORS +++ b/AUTHORS @@ -4,6 +4,7 @@ AUTHORS / CONTRIBUTORS (alphabetic order): * Diemer, Jens -- http://www.jensdiemer.de * Falatic, Martin -- http://www.falatic.com * Hallman, Chris -- http://cdhallman.blogspot.com + * Harvey, Dan -- https://github.com/danharvey * incidence -- https://github.com/incidence * jcborras -- https://github.com/jcborras * Motl, Andreas -- https://github.com/amotl diff --git a/CHANGES.rst b/CHANGES.rst index 234416c..94f921e 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -12,6 +12,7 @@ In progress - Add argument parsing for interactive use, e.g. ``--hostnames=www.example.net,mail.example.net`` - Incorporate "Make it possible to ping without root access" using ``socket.SOCK_DGRAM`` instead of ``socket.SOCK_RAW`` by Marko Tibold: https://github.com/markotibold/gping/commit/b75fa2d4 +- Incorporate "Allow binding to an interface" by Dan Harvey: https://github.com/danharvey/pyping/commit/cb848976 2012-08-21 0.0 ============== diff --git a/gping.py b/gping.py index ab9ae00..1860bd8 100644 --- a/gping.py +++ b/gping.py @@ -62,13 +62,14 @@ class GPing: Then call its send method to send pings. Callbacks will be sent ping details """ - def __init__(self,timeout=2,max_outstanding=10): + def __init__(self, timeout=2, max_outstanding=10, bind=None): """ :timeout - amount of time a ICMP echo request can be outstanding :max_outstanding - maximum number of outstanding ICMP echo requests without responses (limits traffic) """ self.timeout = timeout self.max_outstanding = max_outstanding + self.bind = bind # id we will increment with each ping self.id = 0 @@ -96,6 +97,10 @@ def __init__(self,timeout=2,max_outstanding=10): raise socket.error(msg) raise # raise the original error + # Bind the socket to an interface + if self.bind: + self.socket.bind((self.bind, 0)) # Port number is irrelevant for ICMP + self.receive_glet = gevent.spawn(self.__receive__) self.processto_glet = gevent.spawn(self.__process_timeouts__) @@ -248,8 +253,9 @@ def print_failures(self): print >>sys.stderr, message -def ping(hostnames): - gp = GPing() +def ping(hostnames, **options): + options = options or {} + gp = GPing(**options) template = '{ip:20s}{delay:15s}{hostname:40s}{message}' header = template.format(hostname='Hostname', ip='IP', delay='Delay', message='Message') diff --git a/setup.py b/setup.py index 85c15c2..8452fdb 100755 --- a/setup.py +++ b/setup.py @@ -33,7 +33,7 @@ def get_authors(): setup( name='gping', - version="0.2dev1", + version="0.2dev2", description='A gevent fork of python-ping.', author=get_authors(), maintainer="Ben Toews", From 969ff2556dc53a8f0d88b9b9f6a1cb098b096211 Mon Sep 17 00:00:00 2001 From: Andreas Motl Date: Mon, 21 Nov 2016 00:05:13 +0100 Subject: [PATCH 58/61] Make verbose output optional --- CHANGES.rst | 5 ++- TODO.rst | 1 + gping.py | 90 ++++++++++++++++++++++++++++++++++++----------------- setup.py | 2 +- 4 files changed, 67 insertions(+), 31 deletions(-) diff --git a/CHANGES.rst b/CHANGES.rst index 94f921e..e0e4c13 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -14,10 +14,13 @@ In progress by Marko Tibold: https://github.com/markotibold/gping/commit/b75fa2d4 - Incorporate "Allow binding to an interface" by Dan Harvey: https://github.com/danharvey/pyping/commit/cb848976 -2012-08-21 0.0 + +2012-08-21 0.1 ============== - Fix setup.py deps - Add install instructions to README +- Tag release ``gping-0.1`` +- Make verbose output optional 2012-07-17 0.0 ============== diff --git a/TODO.rst b/TODO.rst index 0d3c5a2..542918e 100644 --- a/TODO.rst +++ b/TODO.rst @@ -6,6 +6,7 @@ Tasks ===== - [x] Add entrypoint ``gping`` and argument parsing - [o] Add reading from stdin +- [o] Add collectd metrics gathering: https://pythonhosted.org/collectd/ Upstream changes ================ diff --git a/gping.py b/gping.py index 1860bd8..f603262 100644 --- a/gping.py +++ b/gping.py @@ -44,17 +44,6 @@ def checksum(source_string): return answer -def test_callback(ping): - template = '{ip:20s}{delay:15s}{hostname:40s}{message}' - message = template.format( - hostname = ping['dest_addr'], - ip = ping['dest_ip'], - delay = ping['success'] and str(round(ping['delay'], 6)) or '', - message = 'message' in ping and ping['message'] or '' - ) - message = message.strip() - print >>sys.stderr, message - class GPing: """ @@ -62,7 +51,7 @@ class GPing: Then call its send method to send pings. Callbacks will be sent ping details """ - def __init__(self, timeout=2, max_outstanding=10, bind=None): + def __init__(self, timeout=2, max_outstanding=10, bind=None, callback=None): """ :timeout - amount of time a ICMP echo request can be outstanding :max_outstanding - maximum number of outstanding ICMP echo requests without responses (limits traffic) @@ -70,14 +59,18 @@ def __init__(self, timeout=2, max_outstanding=10, bind=None): self.timeout = timeout self.max_outstanding = max_outstanding self.bind = bind + self.callback = callback # id we will increment with each ping self.id = 0 - # object to hold and keep track of all of our self.pings + # Object to hold and keep track of all of our self.pings self.pings = {} - # Hold failures + # Keep track of results + self.results = [] + + # Keep track of failures self.failures = [] # event to file when we want to shut down @@ -123,16 +116,19 @@ def join(self): gevent.sleep() - def send(self, dest_addr, callback, psize=64): + def send(self, dest_addr, callback=None, psize=64): """ Send a ICMP echo request. :dest_addr - where to send it :callback - what to call when we get a response :psize - how much data to send with it """ + + callback = callback or self.callback + # make sure we dont have too many outstanding requests while len(self.pings) >= self.max_outstanding: - gevent.sleep() + gevent.sleep(0.01) # figure out our id packet_id = self.id @@ -184,6 +180,14 @@ def send(self, dest_addr, callback, psize=64): #mark the packet as sent self.pings[packet_id]['sent'] = True + def record_result(self, ping): + + # Keep track of ping results + self.results.append(ping) + + # Propagate individual callback + if 'callback' in ping and callable(ping['callback']): + ping['callback'](ping) def __process_timeouts__(self): """ @@ -199,7 +203,7 @@ def __process_timeouts__(self): # Handle all failures if self.pings[i]['error'] == True: - self.pings[i]['callback'](self.pings[i]) + self.record_result(self.pings[i]) self.failures.append(self.pings[i]) del(self.pings[i]) break @@ -238,13 +242,28 @@ def __receive__(self): # i'd call that a success self.pings[packet_id]['success'] = True - # call our callback if we've got one - self.pings[packet_id]['callback'](self.pings[packet_id]) + # Record the ping result + self.record_result(self.pings[packet_id]) # delete the ping del(self.pings[packet_id]) - def print_failures(self): + gevent.sleep() + + def print_report(self): + + # Output header + print >>sys.stderr + template = '{ip:20s}{delay:15s}{hostname:40s}{message}' + header = template.format(hostname='Hostname', ip='IP', delay='Delay', message='Message') + print >>sys.stderr, header + + # Output results + for result in self.results: + message = self.format_ping_result(result) + print >>sys.stderr, message + + # Output failures print >>sys.stderr print >>sys.stderr, 'Failures:' template = '{hostname:45}{message}' @@ -252,19 +271,32 @@ def print_failures(self): message = template.format(hostname=failure['dest_addr'], message=failure.get('message', 'unknown error')) print >>sys.stderr, message + def format_ping_result(self, ping): + template = '{ip:20s}{delay:15s}{hostname:40s}{message}' + message = template.format( + hostname = ping['dest_addr'], + ip = ping['dest_ip'], + delay = ping['success'] and str(round(ping['delay'], 6)) or '', + message = 'message' in ping and ping['message'] or '' + ) + message = message.strip() + return message + -def ping(hostnames, **options): +def ping(hostnames, verbose=False, **options): options = options or {} gp = GPing(**options) - template = '{ip:20s}{delay:15s}{hostname:40s}{message}' - header = template.format(hostname='Hostname', ip='IP', delay='Delay', message='Message') - print >>sys.stderr, header - for hostname in hostnames: - gp.send(hostname, test_callback) + gp.send(hostname) + gp.join() - gp.print_failures() + + if verbose: + gp.print_report() + + return gp + def run(): @@ -280,10 +312,10 @@ def run(): if '--hostnames' in args.assignments: hostnames_raw = args.assignments['--hostnames'].get(0) hostnames = hostnames_raw.split(',') - ping(hostnames) + ping(hostnames, verbose=True) if __name__ == '__main__': top_100_domains = ['google.com','facebook.com','youtube.com','yahoo.com','baidu.com','wikipedia.org','live.com','qq.com','twitter.com','amazon.com','linkedin.com','blogspot.com','google.co.in','taobao.com','sina.com.cn','yahoo.co.jp','msn.com','google.com.hk','wordpress.com','google.de','google.co.jp','google.co.uk','ebay.com','yandex.ru','163.com','google.fr','weibo.com','googleusercontent.com','bing.com','microsoft.com','google.com.br','babylon.com','soso.com','apple.com','mail.ru','t.co','tumblr.com','vk.com','google.ru','sohu.com','google.es','pinterest.com','google.it','craigslist.org','bbc.co.uk','livejasmin.com','tudou.com','paypal.com','blogger.com','xhamster.com','ask.com','youku.com','fc2.com','google.com.mx','xvideos.com','google.ca','imdb.com','flickr.com','go.com','tmall.com','avg.com','ifeng.com','hao123.com','zedo.com','conduit.com','google.co.id','pornhub.com','adobe.com','blogspot.in','odnoklassniki.ru','google.com.tr','cnn.com','aol.com','360buy.com','google.com.au','rakuten.co.jp','about.com','mediafire.com','alibaba.com','ebay.de','espn.go.com','wordpress.org','chinaz.com','google.pl','stackoverflow.com','netflix.com','ebay.co.uk','uol.com.br','amazon.de','ameblo.jp','adf.ly','godaddy.com','huffingtonpost.com','amazon.co.jp','cnet.com','globo.com','youporn.com','4shared.com','thepiratebay.se','renren.com'] - ping(top_100_domains) + ping(top_100_domains, verbose=True) diff --git a/setup.py b/setup.py index 8452fdb..0298e81 100755 --- a/setup.py +++ b/setup.py @@ -33,7 +33,7 @@ def get_authors(): setup( name='gping', - version="0.2dev2", + version="0.2dev3", description='A gevent fork of python-ping.', author=get_authors(), maintainer="Ben Toews", From fa565e50ac3c2c2e432110f2eab00b64eb0f1c39 Mon Sep 17 00:00:00 2001 From: Andreas Motl Date: Mon, 21 Nov 2016 20:55:12 +0100 Subject: [PATCH 59/61] =?UTF-8?q?Add=20=E2=80=9C--bind=E2=80=9D=20commandl?= =?UTF-8?q?ine=20argument=20for=20binding=20to=20a=20specific=20ip=20addre?= =?UTF-8?q?ss?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- gping.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/gping.py b/gping.py index f603262..fdf4523 100644 --- a/gping.py +++ b/gping.py @@ -90,7 +90,7 @@ def __init__(self, timeout=2, max_outstanding=10, bind=None, callback=None): raise socket.error(msg) raise # raise the original error - # Bind the socket to an interface + # Bind the socket to the interface of given IP address if self.bind: self.socket.bind((self.bind, 0)) # Port number is irrelevant for ICMP @@ -309,10 +309,22 @@ def run(): print 'Assignments detected: ' + str(args.assignments) """ + # The --hostnames argument is obligatory if '--hostnames' in args.assignments: + + # Compute list of hostnames from argument hostnames_raw = args.assignments['--hostnames'].get(0) hostnames = hostnames_raw.split(',') - ping(hostnames, verbose=True) + + # Compute additional options from arguments + options = {} + if '--bind' in args.assignments: + options['bind'] = args.assignments['--bind'].get(0) + + ping(hostnames, verbose=True, **options) + + else: + raise ValueError('Please specify the --hostnames= argument for passing a list of comma-separated hostnames') if __name__ == '__main__': From 8b1c7452baa203ef66d070586c0bab99b5e1e438 Mon Sep 17 00:00:00 2001 From: Andreas Motl Date: Mon, 21 Nov 2016 20:55:19 +0100 Subject: [PATCH 60/61] Update documentation --- CHANGES.rst | 11 +++++++---- README.md | 3 +++ 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/CHANGES.rst b/CHANGES.rst index e0e4c13..9efc285 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -4,7 +4,9 @@ gping changelog In progress =========== -- Prepare version 0.2 + +2016-11-21 0.2 +============== - Be graceful to hostname resolution failures - Record all failures (hostname resolution and timeouts) in ``self.failures`` - Improve console output formatting @@ -12,15 +14,16 @@ In progress - Add argument parsing for interactive use, e.g. ``--hostnames=www.example.net,mail.example.net`` - Incorporate "Make it possible to ping without root access" using ``socket.SOCK_DGRAM`` instead of ``socket.SOCK_RAW`` by Marko Tibold: https://github.com/markotibold/gping/commit/b75fa2d4 -- Incorporate "Allow binding to an interface" by Dan Harvey: https://github.com/danharvey/pyping/commit/cb848976 - +- Incorporate "Allow binding to an interface", e.g. ``--bind=192.168.111.2`` + by Dan Harvey: https://github.com/danharvey/pyping/commit/cb848976 +- Non-interactive mode: Record all results in ``self.results`` +- Non-interactive mode: Make verbose output optional 2012-08-21 0.1 ============== - Fix setup.py deps - Add install instructions to README - Tag release ``gping-0.1`` -- Make verbose output optional 2012-07-17 0.0 ============== diff --git a/README.md b/README.md index cf4acd2..da7e4e3 100644 --- a/README.md +++ b/README.md @@ -12,6 +12,9 @@ The point is to have an event driven ping utility for doing a huge number of con # Ping some hostnames gping --hostnames=gnu.org,fsf.org,google.com,microsoft.com,googleusercontent.com,live.com,stackoverflow.com,141.1.1.1,8.8.8.8,192.168.123.1,192.168.999.1 + # Bind to a specific IP address + gping --bind=192.168.111.2 --hostnames=google.com,stackoverflow.com,8.8.8.8 + ### Demo with 100 concurrent pings ### # Ping the top 100 domains From 45faad3ed29ac5ee9639f86da1b0d190ac23ceca Mon Sep 17 00:00:00 2001 From: Andreas Motl Date: Mon, 21 Nov 2016 20:55:42 +0100 Subject: [PATCH 61/61] Bump version --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index 0298e81..e8860b3 100755 --- a/setup.py +++ b/setup.py @@ -33,7 +33,7 @@ def get_authors(): setup( name='gping', - version="0.2dev3", + version="0.2", description='A gevent fork of python-ping.', author=get_authors(), maintainer="Ben Toews",