python构造icmp echo请求和实现网络探测器功能代码分享

1041次阅读  |  发布于5年以前

python发送icmp echo requesy请求

复制代码 代码如下:

import socket
import struct

def checksum(source_string):
sum = 0
countTo = (len(source_string)/2)2
count = 0
while count<countTo:
thisVal = ord(source_string[count + 1])
256 + ord(source_string[count])
sum = sum + thisVal
sum = sum & 0xffffffff
count = count + 2
if countTo<len(source_string):
sum = sum + ord(source_string[len(source_string) - 1])
sum = sum & 0xffffffff
sum = (sum >> 16) + (sum & 0xffff)
sum = sum + (sum >> 16)
answer = ~sum
answer = answer & 0xffff
answer = answer >> 8 | (answer << 8 & 0xff00)
return answer

def ping(ip):
s = socket.socket(socket.AF_INET, socket.SOCK_RAW, 1)
packet = struct.pack(
"!BBHHH", 8, 0, 0, 0, 0
)
chksum=checksum(packet)
packet = struct.pack(
"!BBHHH", 8, 0, chksum, 0, 0
)
s.sendto(packet, (ip, 1))

if name=='main':
ping('192.168.41.56')

Copyright© 2013-2020

All Rights Reserved 京ICP备2023019179号-8