这两天实验室网络不给力,后来发现是有人占用了实验室太多的带宽,而登陆到实验室老的h3c s5500交换机上看端口流量情况很不方便,于是萌生写个小工具来统计端口流量情况,已求找到谁占用了大量带宽。

于是查了下,发现python 有个telnetlib的库,登陆交换机以及进行简单的操作相当简单,于是就写了这么个小工具:

*************************************工作原理********************************************

1、本程序采用telnet的方式登陆到交换机并通过不停的发送display interface [接口] 的方式请求交换机接口

信息并从中提取total input 和 total output的方式计算当前端口速率。

2、本程序仅统计状态为UP的端口的信息。使用display brief interface获取到状态为UP的接口。

3、本程序仅统计gigabytes端口。

*************************************其他**********************************************

1、第一次统计信息,由于没有时间,信息不准确

2、速度的单位都是MB

3、统计结果只能作为参考,并不能代表速率。

4、由于交换机本身更新接口信息速度并不快,而且telnet发送命令回传耗时也较大,本程序设置最小刷新时间,

当前为5s,若由于请求交换机接口所耗时超过5s,则刷新时间为请求耗时时间。

#!/usr/bin/python

import re

import telnetlib

import time

import platform

import os

Host = ‘.....‘

username = ‘‘

password = ‘‘

finish = ‘<....>‘

MIN_INTERVAL = 5.0 # use float

PORT_COUNT = 52 # h3c s5500 has 52 gigabyte ports

# return system type as a string

def get_system_info():

sys_platform = platform.system()

if sys_platform == ‘Linux‘ or sys_platform == ‘Darwin‘:

return ‘UNIX‘

elif sys_platform == ‘Windows‘:

return ‘WINDOWS‘

else:

return ‘UNIX‘

def clear_screen():

sys_type = get_system_info()

if sys_type == ‘UNIX‘:

os.system("clear")

elif sys_type == ‘WINDOWS‘:

os.system(‘cls‘)

else:

os.system(‘clear‘)

# login to the device and return the Telnet object

def login():

# telnet to the device

print ‘connect...‘

tn = telnetlib.Telnet(Host,timeout = 5)

tn.read_until(‘Username:‘,timeout=5)

tn.write(username + ‘\n‘)

tn.read_until(‘Password:‘)

tn.write(password + ‘\n‘)

tn.read_until(finish)

print ‘telnet success‘

return tn

#‘‘‘using Telnet object to get port status and return a tuple filled with ‘UP‘ ports‘‘‘

def get_up_ports(tn):

example = ‘‘‘

The brief information of interface(s) under bridge mode:

Interface Link Speed Duplex Link-type PVID

GE1/0/1 UP 100M(a) full(a) access 409

GE1/0/2 UP 100M(a) full(a) access 409

GE1/0/3 DOWN auto auto access 409‘‘‘

tn.write(‘display brief interface\n‘)

tn.write(‘ ‘)

tn.write(‘ ‘)

ports_brief = tn.read_until(finish)

up_ports = []

port_info= re.findall(r"GE1/0/(\d+)(\s+)(\w+)",ports_brief)

for i in port_info:

if i[-1] == ‘UP‘:

up_ports.append(i[0])

print up_ports

return tuple(up_ports)

def extract_data(port_str):

‘‘‘ get the data from the result of command ‘display interface GigabitEthernet 1/0/i‘

‘‘‘

# (VLAN_ID, total_input, total_output, max_input, max_output)

if re.search(‘GigabitEthernet‘, port_str) == None:

return None

VLAN_ID_list = re.findall(r"PVID: (\d+)",port_str)

input_total_list = re.findall(r"Input \(total\): (\d+) packets, (\d+) bytes", port_str)

output_total_list = re.findall(r"Output \(total\): (\d+) packets, (\d+) bytes", port_str)

peak_input_list = re.findall(r"Peak value of input: (\d+) bytes/sec,", port_str);

peak_output_list= re.findall(r"Peak value of output: (\d+) bytes/sec,", port_str);

state_list= re.findall(r"current state: (.+)",port_str)

VLAN_ID = VLAN_ID_list[0] # string

input_total = long((list(input_total_list[0]))[1])# long

output_total= long((list(output_total_list[0]))[1])# long

peak_input = long(peak_input_list[0])# long

peak_output = long(peak_output_list[0])# long

state = str(state_list[0])# string

return (VLAN_ID, input_total, output_total, peak_input, peak_output, state)

def do_statistic():

last_input = [0] * PORT_COUNT

last_output = [0] * PORT_COUNT

last_update = time.time()

tn = login()

up_ports = get_up_ports(tn)

clear_screen()

print ‘connected, waiting...‘

while(True):

ports_str = []

# h3c s5500 g1/0/1 - g1/0/52

# input command to get output

for i in up_ports:

tn.write(‘display interface GigabitEthernet 1/0/‘ + str(i) +‘\n‘)

tn.write(‘ ‘)

port_info = tn.read_until(finish)

ports_str.append(port_info)

# get interval

interval = (time.time() - last_update)

if interval < MIN_INTERVAL:

time.sleep(MIN_INTERVAL - interval)

interval = MIN_INTERVAL

# get data and print

clear_screen()

print "the input/output is from the port view of the switch."

print "From the user‘s view: input download; output upload."

print "the VLAN 1000 is connected to the firewall. So, it‘s opposite\n\n"

print ‘PORT_NO\tVLAN_ID\tINPUT\tOUTPUT\tMAX_IN\tMAX_OUT\tSTATE (MB)‘

port_index = 0

for _port_str in ports_str:

# (VLAN_ID, total_input, total_output, max_input, max_output)

data = extract_data(_port_str)

if data == None:

continue

port_no = up_ports[port_index]

vlan_id = data[0]

speed_input = (data[1] - last_input[port_index]) / (interval * 1024 * 1024)

speed_output = (data[2] - last_output[port_index]) / (interval * 1024 * 1024)

max_input = data[3] / (1024 * 1024 )

max_output = data[4] / (1024 * 1024 )

state = data[5]

last_input[port_index] = data[1]

last_output[port_index] = data[2]

port_index += 1

# show

print port_no, ‘\t‘, vlan_id, ‘\t‘,float(‘%.2f‘ %speed_input), ‘\t‘, float(‘%.2f‘ %speed_output), ‘\t‘,

print float(‘%.2f‘ %max_input), ‘\t‘ ,float(‘%.2f‘ %max_output), ‘\t‘, state

last_update = time.time()

if __name__ == "__main__":

username = raw_input("please input username:")

password = raw_input("please input password:")

print username

print password

do_statistic()

tn.close()

代码未做异常处理,仅实现了简单的流量统计功能。

查看san交换机端口流量_H3C 交换机telnet查看端口流量小工具相关推荐

  1. h3c交换机限制端口访问_H3C交换机端口限速和流量监管典型配置指导

    system-view [Switch] interface GigabitEthernet 1/0/3 [Switch-GigabitEthernet1/0/3] qos lr outbound c ...

  2. wireshark设置端口镜像_H3C交换机端口镜像,抓取数据包wireshark实战

    端口镜像 system-vies     //进入配置模式 用户名:admin 密码:admin(默认) [H3C] dis cu int  查看所有端口的配置 [H3C] mirroring-gro ...

  3. h3c交换机限制端口访问_h3c交换机设置限制公司员工访问外网

    h3c交换机设置限制公司员工访问外网 使用的环境,把不能访问80端口跟qq服务器的用户划入vlan 50. acl number 3001设置访问控制列表 rule 100 deny tcp dest ...

  4. h3c交换机限制端口访问_H3C交换机限制局域网端口网限方法

    配置思路 流量监管功能是通过QoS命令行实现.本案例中,用户采用固定IP,因此可以通过匹配用户IP地址的方法匹配用户流量,并对其做流量监管. 配置步骤 # 配置ACL规则匹配源IP为10.0.0.2的 ...

  5. telnet服务器端口无响应,分析Telnet 1433端口不通的原因

    Telnet服务可以在SQL2000中进行一些管理.但是很多人对于这部分的设置都不太清楚,会导致连接失败.那么Telnet 1433端口无法连接的原因有哪些呢?下面我们分析一下sql2000 Teln ...

  6. h3c交换机限制端口访问_H3C交换机典型访问控制列表(ACL)配置实例

    一 组网需求: 1 .通过配置基本访问控制列表,实现在每天 8:00 - 18:00 时间段内对源 IP 为 10.1.1.2 主机发出报文的过滤: 2 .要求配置高级访问控制列表,禁止研发部门与技术 ...

  7. telnet查看远程端口

    Telnet 默认未启用,启动telnet win+R快捷键,输入control,打开控制面板 选择程序与功能–>启用或关闭windows功能 打开Telnet 2.输入要查询的端口 3.退出t ...

  8. linux telnet 25端口,telnet 端口

    相关讨论 怎样关闭telnet端口? by 陈咬金 - Solaris - 2004-06-01 20:24:18阅读(2129) 回复(4) 远程登陆(telnet ),提示端口以被占用,登陆不上主 ...

  9. 【网络】H3C 交换机telnet查看端口流量Python小工具

    这两天实验室网络不给力,后来发现是有人占用了实验室太多的带宽,而登陆到实验室老的h3c s5500交换机上看端口流量情况很不方便,于是萌生写个小工具来统计端口流量情况,已求找到谁占用了大量带宽. 于是 ...

最新文章

  1. Excel vba引用工作表的三种写法
  2. CentOS 6.3下源码安装LAMP(Linux+Apache+Mysql+Php)环境
  3. 第一个Django应用程序_part1
  4. printf函数源码linux,再来一版简易的printf函数实现
  5. stl标准模板库_C ++标准模板库(STL)中的数组及其常用功能
  6. NSAttributedStringKey
  7. 直接插入排序中的监视哨问题
  8. 不使用vue-cli 搭建vue项目
  9. 如何将已加好的脚注或尾注转换成中括号“[]”格式
  10. 不要把敏感信息写在k8s的env上
  11. 天啦噜,竟然用AI来点名!你还敢逃课吗
  12. 学习网络安全一头雾水,想找些学习资料都不知道哪里入手?
  13. 流行音乐表明我们的注意力越来越短
  14. 蒙特卡洛树搜索(MTCS)
  15. 2. 监督学习之分类
  16. python的numpy库安装_Python库之numpy库的安装教程
  17. 搭建JIRA避坑指南
  18. 两级运算放大器设计与仿真
  19. GD32VF103_定时器中断
  20. 在Sublime Text 2中将默认语法设置为不同的文件类型

热门文章

  1. imagesize()函数获取图片信息
  2. 神经网络是算法还是模型,神经网络 图像相似度
  3. 【秘鲁收款】秘鲁外贸收款Pago Efectivo支付
  4. web安全 维护及其服务器的管理,web服务器的管理及维护.pdf
  5. C++实现Python变量
  6. Win7, VS2019下, pywin32安装
  7. Kubernetes 在本来生活网的落地实践
  8. spi转串口 linux驱动,RT_Thread WK2124 SPI转串口芯片驱动软件包
  9. Chapter~3 Python基础
  10. ros中msg文件的bool类型并不会生成bool类型变量