网络流量分析(在线网络测速)
生活随笔
收集整理的這篇文章主要介紹了
网络流量分析(在线网络测速)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
網絡流量分析
具體要求
- 收集自己本機的網絡流量數據(至少1小時)并進行數據顯示。
- 可用wireshark軟件抓包
- 網絡流量大小的時序圖,可按每半分鐘、每分鐘、每五分鐘、每十分鐘進行分別顯示。
- 流量協議類型直方圖
- 可設置過濾條件,顯示指定協議數據包、顯示時間段數據包、顯示長度范圍內的數據包
- 提示:由于代碼導入pyshark模塊,注意wireshark安裝路徑為C盤programfils 文件夾下,否則無法運行。
具體思路
- 要想對數據進行分析,首先要有數據,所以第一步要抓取數據
- 抓取數據我所知道的有兩種方法,第一種為通過代碼進行抓取,然后保存在文件中進行讀取,第二種通過wireshark等軟件進行抓取,然后通過代碼分析。
- 前者更傾向于分析實時數據包,后者則耗時間比較少(具體根據需要選擇)
- 拿到數據包以后,在分析之前,我們要通過代碼把數據包中的內容拿出來,我選擇pyshark.FileCapture方法
- 作圖我選擇導入matplotlib模塊,作圖會方便很多
- 具體的分析過程是一些簡單的選擇結構(ps:不懂得可以看一下Python基礎篇)
python代碼實現
# -*- coding: utf-8 -*-
import pyshark
from scapy.all import *
import matplotlib.pyplot as plt
# 讀取pcap文件
packets = pyshark.FileCapture("./net_package.pcap")
def protocal(packets):
"""
制作流量協議類型直方圖
:param packets: 讀取的pcap文件數據
"""
# 新建空字典
dict = {}
for packet in packets:
if packet.highest_layer not in dict.keys():
dict[packet.highest_layer] = 1
else:
dict[packet.highest_layer] += 1
# print(dict)
keys = dict.keys()
values = dict.values()
plt.figure(figsize=(8, 20), dpi=80)
plt.bar(keys, values)
plt.xticks(rotation=45)
plt.xlabel('protocal')
plt.ylabel('amount')
plt.title('the amounts of all protocals')
plt.show()
# print(proto_sum)
def graph_size(packets):
"""
作流量大小時序圖
:param packets: 讀取的pcap文件數據
"""
time_stamps = []
print("正在統計中。。。")
for packet in packets:
# print(int(float(packet.sniff_timestamp)))
time_stamps.append(int(float(packet.sniff_timestamp)))
# print(time_stamps)
print("統計完成!")
d = int(float(input("請輸入時間間隔(單位:分鐘):")) * 60)
# d = 30 #半分鐘
num_bins = (max(time_stamps) - min(time_stamps)) // d
step = len(time_stamps) // num_bins
time_labels = [time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(i)) for i in time_stamps[::step]]
# 新建20*8英寸圖形,分辨率為80
plt.figure(figsize=(20, 8), dpi=80)
# X軸分布數據以及num_bins條柱狀圖
plt.hist(time_stamps, num_bins)
# 標簽旋轉角度45
plt.xticks(range(min(time_stamps), max(time_stamps) + d, d), time_labels, rotation=45)
# plt.xticks(range(min(time_stamps),max(time_stamps)+d,d),rotation = 45)
plt.xlabel("timestamp")
plt.ylabel("amount")
plt.title("amount of per " + str(d) + " s")
plt.show()
def filter(packets):
"""
顯示過濾器
:param packets: 讀取的pcap文件數據
"""
protocal = input("請輸入協議類型:")
begin_time = input("請輸入開始時間(Example:2019-09-09 10:58:42):")
end_time = input("請輸入結束時間(Example:2019-09-09 11:40:00):")
length = int(input("請輸入最大長度限制(單位:字節):"))
# time.strptime把固定格式時間轉換為時間元組
array_begin_time = time.strptime(begin_time, "%Y-%m-%d %H:%M:%S")
# time.mktime把時間元組轉換為以秒表示的時間
begin_time_stamp = float(time.mktime(array_begin_time))
# print("begin_time_stamp:"+str(begin_time_stamp))
array_end_time = time.strptime(end_time, "%Y-%m-%d %H:%M:%S")
end_time_stamp = float(time.mktime(array_end_time))
# print("end_time_stamp:"+str(end_time_stamp))
packlist = []
for packet in packets:
# sniff_timestamp獲取開始嗅探的時間戳
time_stamp = float(packet.sniff_timestamp)
# 獲取數據包的捕獲長度
size = float(packet.captured_length)
if packet.highest_layer == protocal and time_stamp > begin_time_stamp and time_stamp < end_time_stamp and size <= length:
print(packet)
packlist.append(packet)
print("過濾出的數據包個數為 %s" % len(packlist))
# 調用函數進行操作
protocal(packets)
graph_size(packets)
filter(packets)
- 需要提前抓好數據包,在代碼中進行讀取,然后進行分析。
- 由于數據包較大,程序運行時間可能較長。
運行結果展示
- 流量協議類型直方圖
- 作流量大小時序圖
- 過濾器
- 按照控制臺提示輸入過濾條件
- 最后會輸出符合條件的數據包數量
總結
以上是生活随笔為你收集整理的网络流量分析(在线网络测速)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: RFC2616-HTTP1.1-Head
- 下一篇: iphone11单卡还是双卡