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。
相关推荐
可惜已不在41 分钟前
华为 HCIP-Datacom H12-821 题库 (25)
网络·华为
亿林科技网络安全1 小时前
阿里云盘照片事件!网络安全警钟长鸣
网络·安全·web安全
平头哥在等你1 小时前
《计算机网络名词解释》
服务器·网络·计算机网络
德迅--文琪1 小时前
SCDN是服务器吗?SCDN防御服务器有什么特点?
运维·服务器
ice___Cpu1 小时前
Linux 基本使用和 web 程序部署 ( 8000 字 Linux 入门 )
linux·运维·前端
z202305081 小时前
linux 之0号进程、1号进程、2号进程
linux·运维·服务器
时之彼岸Φ2 小时前
Web:HTTP包的相关操作
网络·网络协议·http
W21552 小时前
LINUX网络编程:http
网络·网络协议·http
Mogu_cloud2 小时前
pcdn盒子连接方式
网络·智能路由器
Hqst_Kevin3 小时前
Hqst 品牌 H81801D 千兆 DIP 网络变压器在光猫收发器机顶盒中的应用
运维·服务器·网络·5g·网络安全·信息与通信·信号处理