python测范围ip延迟
python检测范围ip的延迟,并记录。魔改于别人的代码。用于python3版本。
python3 ping ip 延迟
ping ip延迟4次记录到 ip_True.txt
失败ip记录到 ip_False.txt
# coding=utf-8
import time
import struct
import socket
import select
import sys
def chesksum(data):
"""
校验
"""
n = len(data)
m = n % 2
sum = 0
for i in range(0, n - m, 2):
sum += (data[i]) + ((data[i + 1]) << 8) # 传入data以每两个字节(十六进制)通过ord转十进制,第一字节在低位,第二个字节在高位
if m:
sum += (data[-1])
# 将高于16位与低16位相加
sum = (sum >> 16) + (sum & 0xffff)
sum += (sum >> 16) # 如果还有高于16位,将继续与低16位相加
answer = ~sum & 0xffff
# 主机字节序转网络字节序列(参考小端序转大端序)
answer = answer >> 8 | (answer << 8 & 0xff00)
return answer
'''
连接套接字,并将数据发送到套接字
'''
def raw_socket(dst_addr, imcp_packet):
rawsocket = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.getprotobyname("icmp"))
send_request_ping_time = time.time()
# send data to the socket
rawsocket.sendto(imcp_packet, (dst_addr, 80))
return send_request_ping_time, rawsocket, dst_addr
'''
request ping
'''
def request_ping(data_type, data_code, data_checksum, data_ID, data_Sequence, payload_body):
# 把字节打包成二进制数据
imcp_packet = struct.pack('>BBHHH32s', data_type, data_code, data_checksum, data_ID, data_Sequence, payload_body)
icmp_chesksum = chesksum(imcp_packet) # 获取校验和
imcp_packet = struct.pack('>BBHHH32s', data_type, data_code, icmp_chesksum, data_ID, data_Sequence, payload_body)
return imcp_packet
'''
reply ping
'''
def reply_ping(send_request_ping_time, rawsocket, data_Sequence, timeout=2):
while True:
started_select = time.time()
what_ready = select.select([rawsocket], [], [], timeout)
wait_for_time = (time.time() - started_select)
if what_ready[0] == []: # Timeout
return -1
time_received = time.time()
received_packet, addr = rawsocket.recvfrom(1024)
icmpHeader = received_packet[20:28]
type, code, checksum, packet_id, sequence = struct.unpack(
">BBHHH", icmpHeader
)
if type == 0 and sequence == data_Sequence:
return time_received - send_request_ping_time
timeout = timeout - wait_for_time
if timeout <= 0:
return -1
'''
实现 ping 主机/ip
'''
def ping(host):
cars = []
data_type = 8 # ICMP Echo Request
data_code = 0 # must be zero
data_checksum = 0 # "...with value 0 substituted for this field..."
data_ID = 0 # Identifier
data_Sequence = 1 # Sequence number
payload_body = b'abcdefghijklmnopqrstuvwabcdefghi' # data
dst_addr = socket.gethostbyname(host) # 将主机名转ipv4地址格式,返回以ipv4地址格式的字符串,如果主机名称是ipv4地址,则它将保持不变
print("正在 Ping {0} [{1}] 具有 32 字节的数据:".format(host, dst_addr))
yanc=0
for i in range(0, 4):
icmp_packet = request_ping(data_type, data_code, data_checksum, data_ID, data_Sequence + i, payload_body)
send_request_ping_time, rawsocket, addr = raw_socket(dst_addr, icmp_packet)
times = reply_ping(send_request_ping_time, rawsocket, data_Sequence + i)
if times > 0:
print("来自 {0} 的回复: 字节=32 时间={1}ms".format(addr, int(times * 1000)))
cars.append(int(times * 1000))
yanc=yanc+int(cars[i])
time.sleep(0.7)
else:
print("请求超时。")
return int(-1)
return str(yanc/4)
start_Time=int(time.time())
ip_True = open('ip_True.txt','w+')
ip_False = open('ip_False.txt','w+')
IPhost = []
IPbegin = (input(u'请输入起始查询IP: '))
IPend = input(u'请输入终止查询IP: ')
IP1 = IPbegin.split('.')[0]
IP2 = IPbegin.split('.')[1]
IP3 = IPbegin.split('.')[2]
IP4 = IPbegin.split('.')[-1]
IPend_last = IPend.split('.')[-1]
count_True,count_False = 0,0
for i in range(int(IP4)-1,int(IPend_last)):
ip = str(IP1+'.'+IP2+'.'+IP3+'.'+IP4)
int_IP4 = int(IP4)
int_IP4 += 1
IP4 = str(int_IP4)
yanchi=ping(ip)
if (yanchi==int(-1)):
print ('ping %s is fail'%ip)
ip_False.write(ip+'\n')
count_False += 1
else:
print ('ping %s is ok'%ip)
ip_True.write(ip+' 延迟:'+yanchi+'\n')
count_True += 1
ip_True.close()
ip_False.close()
end_Time = int(time.time())
print ("time(秒):",end_Time - start_Time,"s")
print ("ping通的ip数:",count_True," ping不通的ip数:",count_False)
参考
原文作者: ababwbq
原文链接: https://www.unkaer.cf/pyping.html
版权声明: 转载请注明出处(必须保留原文作者署名原文链接)