Advertisement
Guest User

Untitled

a guest
Aug 23rd, 2017
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.28 KB | None | 0 0
  1. import os
  2. import sys
  3. import time
  4. import socket
  5. import struct
  6. import select
  7. import threading
  8.  
  9. if sys.platform == "win32":
  10. # On Windows, the best timer is time.clock()
  11. default_timer = time.clock
  12. else:
  13. # On most other platforms the best timer is time.time()
  14. default_timer = time.time
  15.  
  16. # From /usr/include/linux/icmp.h; your milage may vary.
  17. ICMP_ECHO_REQUEST = 8 # Seems to be the same on Solaris.
  18.  
  19.  
  20. def checksum(source_string):
  21. """
  22. I'm not too confident that this is right but testing seems
  23. to suggest that it gives the same answers as in_cksum in ping.c
  24. """
  25. sum = 0
  26. countTo = (len(source_string)/2)*2
  27. count = 0
  28. while count<countTo:
  29. thisVal = ord(source_string[count + 1])*256 + ord(source_string[count])
  30. sum = sum + thisVal
  31. sum = sum & 0xffffffff # Necessary?
  32. count = count + 2
  33.  
  34. if countTo<len(source_string):
  35. sum = sum + ord(source_string[len(source_string) - 1])
  36. sum = sum & 0xffffffff # Necessary?
  37.  
  38. sum = (sum >> 16) + (sum & 0xffff)
  39. sum = sum + (sum >> 16)
  40. answer = ~sum
  41. answer = answer & 0xffff
  42.  
  43. # Swap bytes. Bugger me if I know why.
  44. answer = answer >> 8 | (answer << 8 & 0xff00)
  45.  
  46. return answer
  47.  
  48.  
  49. def receive_one_ping(my_socket, ID, timeout):
  50. """
  51. receive the ping from the socket.
  52. """
  53. timeLeft = timeout
  54. while True:
  55. startedSelect = default_timer()
  56. whatReady = select.select([my_socket], [], [], timeLeft)
  57. howLongInSelect = (default_timer() - startedSelect)
  58. if whatReady[0] == []: # Timeout
  59. return
  60.  
  61. timeReceived = default_timer()
  62. recPacket, addr = my_socket.recvfrom(1024)
  63. icmpHeader = recPacket[20:28]
  64. type, code, checksum, packetID, sequence = struct.unpack(
  65. "bbHHh", icmpHeader
  66. )
  67. # Filters out the echo request itself.
  68. # This can be tested by pinging 127.0.0.1
  69. # You'll see your own request
  70. if type != 8 and packetID == ID:
  71. bytesInDouble = struct.calcsize("d")
  72. timeSent = struct.unpack("d", recPacket[28:28 + bytesInDouble])[0]
  73. return timeReceived - timeSent
  74.  
  75. timeLeft = timeLeft - howLongInSelect
  76. if timeLeft <= 0:
  77. return
  78.  
  79.  
  80. def send_one_ping(my_socket, dest_addr, ID):
  81. """
  82. Send one ping to the given >dest_addr<.
  83. """
  84. dest_addr = socket.gethostbyname(dest_addr)
  85.  
  86. # Header is type (8), code (8), checksum (16), id (16), sequence (16)
  87. my_checksum = 0
  88.  
  89. # Make a dummy heder with a 0 checksum.
  90. header = struct.pack("bbHHh", ICMP_ECHO_REQUEST, 0, my_checksum, ID, 1)
  91. bytesInDouble = struct.calcsize("d")
  92. data = (192 - bytesInDouble) * ("Q" * 5)
  93. data = struct.pack("d", default_timer()) + data
  94.  
  95. # Calculate the checksum on the data and the dummy header.
  96. my_checksum = checksum(header + data)
  97.  
  98. # Now that we have the right checksum, we put that in. It's just easier
  99. # to make up a new header than to stuff it into the dummy.
  100. header = struct.pack(
  101. "bbHHh", ICMP_ECHO_REQUEST, 0, socket.htons(my_checksum), ID, 1
  102. )
  103. packet = header + data
  104. my_socket.sendto(packet, (dest_addr, 1)) # Don't know about the 1
  105.  
  106.  
  107. def do_one(dest_addr, timeout):
  108. """
  109. Returns either the delay (in seconds) or none on timeout.
  110. """
  111. icmp = socket.getprotobyname("icmp")
  112. try:
  113. my_socket = socket.socket(socket.AF_INET, socket.SOCK_RAW, icmp)
  114. except socket.error, (errno, msg):
  115. if errno == 1:
  116. # Operation not permitted
  117. msg = msg + (
  118. " - Note that ICMP messages can only be sent from processes"
  119. " running as root."
  120. )
  121. raise socket.error(msg)
  122. raise # raise the original error
  123.  
  124. my_ID = os.getpid() & 0xFFFF
  125.  
  126. send_one_ping(my_socket, dest_addr, my_ID)
  127. #delay = receive_one_ping(my_socket, my_ID, timeout)
  128. delay = 0.01
  129. my_socket.close()
  130. return delay
  131.  
  132.  
  133. def verbose_ping(dest_addr, timeout = 2, count = 4):
  134. """
  135. Send >count< ping to >dest_addr< with the given >timeout< and display
  136. the result.
  137. """
  138. for i in xrange(count):
  139. print "ping %s..." % dest_addr,
  140. try:
  141. delay = do_one(dest_addr, timeout)
  142. except socket.gaierror, e:
  143. print "failed. (socket error: '%s')" % e[1]
  144. break
  145.  
  146. if delay == None:
  147. print "failed. (timeout within %ssec.)" % timeout
  148. else:
  149. delay = delay * 1000
  150. print "get ping in %0.4fms" % delay
  151. print
  152.  
  153. def start_threading():
  154. while True:
  155. verbose_ping("18.220.8.98")
  156.  
  157. if __name__ == '__main__':
  158. while True:
  159. t = threading.Thread(target=start_threading)
  160. t.start()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement