Guest User

Untitled

a guest
Feb 17th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.30 KB | None | 0 0
  1. import socket
  2. import struct
  3. import sys
  4. import select
  5.  
  6. class PingProtocolData(object):
  7. def __init__(self):
  8. self.seq_number = 0
  9.  
  10. @property
  11. def ICMP_ECHO(self):
  12. return 8
  13.  
  14. @property
  15. def seq_number(self):
  16. return self.__seq_number
  17.  
  18. @property
  19. def identifier(self):
  20. return 0
  21.  
  22. @seq_number.setter
  23. def seq_number(self, seq_number):
  24. self.__seq_number = seq_number
  25.  
  26. def generate_ping_packet(self):
  27. checksum = 0
  28. header = struct.pack("!BBHHH", self.ICMP_ECHO, 0, checksum, self.identifier,
  29. self.seq_number)
  30. padBytes = "abcdefghijk"
  31. data = bytes(padBytes)
  32. #print data
  33. checksum = self.calculate_checksum(header+data)
  34. header = struct.pack("!BBHHH", self.ICMP_ECHO, 0, checksum, self.identifier,
  35. self.seq_number)
  36. packet = header + data
  37. print packet
  38. return packet
  39.  
  40.  
  41. def calculate_checksum(self, source_string):
  42. """
  43. A port of the functionality of in_cksum() from ping.c
  44. Ideally this would act on the string as a series of 16-bit ints (host
  45. packed), but this works.
  46. Network data is big-endian, hosts are typically little-endian
  47. """
  48. countTo = (int(len(source_string) / 2)) * 2
  49. sum = 0
  50. count = 0
  51.  
  52. # Handle bytes in pairs (decoding as short ints)
  53. loByte = 0
  54. hiByte = 0
  55. while count < countTo:
  56. if (sys.byteorder == "little"):
  57. loByte = source_string[count]
  58. hiByte = source_string[count + 1]
  59. else:
  60. loByte = source_string[count + 1]
  61. hiByte = source_string[count]
  62.  
  63. loByte = ord(loByte)
  64. hiByte = ord(hiByte)
  65.  
  66. sum = sum + (hiByte * 256 + loByte)
  67. count += 2
  68.  
  69. # Handle last byte if applicable (odd-number of bytes)
  70. # Endianness should be irrelevant in this case
  71. if countTo < len(source_string): # Check for odd length
  72. loByte = source_string[len(source_string) - 1]
  73. loByte = ord(loByte)
  74. sum += loByte
  75.  
  76. sum &= 0xffffffff # Truncate sum to 32 bits (a variance from ping.c, which
  77. # uses signed ints, but overflow is unlikely in ping)
  78.  
  79. sum = (sum >> 16) + (sum & 0xffff) # Add high 16 bits to low 16 bits
  80. sum += (sum >> 16) # Add carry from above (if any)
  81. answer = ~sum & 0xffff # Invert and truncate to 16 bits
  82. answer = socket.htons(answer)
  83.  
  84. return answer
  85.  
  86.  
  87. class Pinger(object):
  88. def __init__(self, ppd):
  89. self.ppd = ppd
  90.  
  91. def _send_ping(self, s, target):
  92. ping_packet = self.ppd.generate_ping_packet()
  93. s.sendto(ping_packet, (target, 1))
  94.  
  95. def _receive_ping(self, s):
  96. timeout = 2.5
  97. select.select([s], [], [], timeout)
  98. packet_data, address = s.recvfrom(2048)
  99. print packet_data, address
  100.  
  101.  
  102. def do(self):
  103. s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.getprotobyname("icmp"))
  104. s.setsockopt(socket.IPPROTO_IP, socket.IP_TTL, 1)
  105. self._send_ping(s, "8.8.8.8")
  106. self._receive_ping(s)
  107. s.close()
  108.  
  109.  
  110. def main():
  111. pinger = Pinger(PingProtocolData())
  112. pinger.do()
  113.  
  114.  
  115. if __name__ == "__main__":
  116. main()
Add Comment
Please, Sign In to add comment