实验结果:
; 实验规划:
一.实验设备:
CSR1000v,需要保证其能够和运行python的设备通信。
二.CSR1000v上的配置:
配置SSH,后续方便python实验paramiko连接,获取信息。
配置netflow,制造流量,用于后续测试。
三.两个.py模块:
第一个使用paramiko,连接CSR1000v获取netfflow的信息。
第二个则调用第一个模块,得到netflow信息,并提取其中协议和对应数据流量的信息,再通过matplotlib进行画图。
具体配置:
一、CSR1000v上Netflow配置:
flow record test-record
match application name
collect counter bytes
!
flow monitor test-monitor
record test-record
!
interface GigabitEthernet1
ip flow monitor test-monitor input
netflow信息如下,后续需要提取的是ssh和telnet的相关数据,做饼状图:
SSH:略
二、python脚本代码:
ssh_csr1000v.py: 使用paramiko连接到设备,并返回显示的信息。具体解释请参考如下链接信息:
https://blog.csdn.net/tushanpeipei/article/details/113704076
import time
import paramiko
def multicmd(ip, username, password, cmd_list, enable='', wait_time=2, verbose=True):
ssh = paramiko.SSHClient()
ssh.load_system_host_keys()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(ip, 22, username, password, timeout=5, compress=True)
print("You have successfully connect to " + ip + '\n')
command = ssh.invoke_shell()
time.sleep(wait_time)
command.send('enable\n')
command.send(enable + '\n')
for cmd in cmd_list:
command.send(cmd)
time.sleep(wait_time)
output = command.recv(65535)
x = output.decode('ascii')
if verbose:
print(x)
return x
netflow_pie.py:
from matplotlib import pyplot as plt
import re
import ssh_csr1000v
def find_netflow_info(ip, username, password, cmd_list, enable):
result_raw = ssh_csr1000v.multicmd(ip, username, password, cmd_list, enable, verbose=False)
netflow_info_raw = re.findall('APP NAME[\s\S]+', result_raw)[0]
netflow_info = re.findall('\w+\s(\w+)\s+(\d+)',netflow_info_raw)
return netflow_info
def mat_bing(size_list, name_list):
plt.figure(figsize=(6, 6))
patches, label_text, percent_text = plt.pie(size_list,
labels=name_list,
labeldistance=1.1,
autopct='%3.1f%%',
shadow=False,
startangle=90,
pctdistance=0.6)
for l in label_text:
l.set_size = 30
for p in percent_text:
p.set_size = 20
plt.axis('equal')
plt.legend()
plt.show()
if __name__ == "__main__":
counters = []
protocols = []
command = ['show flow monitor name test-monitor cache format table\n']
netflow_info = find_netflow_info('192.168.1.155', 'Prin', 'Cisco123', command, 'cisco')
for info in netflow_info:
protocols.append(info[0])
counters.append(int(info[1]))
mat_bing(counters, protocols)
参考资料来源:现任明教教主
Original: https://blog.csdn.net/tushanpeipei/article/details/115434161
Author: 格洛米爱学习
Title: 使用python的matplotlib模块对netflow的流量信息分析(饼图)
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/769516/
转载文章受原作者版权保护。转载请注明原作者出处!