Script de Python para hacer una copia de seguridad del archivo de configuración del conmutador a través de FTP
Automatización de Python
Uno, instale el módulo Paramiko
Primero instale Python3,
luego instale el intérprete pip y
finalmente instale el módulo Paramiko
Abra Cmd en Windows + R e ingrese el comando:
pip3 instalar paramiko
Ingrese python e importe el módulo paramiko:
importar paramiko
2. Configurar el entorno experimental
1. Puente de tarjeta de red informática
2. Configure el conmutador
Configuración básica del conmutador
<Huawei>sys
[Huawei]sys SW1
[SW1]
[SW1]vlan 10
[SW1-vlan10]qu
[SW1]
[SW1]int Vlanif 10
[SW1-Vlanif10]ip add 192.168.1.254 24
[SW1]int g0/0/1
[SW1-GigabitEthernet0/0/1]port link-type access
[SW1-GigabitEthernet0/0/1]port default vlan 10
Pruebe la conectividad entre el conmutador y la computadora
Configuración de conmutador FTP
[SW1]ftp server enable
[SW1]user-interface vty 0 4
[SW1-ui-vty0-4]authentication-mode aaa
[SW1-ui-vty0-4]q
[SW1]aaa
[SW1-aaa]local-user huawei password cipher huawei123
[SW1-aaa]local-user huawei privilege level 15
[SW1-aaa]local-user huawei ftp-dir flash:
[SW1-aaa]local-user huawei service-type ftp
Verificar FTP
El servicio FTP es normal
Ver archivo de configuración del conmutador
El archivo de configuración es vrpcfg.zip
Tres, secuencia de comandos de Python
#欢迎关注喜欢华为的李工
# more ftp-huawei.py
# -*- coding=utf-8 -*-
from ftplib import FTP
import time,os
hostid="192.168.1.254"
Today = time.strftime("%Y-%m-%d", time.localtime())#获取当天时间
ftp = FTP()#创建变量
ftp.set_debuglevel(2)#开启debug模式
ftp.connect(host=hostid,port=21)#FTP地址(交换机地址)
ftp.login(user="huawei",passwd="huawei123")#FTP账号密码
#欢迎关注喜欢华为的李工
print(ftp.ftp登陆成功())#打印ftp登陆成功信息
os.chdir('./')#改变当前路径至(已经在该目录下创建了这个文件夹)
bufsize=1024
filename=hostid + "--{}.zip".format(Today)#创建日期为前缀的zip文件
file_handle=open(filename,"wb").write#创建写操作
ftp.retrbinary("RETR vrpcfg.zip",file_handle,bufsize)#ftp后获取vrpcfg.zip这个文件并写入本地文件
ftp.set_debuglevel(0)#关闭调试模式
ftp.quit()#关闭FTP
#欢迎关注喜欢华为的李工
Ver la carpeta antes de ejecutar el script
1. Ejecute el script
2. La copia de seguridad se realizó correctamente