H3C Switch telnet view port traffic gadget

Source: Internet
Author: User

The two days of laboratory network did not give force, and later found that someone occupied the lab too much bandwidth, and landed in the laboratory old H3C s5500 switch to see the port traffic situation is very inconvenient, so the initiation of writing a small tool to statistics port traffic situation, has sought to find who occupied a large amount of bandwidth.

So I looked up and found that Python had a telnetlib library, a login switch and a simple operation that was quite simple, so I wrote this gadget:

Working principle ********************************************

1. This program uses Telnet to log in to the switch and requests the switch interface by sending the display interface [interface] in a non-stop manner.
Information and extract total input and total output from this to calculate the current port rate.

2, this program only statistics status is up the port information. Use the display brief interface to get to the interface with the status up.

3, this program only statistics gigabytes port.


Other **********************************************

1, the first statistical information, because there is no time, the information is inaccurate
2, the speed of the units are MB
3, statistical results can only be used as reference, and can not represent the rate.
4, because the switch itself update the interface information speed is not fast, and Telnet Send command callback time is also large, this program set the minimum refresh time,
Currently 5s, the refresh time is a time-consuming request if the switch interface takes more than 5s to request.

#!/usr/bin/pythonimport reimport telnetlibimport timeimport platformimport oshost = ' ... ' username = ' Password = ' fini SH = ' <....> ' min_interval = 5.0 # Use Floatport_count = * # H3C s5500 has the gigabyte ports# return system T Ype as a stringdef 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 objectdef login (): # Telnet to the Deviceprint ' connect ... ' tn = Telnetli B.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 an D 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 PVIDGE1/0/1 up 100M (a) full (a) Access 409GE1/0/2 up 100M (a) full (a) Access 409GE1/0/3 down Auto auto access 409 ' tn.write (' disp Lay 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_portsreturn 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 nonevlan_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 O F Input: (\d+) Bytes/sec, ", port_str);p eak_output_list= Re.findall (r" Peak value of output: (\d+) Bytes/sec, ", port_str); s tate_list= Re.findall (r "Current state: (. +)", port_str) vlan_id = vlan_id_list[0] # stringinput_total = Long (list (input_ TOTAL_LIST[0])) [1]) # longoutput_total= Long ((list (output_total_list[0)) [1]) # longpeak_input = Long (peak_input_list [0]) # longpeak_output = Long (peak_output_list[0]) # longstate = str (state_list[0]) # Stringreturn (vlan_id, Input_total, Output_total, Peak_input, Peak_output, State) def do_statistic (): Last_input = [0] * port_countlast_output = [0] * PORT_COUN Tlast_update = Time.time () tn = login () Up_ports = Get_up_ports (TN) clear_screen () print ' Connected, waiting ... ' while (True ):p orts_str = []# h3c s5500 g1/0/1-g1/0/52# input command to get outputfor i in Up_ports:tn.write (' Display interface Gig Abitethernet 1/0/' + str (i) + ' \ n ') tn.write(') Port_info = Tn.read_until (finish) Ports_str.append (port_info) # Get Intervalinterval = (Time.time ()-last_update) if Interval < MIN_INTERVAL:time.sleep (min_interval-interval) interval = min_interval# get data and Printclear_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 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 = 0for _port_str In ports_str:# (vlan_id, Total_input, Total_output, Max_input, max_output) data = Extract_data (_PORT_STR) if data = = None:c  Ontinueport_no = up_ports[port_index]vlan_id = Data[0]speed_input = (data[1]-last_input[port_index])/(interval * 1024 * 1024x768) Speed_output = (data[2]-last_output[port_index])/(interval * 1024x768 * 1024x768) Max_input = data[3]/(1024x768 * 1024x768) m Ax_output = data[4]/(1024x768 * 1024x768) state = Data[5]last_input[port_index] = Data[1]lasT_output[port_index] = Data[2]port_index + = Showprint port_no, ' \ t ', vlan_id, ' \ t ', float ('%.2f '%speed_input), ' \ t ', FL Oat ('%.2f '%speed_output), ' \ t ', print float ('%.2f '%max_input), ' \ t ', float ('%.2f '%max_output), ' \ t ', statelast_update = Time.time () if __name__ = = "__main__": Username = raw_input ("Please input username:") password = raw_input ("Please input PA ssWOrd: ") print Usernameprint passworddo_statistic () tn.close ()

  

The code does not do exception handling, only the simple traffic statistics function is realized.

H3C Switch telnet view port traffic gadget

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.