点击上方网工进阶之路,选择设为星标

优质文章,及时送达

精华推荐

借助上章节内容,,我们实现了本地网络和ensp模拟器上设备的互通。

今天我们就来演示下如何使用python通过telnet批量登录华为交换机;

华为路由登陆器密码是什么_华为路由器登录网址192.168.3.1_华为路由器登录器

配置telnet服务器

这里我们在R1路由器上配置下telnet登录配置,从而能够通过本地telent登录;

[R1]telnet server enable[R1]user-interface vty  0  4[R1-ui-vty0-4]authentication-mode aaa[R1]aaa[R1-aaa]local-user huawei  password cipher  huawei[R1-aaa]local-user huawei  service-type  telnet[R1-aaa]local-user huawei   privilege level 3

在本地验证下,可以登录路由器R1;

华为路由器登录器_华为路由器登录网址192.168.3.1_华为路由登陆器密码是什么

使用python脚本登录设备

要通过 Python 脚本使用 Telnet批量登录华为交换机,你可以使用第三方库如telnetlib来实现。

首先,确保你已经安装了 telnetlib 库,如果没有安装,可以使用以下命令安装:

pip install telnetlib

然后,可以使用下面的 Python 脚本来实现批量 Telnet 登录和执行命令:

import telnetlib
def telnet_login(host, username, password, commands): try: # 连接 Telnet 服务器 tn = telnetlib.Telnet(host)
# 输入用户名 tn.read_until(b"Username:") tn.write(username.encode('ascii') + b"n")
# 输入密码 tn.read_until(b"Password:") tn.write(password.encode('ascii') + b"n")
# 登录成功后执行命令 for cmd in commands: tn.write(cmd.encode('ascii') + b"n") output = tn.read_until(b">").decode('ascii') # 根据设备的提示符来调整 print(output) # 打印命令输出
print("1") # 退出 Telnet tn.write(b"quitn") tn.close() except Exception as e: print(f"Telnet connection to {host} failed: {e}")



if __name__ == "__main__": host_list = ["192.168.56.2"] # 设备的 IP 地址列表 username = "huawei" password = "huawei" commands_to_run = ["display version", "display interface brief",""] # 要执行的命令列表
for host in host_list: print(f" Connecting to {host}...") telnet_login(host, username, password, commands_to_run)

在这个脚本中:

python执行结果

 Connecting to 192.168.56.2...
Info: The max number of VTY users is 10, and the number of current VTY users on line is 1. The current login time is 2024-04-09 22:21:33.display versionHuawei Versatile Routing Platform SoftwareVRP (R) software, Version 5.110 (eNSP V100R001C00)Copyright (c) 2000-2011 HUAWEI TECH CO., LTD
display interface briefPHY: Physical*down: administratively down^down: standby(l): loopback(s): spoofing(b): BFD down(e): ETHOAM down(d): Dampening SuppressedInUti/OutUti: input utility/output utilityInterface PHY Protocol InUti OutUti inErrors outErrorsEthernet0/0/0 up up 0% 0% 0 0Ethernet0/0/1 up up 0% 0% 0 0GigabitEthernet0/0/0 down down 0% 0% 0 0GigabitEthernet0/0/1 down down 0% 0% 0 0GigabitEthernet0/0/2 down down 0% 0% 0 0GigabitEthernet0/0/3 down down 0% 0% 0 0NULL0 up up(s) 0% 0% 0 0Serial0/0/0 down down 0% 0% 0 0Serial0/0/1 down down 0% 0% 0 0Serial0/0/2 down down 0% 0% 0 0Serial0/0/3 down down 0% 0% 0 0


创业/副业必备:

本站已持续更新1W+创业副业顶尖课程,涵盖多个领域。

点击查看详情

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。