005_ss-link.info的ping探测工具

用小工具ping.py测试距离您最快的节点
#!/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/socketubs/Pyping/
	: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 Response(object):
	def __init__(self):
		self.max_rtt = None
		self.min_rtt = None
		self.avg_rtt = None
		self.packet_lost = None
		self.ret_code = None
		self.output = []

		self.packet_size = None
		self.timeout = None
		self.destination = None
		self.destination_ip = None

class Ping(object):
	def __init__(self, destination, timeout=1000, packet_size=55, own_id=None, quiet_output=True, udp=False):
		self.quiet_output = quiet_output
		if quiet_output:
			self.response = Response()
			self.response.destination = destination
			self.response.timeout = timeout
			self.response.packet_size = packet_size

		self.destination = destination
		self.timeout = timeout
		self.packet_size = packet_size
		self.udp = udp

		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)
			if quiet_output:
				self.response.destination_ip = self.dest_ip
		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):
		msg = "\nPYTHON-PING %s (%s): %d data bytes" % (self.destination, self.dest_ip, self.packet_size)
		if self.quiet_output:
			self.response.output.append(msg)
		else:
			print(msg)

	def print_unknown_host(self, e):
		msg = "\nPYTHON-PING: Unknown host: %s (%s)\n" % (self.destination, e.args[1])
		if self.quiet_output:
			self.response.output.append(msg)
			self.response.ret_code = 1
		else:
			print(msg)

		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)

	   	msg = "%d bytes from %s: icmp_seq=%d ttl=%d time=%.1f ms" % (packet_size, from_info, icmp_header["seq_number"], ip_header["ttl"], delay)

		if self.quiet_output:
			self.response.output.append(msg)
			self.response.ret_code = 0
		else:
			print(msg)
		#print("IP header: %r" % ip_header)
		#print("ICMP header: %r" % icmp_header)

	def print_failed(self):
		msg = "Request timed out."

		if self.quiet_output:
			self.response.output.append(msg)
			self.response.ret_code = 1
		else:
			print(msg)

	def print_exit(self):
		msg = "\n----%s PYTHON PING Statistics----" % (self.destination)

		if self.quiet_output:
			self.response.output.append(msg)
		else:
			print(msg)

		lost_count = self.send_count - self.receive_count
		#print("%i packets lost" % lost_count)
		lost_rate = float(lost_count) / self.send_count * 100.0

		msg = "%d packets transmitted, %d packets received, %0.1f%% packet loss" % (self.send_count, self.receive_count, lost_rate)

		if self.quiet_output:
			self.response.output.append(msg)
			self.response.packet_lost = lost_count
		else:
			print(msg)

		if self.receive_count > 0:
			msg = "round-trip (ms)  min/avg/max = %0.3f/%0.3f/%0.3f" % (self.min_time, self.total_time / self.receive_count, self.max_time)
			if self.quiet_output:
				self.response.min_rtt = ‘%.3f‘ % self.min_time
				self.response.avg_rtt = ‘%.3f‘ % (self.total_time / self.receive_count)
				self.response.max_rtt = ‘%.3f‘ % self.max_time
				self.response.output.append(msg)
			else:
				print(msg)

		if self.quiet_output:
			self.response.output.append(‘\n‘)
		else:
			print(‘‘)

	#--------------------------------------------------------------------------

	def signal_handler(self, signum, frame):
		"""
		Handle print_exit via signals
		"""
		self.print_exit()
		msg = "\n(Terminated with signal %d)\n" % (signum)

		if self.quiet_output:
			self.response.output.append(msg)
			self.response.ret_code = 0
		else:
			print(msg)

		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.
		"""
		if not self.quiet_output:
			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()
		if self.quiet_output:
			return self.response

	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
			if self.udp:
				current_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.getprotobyname("udp"))
			else:
				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:
			self.response.output.append("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 ping(hostname, timeout=1000, count=3, packet_size=55, udp=False,*args, **kwargs):
	p = Ping(hostname, timeout, packet_size, *args, **kwargs)
	return p.run(count)

def allIP():
	#return ["8.8.8.8","a8.8.4.4"]
	r=[]
	try:
		import urllib2
		hdr = {‘User-Agent‘: ‘Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11‘,
				‘Accept‘: ‘text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8‘,
				‘Accept-Charset‘: ‘ISO-8859-1,utf-8;q=0.7,*;q=0.3‘,
				‘Accept-Encoding‘: ‘none‘,
				‘Accept-Language‘: ‘en-US,en;q=0.8‘,
				‘Connection‘: ‘keep-alive‘}
		req=urllib2.Request("https://www.ss-link.info/all",headers=hdr)

		s=urllib2.urlopen(req).read()
		r=s.split()
	except Exception,e:
		print "Network Error",e
	return r

totalCount=5
def mycompare(x,y):
	x1 = float(x.avg_rtt)+x.packet_lost*1000.0/totalCount
	y1 = float(y.avg_rtt)+y.packet_lost*1000.0/totalCount
	return int( 1000*(x1-y1) )
if __name__ == "__main__":
	ips=allIP()
	print "Start ..."
	results=[]
	for ip in ips:
		r=None
		try:
			r=ping(ip,count=totalCount)
		except Exception,e:
			print e
		if r:
			print r.destination_ip,"..."
			if r.avg_rtt == None: r.avg_rtt = 2000
			results.append(r)
	print ""
	print "The most fast IP:"
	results = sorted( results, cmp=mycompare )
	for r in results:
		print r.destination_ip,"AVG",r.avg_rtt,"LOST",r.packet_lost*1.0/totalCount

  

小工具ping.py是用Python语言开发的,可以运行在Windows, Linux, Mac OS等平台。下载请点击这里。ping.py自动从www.ss-link.com网站获取所有可用的节点,然后对每个节点发送5个ICMP ping,按平均响应时间和丢包率排序,给出结果。ping.py需要管理员权限运行,因为发送ICMP是需要管理员权限的。

在Linux和Mac OS下使用这个工具的方法是运行命令:sudo python ping.py

在Windows下需要先下载安装Python 2.7,下载Python请点击这里,64位的Windows选择Windows x86-64 MSI installer,32位的选择Windows x86 MSI installer。安装Python建议默认安装即可,默认是安装在C:\Python27 目录下。小工具ping.py建议也放到C:\Python27 目录下。按照下面的步骤运行。

用管理员模式运行cmd。在Windows的菜单的搜索框中输入cmd,会搜索出“命令提示符”,旁边是一个黑色小窗口的图标,类似。在这个小图标上点击鼠标右键,选择“以管理员身份运行”,如图

然后出现cmd黑色窗口,请注意窗口的左上角应该有“管理员”字样,这样表示是用的管理员身份运行的。在这个窗口执行如下两个命令“cd C:\Python27” 和“python ping.py”,注意不要带双引号,如果ping.py保存到了别的地方比如D:\tools 那么需要修改第二个命令为“python D:\tools\ping.py”

这个命令需要几分钟才能运行完成,完成后输出的“The most fast IP:”后面的部分,越靠前的IP速度越快。

时间: 2024-10-10 04:00:36

005_ss-link.info的ping探测工具的相关文章

OSSIM中主动与被动探测工具(pads+pf0+arpwatch)组合应用

OSSIM中主动与被动探测工具(pads+pf0+arpwatch)组合应用 OSSIM不仅降低了大家涉足IDS的门槛,而且为各种复杂的应用提供了一种快捷的平台,其中核心技术之一就是基于插件的事件提取,系统内置的180插件,几乎囊括了各大硬件设备厂商和各种网络应用.下面对OSSIM3 下把一些不起眼的小工具组合起来,就能为你解决大问题.下面就对pads+p0f+arpwatch的使用进行简单说明. 工具介绍 对于下面介绍的这些开源工具,在OSSIM中无需安装配置,你只要懂得如何应用就OK. Ar

linux 局域网探测工具nmap

NMap,也就是Network Mapper,是Linux下的网络扫描和嗅探工 具包,其基本功能有三个, 一是探测一组主机是否在线: 其次是扫描主机端口,嗅探所提供的网络服务: 还可以推断主机所用的操作系统 . Nmap可用于扫描仅有两个节点的LAN,直至500个节点以上的网络.Nmap 还允许用户定制扫描技巧.通常,一个简单的使用ICMP协议的ping操 作可以满足一般需求:也可以深入探测UDP或者TCP端口,直至主机所使用的操作系统:还可以将所有探测结果记录到各种格式的日志中, 供进一步分析

OSSIM中主动与被动探测工具(arpwatch+p0f+pads)组合应用

OSSIM中主动与被动探测工具(pads+pf0+arpwatch)组合应用 OSSIM不仅降低了大家涉足IDS的门槛,而且为各种复杂的应用提供了一种快捷的平台,其中核心技术之一就是基于插件的事件提取,系统内置的180插件,几乎囊括了各大硬件设备厂商和各种网络应用.下面对OSSIM3 下把一些不起眼的小工具组合起来,就能为你解决大问题.下面就对pads+p0f+arpwatch的使用进行简单说明. 工具介绍 对于下面介绍的这些开源工具,在OSSIM中无需安装配置,你只要懂得如何应用就OK. Ar

双心ping GUI工具1.0

双心ping GUI工具1.0该软件利用WindowsAPI提供了图形界面的ping程序,同时还可以调用DOS下的ping命令.ping成功后自动加入网址列表框及同目录下的列表文件Pinglist.ini.双击网址即可ping该网址. http://yunpan.cn/cw3LLw42ZJBg5  访问密码 84d9

网站robots.txt探测工具Parsero

robots.txt文件是网站根目录下的一个文本文件.robots.txt是搜索引擎中访问网站的时候要查看的第一个文件.当搜索引擎访问一个站点时,它会首先检查该站点根目录下是否存在robots.txt.如果存在,搜索引擎就会按照该文件中的内容来确定访问的范围:如果该文件不存在,则会够访问网站上所有没有被口令保护的所有页面. 网站为了防止搜索引擎访问一些重要的页面,会把其所在的目录放入robots.txt文件中.所以,探测该文件,也可以获取网站的重要信息.Kali Linux提供一个小工具Pars

网络探测工具(二)——traceroute

简述 搭建网络环境的时候,经常有想要知道报文从源到目的所经历的所有路由节点这样的需要.此时,traceroute小程序,后续简称为tracert,就有用武之地了. 功能 tracert,最初由大神Van Jacobson设计并实现(膜拜),可以用来探测出报文在网络中从源到目的所经过的所有三层路由节点. 原理 既然tracert用来探测路径,那么问题来了:它如何确定路径? 1. 确定路径 我们知道IP报文头有一个ttl字段,用来确定IP报文的存活时间.每经过一次转发,ttl数值减一,当ttl为0时

内核探测工具systemtap简介【转】

转自:http://www.cnblogs.com/hazir/p/systemtap_introduction.html systemtap是内核开发者必须要掌握的一个工具,本文我将简单介绍一下此工具,后续将会有系列文章介绍systemtap的用法. 什么是systemtap 假如现在有这么一个需求:需要获取正在运行的 Linux 系统的信息,如我想知道系统什么时候发生系统调用,发生的是什么系统调用等这些信息,有什么解决方案呢? 最原始的方法是,找到内核系统调用的代码,加上我们需要获得信息的代

内核探测工具systemtap简介

转自:http://www.cnblogs.com/hazir/p/systemtap_introduction.html systemtap是内核开发者必须要掌握的一个工具,本文我将简单介绍一下此工具,后续将会有系列文章介绍systemtap的用法. 什么是systemtap 假如现在有这么一个需求:需要获取正在运行的 Linux 系统的信息,如我想知道系统什么时候发生系统调用,发生的是什么系统调用等这些信息,有什么解决方案呢? 最原始的方法是,找到内核系统调用的代码,加上我们需要获得信息的代

ubuntu 下安装ping 命令工具

使用docker仓库下载的ubuntu 14.04 镜像.里面精简的连 ping 命令都没有.google 百度都搜索不到ping 命令在哪个包里. 努力找了半天,在一篇文件的字里行间发现了 ping 的来历- [email protected]:/# apt-get install inetutils-ping 还有ifconfig   可以用 apt-get install net-tools 来安装-