HCIA-Datacom实验指导手册:8 网络编程与自动化基础

HCIA-Datacom实验指导手册:8 网络编程与自动化基础

一、实验介绍:

通过本实验,读者将掌握 Python telnetlib 库的常用方法。

二、实验拓扑:

三、实验目的:

1.编写ypthon文件使用python的telnetlib库登录AR1,并执行save和tftp上传配置文件到管理电脑。

2.使用windows的计划任务,每5分钟执行一次上面的python文件。

四、配置步骤:

步骤 1 完成交换机的 Telnet 预配置

bash 复制代码
[ar 1-aaa]di th
[V200R003C00]
#
aaa 
 local-user huawei password cipher %$%$AV.6:XB(fVje3N0:Ky_,,^';%$%$
 local-user huawei privilege level 15
 local-user huawei service-type telnet
#
return
[ar 1-aaa]  

[Huawei]telnet server enable 
Info: The Telnet server has been enabled.

user-interface vty 0 4
 authentication-mode aaa
 protocol inbound  telnet 

步骤 2 Python 代码编写

bash 复制代码
#导入 telnetlib和time库。
import telnetlib
import time
host = '192.168.30.100' #路由器的管理ip地址
username = 'huawei' #定义telnet账号
password = 'Huawei@123' #定义telnet密码
tn = telnetlib.Telnet(host) #登陆 host='192.168.56.101'的设备,然后将telnetlib.Telnet(host)赋值给 tn。
tn.read_until(b"Username:") #read_until方法表示:读到什么为止。这里表示读到显示Username:为止。b表示将 Python3 中默认的 unicode 编码变为 bytes。
tn.write(username.encode('ascii') + b'\n') #写入字符串'huawei'。username.encode('ascii') 表示转换 username 代表的字符串"huawei"的编码类型为 ASCII
tn.read_until(b"Password:")
tn.write(password.encode('ascii') + b"\n")
tn.write(b'save 123.cfg\ny\ny\ntftp 192.168.30.1 put 123.cfg 2222.cfg\n') #先使用save命令保持配置文件,然后输入第一个y确认,第二个y确认覆盖。最后使用tftp 192.168.30.1 put 123.cfg 2222.cfg上传配置文件到192.168.30.1。
time.sleep(3) #上传配置文件需要一定时间,这里设置程序等待3秒后再执行后面的命令。
tn.write(b'screen-length 0 temporary\n display cu \n') #
time.sleep(3)
print(tn.read_very_eager().decode('ascii')) #print()表示显示括号内的内容到控制台。tn.read_very_eager()表示读取当前的尽可能多的数据。. decode('ascii'))表示将读取的数据解码为 ASCII。
tn.close() #调用 close()关闭当前会话。设备 vty 连接数量有限,在执行完脚本后需要关闭此 telnet 会话。

五、结果验证

bash 复制代码
C:\ProgramData\anaconda3\python.exe C:\myrepository\HCIA实验\telnet登录设备并备份配置文件.py 
C:\myrepository\HCIA实验\telnet登录设备并备份配置文件.py:2: DeprecationWarning: 'telnetlib' is deprecated and slated for removal in Python 3.13
  import telnetlib

  -----------------------------------------------------------------------------     
  User last login information:     
  -----------------------------------------------------------------------------
  Access Type: Telnet      
  IP-Address : 192.168.30.1     
  Time       : 2024-03-02 21:54:55-08:00     
  -----------------------------------------------------------------------------
<ar 1>save 123.cfg
 Are you sure to save the configuration to 123.cfg? (y/n)[n]:y
 flash:/123.cfg exists, overwrite? (y/n)[n]:y
  It will take several minutes to save configuration file, please wait......
  Configuration file had been saved successfully
  Note: The configuration file will take effect after being activated
<ar 1>tftp 192.168.30.1 put 123.cfg 2222.cfg
Info: Transfer file in binary mode.
Uploading the file to the remote TFTP server. Please wait...

TFTP: Uploading the file successfully.
    1027 bytes send in 1 second.
<ar 1>screen-length 0 temporary
Info: The configuration takes effect on the current user terminal interface only.
<ar 1> display cu 
[V200R003C00]
#
 sysname ar 1
#
 snmp-agent local-engineid 800007DB03000000000000
 snmp-agent 
#
 clock timezone China-Standard-Time minus 08:00:00
#
portal local-server load portalpage.zip
#
 drop illegal-mac alarm
#
 set cpu-usage threshold 80 restore 75
#
aaa 
 authentication-scheme default
 authorization-scheme default
 accounting-scheme default
 domain default 
 domain default_admin 
 local-user admin password cipher %$%$K8m.Nt84DZ}e#<0`8bmE3Uw}%$%$
 local-user admin service-type http
 local-user huawei password cipher %$%$AV.6:XB(fVje3N0:Ky_,,^';%$%$
 local-user huawei privilege level 15
 local-user huawei service-type telnet
#
firewall zone Local
 priority 15
#
interface GigabitEthernet0/0/0
 ip address 192.168.30.100 255.255.255.0 
#
interface GigabitEthernet0/0/1
#
interface GigabitEthernet0/0/2
#
interface NULL0
#
user-interface con 0
 authentication-mode password
user-interface vty 0 4
 authentication-mode aaa
user-interface vty 16 20
#
wlan ac
#
return
<ar 1>

Process finished with exit code 0

六、windows 计划任务程序配置

触发器:选择windows系统启动时开始计划任务。

操作:选择要定时执行的后缀为.py的python文件

最后确认时需要选择使用哪个windows账号和密码来执行。

七、 思考题与附加内容

  1. 如何使用 telnetlib 配置设备,例如配置设备管理接口地址?
    答:使用write(b'')写入相关的配置即可。
  2. 如何保存配置文件到本地目录?
    答:使用ftp、sftp、tftp都可以实现,上面我演示的是tftp。
相关推荐
鸠摩智首席音效师4 小时前
linux 系统中 Shutting Down, Restarting, Halting 有什么区别 ?
linux·运维·服务器
CIb0la4 小时前
Linux 将继续不支持 HDMI 2.1 实现
linux·运维·服务器
老蒋新思维5 小时前
创客匠人峰会深度解析:知识变现的 “信任 - 效率” 双闭环 —— 从 “单次交易” 到 “终身复购” 的增长密码
大数据·网络·人工智能·tcp/ip·重构·数据挖掘·创客匠人
卓码软件测评5 小时前
第三方高校软件课题验收测试机构:【使用Apifox测试gRPC服务步骤和技巧】
网络·测试工具·测试用例
吕了了5 小时前
85 微PE吕了了修改版--更新!
运维·windows·电脑·系统
掘根5 小时前
【消息队列】交换机数据管理实现
网络·数据库
鹿鸣天涯5 小时前
Kali Linux 2025.4 发布:桌面环境增强,新增 3 款安全工具
linux·运维·安全
学习&笔记6 小时前
MTK(系统篇)user版本无法使用setenforce 0命令关闭selinux权限
linux·运维·服务器
华硕之声6 小时前
WIN+R 指令大全
网络·数据·华硕
小小测试开发7 小时前
提升App UI自动化性能与效率:从脚本到架构的全链路优化指南
ui·架构·自动化