《思科 - GNS3 - 备份配置》

52 篇文章 8 订阅
9 篇文章 4 订阅

Python实现网络设备的配置备份

 

实验目的:在centos8主机上开启FTP server服务,创建python脚本,将SW1~SW5的running  configuration 备份保存到TFTP服务器上;

实验准备:

1.在VMware虚拟机安装CentOS 8 的主机,输入下列命令下载安装vsftpd(FTP服务),安装前需要确认主机能否连通外网

yum install vsftpd -y

2.当前启用ftp服务:

systemctl start vsftpd

3.开机自启动ftp服务:

systemctl enable vsftpd

4.确认vsftpd已被启动运行:

systemctl status vsftpd

5.关闭防火墙

systemctl stop firewalld

systemctl status firewalld                #查看防火墙被关闭

6.VM创建新用户     #作为ftp用户

useradd -create-home python

passwd python

7.创建ip.txt,放入需要备份的交换机的IP地址

实验代码如下:

import paramiko

import time

import getpass

username = input('Username:')

password = getpass.getpass('password:')

f = open("ip.txt")

for line in f.readlines():

ip_address = line.strip()

ssh_client = paramiko.SSHClient()

ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())

ssh_client.connect(hostname=ip_address,username=username,password=password,allow_agent=False,look_for_keys=False)

print ("成功连接到", ip_address)

command = ssh_client.invoke_shell()

command.send("configure terminal\n")

command.send("ip ftp username python\n")

command.send("ip ftp password 123\n")

command.send("file prompt quiet\n")

command.send("end\n")

command.send("copy running-config ftp://192.168.0.129\n")

time.sleep(5)

output = command.recv(65535)

print (output.decode('ascii'))

f.close()

ssh_client.close

验证:

运行脚本完毕后,回到/home/python,ls 查看running config是否备份到文件夹下

用cat 打开其中任意一个config文件,验证其内容。

 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

你可知这世上再难遇我

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值