首页 > 其他分享 >通过telnetlib获取中兴交换机设备信息

通过telnetlib获取中兴交换机设备信息

时间:2024-09-11 16:25:10浏览次数:1  
标签:tn 中兴 host 交换机 telnetlib interface output port match

import telnetlib, re
from multiprocessing import Process
import pandas as pd

# 登录设备
def telnetDevice(host,port,command):
# 创建telnet连接
tn = telnetlib.Telnet(host, port, timeout=5) #paramiko
# 等待设备响应,通常需要一段时间
tn.read_until(b"Username:")
# 输入用户名
tn.write(username.encode('ascii') + b"\n")
tn.read_until(b"Password:")
# 输入密码
tn.write(password.encode('ascii') + b"\n")
# 等待设备登录并执行命令
tn.read_until(b"#")
# 设置不分页显示
# tn.write(b"screen-length 0 temporary\n") # huawei
tn.write(b"terminal length 0 \n") #zte
tn.read_until(b"#")
# 执行命令
tn.write(command.encode('ascii') + b"\n")
# 读取命令执行结果
output = tn.read_until(b"#")
# 打印输出
# print(output.decode('ascii'))
# 关闭连接
tn.close()
return output.decode('ascii')

def showIPInterfaces(host,port,command="show ip int bri"):
output = telnetDevice(host, port, command)
outputs = re.split(r'\n\s*\n', output.strip())[-1].splitlines()
interfaces = []
for line in outputs:
# 提取详细接口信息
interface_pattern = r'(\S+)\s+(\S+)\s+(\S+)\s+(down|up)\s+(down|up)\s+(down|up)\s*'
interface_matches = re.findall(interface_pattern, line)
if interface_matches != []:
match = interface_matches[0]
interfaces.append({
'interface': match[0],
'ip_address': match[1],
'mask': match[2],
'admin_status': match[3],
'phy_status': match[4],
'prot_status': match[5]
})
# print("Interfaces:", interfaces)
df = pd.DataFrame(interfaces)
print(df)
return df

def showInterfaces(host,port,command="show interface bri"):
output = telnetDevice(host, port, command)
outputs = re.split(r'\n\s*\n', output.strip())[-1].splitlines()
interfaces = []
for line in outputs:
# 提取详细接口信息
interface_pattern = r'(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(down|up)\s+(down|up)\s+(down|up)\s+(\S*)'
interface_matches = re.findall(interface_pattern, line)
if interface_matches != []:
match = interface_matches[0]
if len(match) == 8:
desc = match[7]
else:
desc = ""
interfaces.append({
'interface': match[0],
'portattribute': match[1],
'mode': match[2],
'bw(mbps)': match[3],
'admin_status': match[4],
'phy_status': match[5],
'prot_status': match[6],
'description': desc,
})
# print("Interfaces:", interfaces)
df = pd.DataFrame(interfaces)
print(df)
return df

def showIPOspfNeighbor(host,port,command="show ip ospf neighbor"):
output = telnetDevice(host, port, command)
outputs = re.split(r'\n\s*\n', output.strip())[-1].splitlines()
interfaces = []
for line in outputs:
# 提取详细接口信息
interface_pattern = r'(\S+)\s+(\d+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)'
interface_matches = re.findall(interface_pattern, line)
if interface_matches != []:
match = interface_matches[0]
interfaces.append({
'neighborid': match[0],
'pri': match[1],
'state': match[2],
'deadtime': match[3],
'address': match[4],
'interface': match[5]
})
# print("Interfaces:", interfaces)
df = pd.DataFrame(interfaces)
print(df)
return df

def showInterfacedetail(host,port,interface):
output = telnetDevice(host, port, "show interface " + interface + "\n")
# print(output)
msg = {}
msg["host"] = host
msg["interface"] = interface
pattern = r'In_Bytes\s+(\S+)\s+'
match = int(re.findall(pattern, output, re.S)[0])
msg["In_Bytes"] = match
pattern = r'E_Bytes\s+(\S+)\s+'
match = int(re.findall(pattern, output, re.S)[0])
msg["E_Bytes"] = match
print(msg)

def runcommands(host,port,command="show running"):
commands = command.split("\n")
# 创建telnet连接
tn = telnetlib.Telnet(host, port)
# 等待设备响应,通常需要一段时间
tn.read_until(b"Username:")
# 输入用户名
tn.write(username.encode('ascii') + b"\n")
tn.read_until(b"Password:")
# 输入密码
tn.write(password.encode('ascii') + b"\n")
# 等待设备登录并执行命令
tn.read_until(b"#")
# 设置不分页显示
tn.write(b"screen-length 0 temporary\n") # huawei
# tn.write(b"terminal length 0 \n") #zte
tn.read_until(b"#")
for cmd in commands:
# 执行命令
tn.write(cmd.encode('ascii') + b"\n")
# 读取命令执行结果
# 打印输出
# print(output.decode('ascii'))
# 关闭连接
output = tn.read_until(b"#",5)
output = output.decode('ascii')
tn.close()
print(output)
return output

def process_print():
print(time.time())

if __name__ == '__main__':
login_msgs = [{"ip":"129.60.161.169","port":"23"},{"ip":"129.60.161.170","port":"23"}]
# ip_interfaces =pd.DataFrame()
# for login_msg in login_msgs:
# host = login_msg["ip"]
# port = login_msg["port"]
# print(f"host = {host} , port = {port}")
# interfaces = showInterfaces(host, port)
# for interface in interfaces["interface"]:
# showInterfacedetail(host, port, interface)
# ip_interface = showIPOspfNeighbor(host,port)
# ip_interface["host"] = host
# print(ip_interface)
# ip_interfaces = pd.concat([ip_interfaces, ip_interface],ignore_index=True)
# print("-------------------")
# print(ip_interfaces)
processes = []
for login_msg in login_msgs:
host = login_msg["ip"]
port = login_msg["port"]
p = Process(target=showInterfaces(host, port))
p.start()
processes.append(p)

# 等待所有进程完成
for p in processes:
p.join()

标签:tn,中兴,host,交换机,telnetlib,interface,output,port,match
From: https://www.cnblogs.com/norvell/p/18408391

相关文章

  • Mininet MAC地址学习:通过Mininet模拟二层交换机和两个主机,通过两个主机通信来了解交换
    一.MAC地址学习1.登录我们创建mininet的虚拟机,创建一个线型拓扑,控制器设置为无。2.查看全部节点,查看链路信息,然后查看节点信息3.再打开一个终端(Terminal窗口2),然后打开交换机s1和交换机s2的二层(因为交换机s1和交换机s2是两个SDN交换机,在启动Mininet时没有指定任何控制器,交......
  • WGCLOUD可以监测交换机每个接口的上行和下行速率吗
    可以的WGCLOUD通过SNMP协议来获取设备的各种指标数据,比如基本信息、上下行总流量、各个接口的传输速率、cpu使用率、内存使用率、磁盘占用率、温度、电压、接口状态(UP或DOWN)......
  • 车载以太网交换机入门基本功(4)—优先级设计与VLAN测试
        在《车载以太网交换机入门基本功(3)》介绍了交换机端口属性和实际的VLAN转发过程。但是,当存在多个待转发的报文时,既要考虑到报文的及时性,又要考虑到转发效率,因此,如何进行有效调度就成了重要问题。一个解决办法是进行优先级设计。优先级设计    优先级设计包括报......
  • 嵌入式24千兆电口+4万兆光口管理型三层交换机RTL9301模块
    核心模块概述:嵌入式RTL9301模块可以支持4口万兆上联+24口千兆三层管理型以太网交换机,也就是最多可以提供24个10/100/1000自适应电口、4个10GbSFP+端口、1个console口、1个USB串口。完善的安全控制策略及CPU保护策略(CPUprotectpolicy)提高容错能力,保证网络的稳定运行和链路......
  • 长期可靠性:工业级以太网交换机的MTBF分析
    在工业自动化领域,设备的可靠性是保证生产效率和减少停机时间的关键因素。对于工业级以太网交换机而言,其长期可靠性的一个关键指标是MTBF(MeanTimeBetweenFailures,平均故障间隔时间)。本文将探讨MTBF的概念,以及如何通过MTBF分析来确保工业级以太网交换机的长期可靠性。MTBF的概念MT......
  • 无缝切换:Bypass交换机在关键任务网络中的应用
    在关键任务网络中,如金融交易系统、空中交通控制或紧急服务通信,任何形式的网络中断都可能导致严重的后果。因此,确保网络的高可用性变得至关重要。Bypass交换机,也称为旁路交换机或冗余交换机,通过提供无缝切换功能,在这些网络中发挥着至关重要的作用。本文将探讨Bypass交换机如何实现网......
  • 环境适应性:工业级以太网交换机的极端条件性能
    在工业自动化领域,网络基础设施常常面临着极端的环境条件。从酷热的沙漠到冰冷的北极,从潮湿的海洋气候到充满灰尘和震动的工厂车间,工业级以太网交换机必须具备出色的环境适应性,以确保网络的稳定性和可靠性。本文将探讨工业级以太网交换机如何适应极端环境条件,并保持其性能。工业环境......
  • ensp使用交换机配置svi连通网段
    ensp使用交换机配置svi连通网段实验目的如下图所示,PC1、PC2、PC3分别位于不同网段,使用S5700型号交换机连接,目前需要配置交换机和主机,主机能够互相连通。常用命令uninen:关闭信息通知disipintb:显示端口ip配置情况(brief模式)disiprouting-table:显示路由表vlan<编号>......
  • 升级你的网络速度 QNAP QSW-1105-5T 2.5GbE交换机 即插即用 网速瞬间翻倍
    Hey小伙伴们,今天要给大家安利一款超赞的网络神器——QNAPQSW-1105-5T2.5GbE交换机!......
  • 华为交换机添加ssh用户连接
    1.配置交换机的基本信息:        确保交换机的IP地址已配置,并且SSH功能已启用。2.创建本地用户:你可以通过创建本地用户并设置密码来允许SSH访问。local-user[username]passwordirreversible-cipher[password]local-user[username]privilegelevel[lev......