方式一:自己编写实现ping功能

#!/usr/bin/env python3
# -*- encoding = utf-8 -*-
# 优点:全部通过python3自带库实现,无需安装任何第三方依赖库
# 缺点:需要root权限
# 用法:sudo python3 ping.py <host>

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):
  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))
  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)))
      time.sleep(0.7)
    else:
      print("请求超时。")

if __name__ == "__main__":
  if len(sys.argv) < 2:
    sys.exit('Usage: ping.py <host>')
  ping(sys.argv[1])

方式二:通过更为强大的scapy库实现

#!/usr/bin/env python3
# -*- encoding = utf-8 -*-
# sudo apt install python3-scapy
# 调用scapy库实现ping功能
# 优点:几乎没有
# 缺点:需要root权限
from scapy.layers.inet import IP, ICMP
from scapy.packet import Raw
from scapy.sendrecv import sr1
import logging
logging.getLogger("scapy.runtime").setLevel(logging.ERROR)  # 清除报错信息

def ping_one(dst):  # 构建函数
    ping_pkt = IP(dst=dst)/ICMP() / b'welcome!'  # 构建数据包,目的地址为dst,数据部分为字节字符串welcome!
    ping_result = sr1(ping_pkt, timeout=2, verbose=False)  # 发送数据包并将返回结构赋值到ping_result中
    try:
        if ping_result.getlayer(IP).fields['src'] == dst and ping_result.getlayer(ICMP).fields['type'] == 0 \
                and ping_result.getlayer(Raw).fields['load'] == b'welcome!':
            # 判断返回数据包是否为reply,通过源地址,返回数据与返回类型进行判断
            return dst, 1  # 如果为返回数据包,函数返回输入的目的地址与1
        else:
            return dst, 2  # 如果不是返回数据包,则函数2
    except Exception:  # 如果无法ping通,则不会有返回结果,会出现报错,如果产生报错,直接返回dst与2
        return dst, 2

if __name__ == '__main__':
    try:
        while True:
            dst = input('请输入目的IP地址:')
            if dst == '':
                print('目的地址为空,请重新输入!')
            else:
                break
        result = ping_one(dst)
        if result[-1] == 1:
            print('目的', result[0], '可达!')
        else:
            print('目的', result[0], '不可达!')
    except KeyboardInterrupt:
        print('成功接收信号,退户程序!')

方式三:使用tcping实现

#!/usr/bin/env python3
# -*- encoding = utf-8 -*-
# 优点:无需root权限
# 缺点:需要安装依赖tcping,仅能通过tcp实现ping,必须指定端口
# python3 -m pip install tcping
# https://pypi.org/project/tcping/
from tcping import Ping
# ping_check(地址,端口,超时秒数,ping次数)
def ping_check(address,port,timeout,times):
    ping = Ping(address, port, timeout)
    ping.ping(times) #ping 4 次
    statistics = ping.result.rows
    # statistics = [Statistics(host='192.168.1.1', port='80', successed=4, failed=0, success_rate='100.00%', minimum='0.09ms', maximum='0.25ms', average='0.14ms')]
    print(statistics[0])
    ret = ping.result.raw
    print(ret)
    ret = ping.result.table
    print(ret)
    #从返回的statistics元组中获得成功次数和平均延迟
    successed = statistics[0][2]
    # 返回状态及平均延迟
    if successed > 0:
        return True, statistics[0][-1]
    else:
        return False, statistics[0][-1]
if __name__ == '__main__':
    i = input("请输入要Ping的地址和端口(例:192.168.1.1:80):")
    addr = i.split(":")
    ping_result = ping_check(addr[0],addr[1],5,4)
    print(ping_result)