通常来讲,手工备份交换机配置有两种方法:
1、通过命令dis cur来查看配置并保存;
2、使用ftp上传交换机的配置文件并保存;
很明显,使用第二种放手是更方便快捷的,命令简单,需要做的修改少,特别是在使用python来自动备份交换机配置的时候,优势更明显。
![如何使用Python批量备份交换机配置 图片[1]-如何使用Python批量备份交换机配置-不念博客](https://www.bunian.cn/wp-content/uploads/2024/03/image-112.png)
原理
华三交换机可以使用tftp命令将本机配置文件上传到ftp的服务器,所以我们可以把某台电脑终端作为ftp的服务器,将交换机配置文件上传至此服务器。
操作步骤
1、使用3CD软件将PC设位置FTP服务器;
2、编写python脚本,自动将交换机配置上传至服务器;
具体代码如下:
import paramikoimport timeusername = '交换机的用户名'password = '交换机的密码'# ip_address.txt这个文件存放的是交换机的ip地址,一行一个with open('ip_address.txt', 'r') as f:for line in f.readlines():try:swip = line.strip() # 删除字符串两边的空格ssh_client = paramiko.SSHClient()ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())ssh_client.connect(hostname=swip, username=username, password=password)print(f'Successfully connect to {swip}')remote_connection = ssh_client.invoke_shell()# 保存华三交换机配置,并修改配置文件的名称remote_connection.send('save' + '\n')remote_connection.send(f"{swip}_startup.cfg" + '\n')# 上传交换机配置remote_connection.send(f"tftp 192.168.1.1 put {swip}_startup.cfg" + '\n')time.sleep(1)except:print(f"{swip}交换机自动备份失败,请手动备份!!!")continueimport paramiko import time username = '交换机的用户名' password = '交换机的密码' # ip_address.txt这个文件存放的是交换机的ip地址,一行一个 with open('ip_address.txt', 'r') as f: for line in f.readlines(): try: swip = line.strip() # 删除字符串两边的空格 ssh_client = paramiko.SSHClient() ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh_client.connect(hostname=swip, username=username, password=password) print(f'Successfully connect to {swip}') remote_connection = ssh_client.invoke_shell() # 保存华三交换机配置,并修改配置文件的名称 remote_connection.send('save' + '\n') remote_connection.send(f"{swip}_startup.cfg" + '\n') # 上传交换机配置 remote_connection.send(f"tftp 192.168.1.1 put {swip}_startup.cfg" + '\n') time.sleep(1) except: print(f"{swip}交换机自动备份失败,请手动备份!!!") continueimport paramiko import time username = '交换机的用户名' password = '交换机的密码' # ip_address.txt这个文件存放的是交换机的ip地址,一行一个 with open('ip_address.txt', 'r') as f: for line in f.readlines(): try: swip = line.strip() # 删除字符串两边的空格 ssh_client = paramiko.SSHClient() ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh_client.connect(hostname=swip, username=username, password=password) print(f'Successfully connect to {swip}') remote_connection = ssh_client.invoke_shell() # 保存华三交换机配置,并修改配置文件的名称 remote_connection.send('save' + '\n') remote_connection.send(f"{swip}_startup.cfg" + '\n') # 上传交换机配置 remote_connection.send(f"tftp 192.168.1.1 put {swip}_startup.cfg" + '\n') time.sleep(1) except: print(f"{swip}交换机自动备份失败,请手动备份!!!") continue
注意
1、这个脚本的配置逻辑是基于华三V5版本的交换机,直接使用在其他交换机上可能出错;
2、修改交换机配置文件的名称可以向我这样先在交换机上修改,然后在上传配置,也可以先上传配置,在批量修改配置的文件名,我觉得这样子更好;
3、对比第一种方法,此方法配置命令简单,并且不用对配置文件做二次修改,第一种方法还要配置交换机的输出回显为一次性显示全部,如下:
[H3C-ui-vty0-4]screen-length ?INTEGER<0-512> Show lines on one screen (0 indicates no split screen)[H3C-ui-vty0-4]screen-length 0[H3C-ui-vty0-4]screen-length ? INTEGER<0-512> Show lines on one screen (0 indicates no split screen) [H3C-ui-vty0-4]screen-length 0[H3C-ui-vty0-4]screen-length ? INTEGER<0-512> Show lines on one screen (0 indicates no split screen) [H3C-ui-vty0-4]screen-length 0
© 版权声明
本站文章由不念博客原创,未经允许严禁转载!
THE END