最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python實(shí)現(xiàn)ping命令小程序

 更新時(shí)間:2020年12月28日 17:26:23   作者:月為暮  
這篇文章主要介紹了python實(shí)現(xiàn)ping命令小程序的方法,幫助大家更好的理解和使用python,感興趣的朋友可以了解下

ping的原理是發(fā)送一個(gè)ICMP請(qǐng)求包,然后根據(jù)目的地址的應(yīng)答包來(lái)判斷是否能夠和這個(gè)主機(jī)進(jìn)行通信。
我們使用python實(shí)現(xiàn),借助于scapy來(lái)進(jìn)行編寫程序。

from scapy.all import *
import time,struct,random
# 編寫ping一個(gè)包的函數(shù)。
def ping_one(dst = '36.152.44.95',ttl_no = 64,id_no = 345,seq_no = 5):
  start_time = time.time()
  # 將時(shí)間轉(zhuǎn)換為二進(jìn)制序列。
  time_to_bytes = struct.pack('>d',start_time)
  # 進(jìn)行發(fā)送ICMP包,發(fā)送出去一個(gè),收回來(lái)一個(gè)。
  ping_one_result = sr1(IP(dst = dst,ttl = ttl_no)/ICMP(seq = seq_no,id = id_no)/time_to_bytes, timeout = 1, verbose=False)
  # print(ping_one_result.show())
  # 判斷收回來(lái)的包是不是ICMP的應(yīng)答包,和序列號(hào)是否相同。
  try:
    if ping_one_result.getlayer('ICMP').type == 0 and ping_one_result.getlayer('ICMP').seq == seq_no:
      # print('進(jìn)行解析包')
      # 提取IP頭部中的源IP地址,也就是我們ping的IP地址。
      reply_src_IP = ping_one_result.getlayer('IP').src
      # 提取序列號(hào)。
      reply_icmp_seq = ping_one_result.getlayer('ICMP').seq
      # 提取ttl
      reply_icmp_ttl = ping_one_result.getlayer('IP').ttl
      # 數(shù)據(jù)長(zhǎng)度等于 數(shù)據(jù)長(zhǎng)度(Raw) + 墊片長(zhǎng)度(Padding) + 8字節(jié)(ICMP頭部長(zhǎng)度)
      if ping_one_result.getlayer(Raw) != None:
        Raw_length = len(ping_one_result.getlayer(Raw).load)
      else:
        Raw_length = 0
      if ping_one_result.getlayer(Padding) != None:
        Padding_length = len(ping_one_result.getlayer(Padding).load)
      else:
        Padding_length = 0
      # 計(jì)算數(shù)據(jù)長(zhǎng)度。
      reply_data_length = Raw_length + Padding_length + 8
      # 取出數(shù)據(jù)部分,這里的數(shù)據(jù)部分是我們發(fā)送ICMP請(qǐng)求包的時(shí)候填入的時(shí)間。
      reply_data = ping_one_result.getlayer(Raw).load
      # 定義我們收包的時(shí)間。
      end_time = time.time()
      # 將數(shù)據(jù)時(shí)間部分進(jìn)行轉(zhuǎn)換。
      reply_data_time = struct.unpack('>d',reply_data)
      # 然后打印出轉(zhuǎn)換后的類型。
      # print(type(reply_data_time))
      # print(reply_data_time)
      time_to_pass_ms = (end_time - reply_data_time[0]) * 1000
      # (接收時(shí)間 - 發(fā)送時(shí)間) * 1000為毫秒數(shù)為消耗時(shí)間的毫秒數(shù)
      # print(time_to_pass_ms)
      return reply_data_length,reply_src_IP,reply_icmp_seq,reply_icmp_ttl,time_to_pass_ms
  except Exception as e:
    # 打印出錯(cuò)誤。
    # print('e', e)
    # 匹配錯(cuò)誤是否為NoneType類型。
    if re.match('.*NoneType.*', str(e)):
      print('錯(cuò)誤了')
      # 如果沒(méi)有回應(yīng),就返回None
      return None
def ping(dst = '36.152.44.95'):
  # 這里其實(shí)可以取進(jìn)程號(hào)的,但是我們用隨機(jī)生成一個(gè)數(shù)字模擬一下。
  id_no = random.randint(0,65535)
  # print(id_no)
  # 然后進(jìn)行發(fā)送5個(gè)數(shù)據(jù)包。
  for i in range(1,6):
    # 調(diào)用ping一個(gè)包函數(shù),入?yún)槟康男枰猵ing的IP地址。ttl,id,和序列號(hào)。seq。
    ping_result = ping_one(dst,64,id_no,i)
    if ping_result != None:
      print('%d bytes from %s: icmp_seq=%d ttl=%d time=%4.2f ms' % (ping_result[0], ping_result[1], ping_result[2], ping_result[3], ping_result[4]))
    else:
      print('.',end = '',flush = True)
    # 這里我們暫停一秒。
    time.sleep(1)

if __name__ == "__main__":
  ping('36.152.44.95')

但現(xiàn)在為止,我們的ping小程序就用python實(shí)現(xiàn)了,接下來(lái)就可以用wireshark工具抓包來(lái)看一下,進(jìn)行ping百度的地址。

以上就是python實(shí)現(xiàn)ping命令小程序的詳細(xì)內(nèi)容,更多關(guān)于python ping命令的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評(píng)論

印江| 会理县| 昌平区| 嫩江县| 南京市| 连平县| 汶上县| 浪卡子县| 河南省| 耒阳市| 瑞丽市| 镇安县| 江华| 淮安市| 徐州市| 盘锦市| 井陉县| 永善县| 丹巴县| 泸西县| 云梦县| 张家港市| 鄂尔多斯市| 广德县| 新巴尔虎左旗| 太仆寺旗| 昌黎县| 惠来县| 大洼县| 陆良县| 遂昌县| 英超| 洪雅县| 明水县| 安宁市| 灵山县| 兴和县| 仁怀市| 徐闻县| 霍州市| 肥东县|