华为设备使用python实现文件自动保存下载

实验目的:

公司有一台CE12800的设备,管理地址为172.16.1.2,现在需要编写自动化脚本,STELNET实现设备的自动保存配置文件,使用SFTP实现设备的文件下载。

实验拓扑:

实验步骤:

步骤1:将本地电脑和ensp的设备进行桥接,桥接配置如下图所示:

步骤2:配置交换机的IP地址。
<HUAWEI>system-view immediately

HUAWEI\]sysname CE1 \[CE1\]interface Vlanif 1 \[CE1-Vlanif1\]ip address 172.16.1.2 24 \[CE1-Vlanif1\]quit \[CE1\]interface g1/0/0 \[CE1-GE1/0/0\]undo shutdown 测试本地的cmd窗口与CE1设备的连通性。 C:\\Users\\xxx\>ping 172.16.1.2 正在 Ping 172.16.1.2 具有 32 字节的数据: 来自 172.16.1.2 的回复: 字节=32 时间=19ms TTL=255 来自 172.16.1.2 的回复: 字节=32 时间=7ms TTL=255 来自 172.16.1.2 的回复: 字节=32 时间=5ms TTL=255 来自 172.16.1.2 的回复: 字节=32 时间=7ms TTL=255 172.16.1.2 的 Ping 统计信息: 数据包: 已发送 = 4,已接收 = 4,丢失 = 0 (0% 丢失), 往返行程的估计时间(以毫秒为单位): 最短 = 5ms,最长 = 19ms,平均 = 9ms 步骤3:配置CE1的SSH登陆。 1. 创建SSH登陆的账号 \[CE1\]aaa \[CE1-aaa\]local-user python password cipher Huawei@123 \[CE1-aaa\]local-user python user-group manage-ug \[CE1-aaa\]local-user python service-type ssh \[CE1-aaa\]local-user python level 3 1. 在CE1设备配置SSH用户的认证方式和服务类型。 \[CE1\]ssh user python \[CE1\]ssh user python authentication-type password \[CE1\]ssh user python service-type sftp stelnet \[CE1\]ssh user python sftp-directory cfcard: \[CE1\]ssh authentication-type default password \[CE1\]sftp server enable \[CE1\]stelnet server enable 步骤4:编写python代码 完整代码如下: from paramiko import SSHClient,AutoAddPolicy from time import sleep service_ip = '172.16.1.2' ssh_user = 'python' ssh_pass = 'Huawei@123' class datacom(): def __init__(self,ip,username,password): self.ip = ip self.username = username self.password = password self.ssh = self.ssh_connect() def ssh_connect(self): ssh = SSHClient() ssh.set_missing_host_key_policy(AutoAddPolicy) ssh.connect(self.ip,username=self.username,password=self.password) return ssh def save_config(self): shell = self.ssh.invoke_shell() shell.send('n\\n') sleep(2) shell.send('save CE1_save.zip\\n') sleep(2) shell.send('y\\n') sleep(2) shell.send('dir\\n') sleep(2) dis_file = shell.recv(999999).decode() print(dis_file) self.ssh.close() def down_file(self): remotename = 'CE1_save.zip' localname = r'F:\\test\\CE1_save.zip' self.ssh.open_sftp().get(remotename,localname) print('get file succeed') self.ssh.close() if __name__ =='__main__': Joinlabs = datacom(service_ip,ssh_user,ssh_pass) Joinlabs.save_config() Joinlabs = datacom(service_ip,ssh_user,ssh_pass) Joinlabs.down_file() 步骤5:编译器执行 ![](https://file.jishuzhan.net/article/1729747347732172802/21d1691b3ae146234eee71d01fae71df.webp) 步骤6:查看输出结果 Warning: The initial password poses security risks. The password needs to be changed. Change now? \[Y/N\]:n Info: The max number of VTY users is 5, the number of current VTY users online is 1, and total number of terminal users online is 2. The current login time is 2023-11-09 14:48:19. The last login time is 2023-11-09 11:52:29 from 172.16.1.1 through SSH. \save CE1_save.zip Warning: Are you sure to save the configuration to cfcard:/CE1_save.zip? \[Y/N\]:y Now saving the current configuration to the slot 17 Info: Save the configuration successfully. \dir Directory of cfcard:/ Idx Attr Size(Byte) Date Time FileName 0 dr-x - Nov 09 2023 10:03:28 $_checkpoint 1 dr-x - Nov 09 2023 09:36:26 $_install_mod 2 dr-x - Nov 09 2023 09:37:01 $_license 3 dr-x - Nov 09 2023 09:37:06 $_security_info 4 dr-x - Nov 09 2023 09:37:03 $_system 5 -rw- 0 Nov 09 2023 09:36:26 CE12800 6 -rw- 866 Nov 09 2023 14:48:23 ***CE1_save.zip*** 7 -rw- 104 Nov 09 2023 09:36:26 VRPV800R011C00SPC607B607D0203_s12800.cc 8 -rw- 2,893 Nov 09 2023 14:48:23 device.sys 9 -rw- 1,718 Nov 09 2023 10:13:31 test.cfg 10 -rw- 1,718 Nov 09 2023 09:36:26 vrpcfg.cfg 8,388,608 KB total (6,224,796 KB free) \ get file succeed 通过以上输出可以看出,设备的文件系统多了CE1_save.zip的保存配置文件。并且执行结果输出了get file succeed ,表示下载文件成功,在本地电脑上查看F:\\test 路径中是否存在 CE1_save.zip。 ![](https://file.jishuzhan.net/article/1729747347732172802/acfc3c25e1242bd726993a6493be8d34.webp) 代码解析: (1)导入库 from paramiko import SSHClient,AutoAddPolicy from time import sleep (2)定义变量 service_ip = '172.16.1.2' ssh_user = 'python' ssh_pass = 'Huawei@123' 将SSH 登陆需要用的IP、用户名、密码定义为变量。 (3)定义类 class datacom(): 定义类datacom,类名为datacom。在此类中定义三个方法 ssh_connect()、save_config()、down_file()。 ssh_connect()用于建立SSH连接; save_config()用于保存设备配置; down_file()用于下载设备的文件。 (4)定义构造函数 def __init__(self,ip,username,password): self.ip = ip self.username = username self.password = password self.ssh = self.ssh_connect() 构造方法__init__用于创建实例对象时使用,每当创建一个类的实例对象时,Python [解释器](https://www.zhihu.com/search?q=%E8%A7%A3%E9%87%8A%E5%99%A8&search_source=Entity&hybrid_search_source=Entity&hybrid_search_extra=%7B%22sourceType%22%3A%22answer%22%2C%22sourceId%22%3A2390824591%7D "解释器")都会自动调用它,用来初始化对象的某些属性。 def __init__(self,ip,username,password) 表示在调用类 datacom()时,所需要填入的ip地址,用户名和密码。 self.ip、self.username、self.password可以在同一个类的函数下进行传参的动作。 self.ssh = self.ssh_connect()代表调用下个步骤定义的ssh_connect()这个方法。 (5)定义ssh_connect()方法,用于建立SSH连接。 def ssh_connect(self): ssh = SSHClient() ssh.set_missing_host_key_policy(AutoAddPolicy) ssh.connect(self.ip,username=self.username,password=self.password) return ssh ssh.connect(self.ip,username=self.username,password=self.password)代表ssh登陆网络设备时,将输入ip、用户名、密码三个参数。 (6)定义save_config()方法,用于保存设备配置。 def save_config(self): shell = self.ssh.invoke_shell()#调用构造函数中的self.ssh,而self.ssh就是用于建立ssh连接的,并且开启交互式会话 shell.send('n\\n') #输入命令no ,登陆是设备提示修改密码,此时输入n代表不修改密码 sleep(2) shell.send('save CE1_save.zip\\n') #输入命令save CE1_save.zip 将保存的文件命名为CE1_save.zip sleep(2) shell.send('y\\n') sleep(2) shell.send('dir\\n') sleep(2) dis_file = shell.recv(999999).decode() print(dis_file) self.ssh.close() (7)定义down_file()方法,用于下载配置文件 def down_file(self): remotename = 'CE1_save.zip' localname = r'F:\\test\\CE1_save.zip' self.ssh.open_sftp().get(remotename,localname) print('get file succeed') self.ssh.close() (8)定义主函数,顺序执行 if __name__ =='__main__': Joinlabs = datacom(service_ip,ssh_user,ssh_pass) Joinlabs.save_config() Joinlabs = datacom(service_ip,ssh_user,ssh_pass) Joinlabs.down_file()

相关推荐
珠海西格电力科技1 小时前
微电网控制策略基础:集中式、分布式与混合式控制逻辑
网络·人工智能·分布式·物联网·智慧城市·能源
AAD555888996 小时前
数字仪表LCD显示识别与读数:数字0-9、小数点及单位kwh检测识别实战
python
开源技术8 小时前
Python Pillow 优化,打开和保存速度最快提高14倍
开发语言·python·pillow
syseptember8 小时前
Linux网络基础
linux·网络·arm开发
Li emily9 小时前
解决港股实时行情数据 API 接入难题
人工智能·python·fastapi
wfeqhfxz25887829 小时前
农田杂草检测与识别系统基于YOLO11实现六种杂草自动识别_1
python
mftang10 小时前
Python 字符串拼接成字节详解
开发语言·python
0思必得010 小时前
[Web自动化] Selenium设置相关执行文件路径
前端·爬虫·python·selenium·自动化
石去皿10 小时前
大模型面试通关指南:28道高频考题深度解析与实战要点
人工智能·python·面试·职场和发展
jasligea10 小时前
构建个人智能助手
开发语言·python·自然语言处理