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。
相关推荐
网络空间小黑2 小时前
TCP/IP 知识体系
网络·网络协议·tcp/ip·计算机网络·5g·wireshark·信息与通信
ALex_zry2 小时前
SSH主机密钥验证失败:全面解决方案与技术手册
运维·ssh
Dotrust东信创智3 小时前
面向SDV的在环测试深度解析——仿真中间件SIL KIT应用篇
网络·中间件·汽车
厦门辰迈智慧科技有限公司3 小时前
城市排水管网流量监测系统解决方案
运维·服务器
我没有开挂4 小时前
旧 docker 版本通过 nvkind 搭建虚拟多节点 gpu 集群的坑
运维·docker·容器
qq_339282234 小时前
centos中libc.so.6No such file的解决方式
linux·运维·centos
leoufung4 小时前
ECPF 简介
linux·网络·kernel
小鸡,啄米4 小时前
centos9安装docker 配置docker代理
运维·docker·容器
水银嘻嘻4 小时前
12 web 自动化之基于关键字+数据驱动-反射自动化框架搭建
运维·前端·自动化
在肯德基吃麻辣烫5 小时前
Netdata在Ubuntu环境下的安装与配置:构建实时系统监控与性能分析平台
linux·运维·ubuntu