实验目的:
公司有一台CE12800的设备,管理地址位172.16.1.2,现在需要编写自动化脚本,通过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
\[CE1\]ssh user python sftp-directory cfcard:
\[CE1\]ssh authentication-type default password
\[CE1\]sftp server enable
步骤4:编写python代码
完整代码如下:
import paramiko
tran = paramiko.Transport(('172.16.1.2',22))
tran.connect(username='python',password='Huawei@123')
sftp = paramiko.SFTPClient.from_transport(tran)
local_path = r'F:\\test\\test.cfg'
remote_path = '/vrpcfg.cfg'
sftp.get(remote_path,local_path)
print('get file succeed')
sftp.put(local_path,'/test.cfg')
print('put file succeed')
tran.close()
步骤5:编译器执行
步骤6:查看执行结果:
get file succeed
put file succeed
在本地电脑的F:\\test查看是否有备份的test.cfg的文件

在CE1设备上查看是否上传了test.cfg的文件

\