python备份网络设备的配置文件(交换机需要开启ftp server)

话不多说,上代码
bj_iplist.txt

192.168.1.1
192.168.1.3

wq_iplist.txt

172.20.254.1
172.20.254.2
172.20.254.4
172.20.254.5
172.20.254.6
172.20.254.7
172.20.254.8
172.20.254.11
172.20.254.254
172.20.252.252
172.20.252.253
172.19.254.1
172.19.254.2
172.19.254.3
172.19.254.4
172.19.254.5
172.19.254.6
172.19.254.7
172.19.254.8
172.19.254.9
172.19.252.2
172.19.250.250
172.19.254.254
from ftplib import FTP
import time,os
Today=time.strftime("%Y-%m-%d", time.localtime())
hostfiles=open("bj_iplist.txt","r")
Hostfiles=open("wq_iplist.txt","r")
port=21
username="wu.kangbao"
password="wu@kangbao123"
Password="Wu@kangbao123"
Pass="wU@kangbao123"
for ip in hostfiles.readlines():
	ip=ip.replace("\n","")
	print(ip)
	ftp=FTP()
	ftp.set_debuglevel(2)
	ftp.connect(ip,port)
	ftp.login(username,password)
	os.chdir("//root//network_back")
	print(ftp.getwelcome())
	bufsize=1024
	nametime=ip+"-"+Today
	if ip == "192.168.1.3":
		filename="{0}.cfg".format(nametime)
		file_handle=open(filename,"wb").write
		ftp.retrbinary("RETR BJAR2240.cfg",file_handle,bufsize)
		ftp.set_debuglevel(0)
		ftp.quit()
	else:
		filename="{0}.zip".format(nametime)
		file_handle=open(filename,"wb").write
		ftp.retrbinary("RETR vrpcfg.zip",file_handle,bufsize)
		ftp.set_debuglevel(0)
		ftp.quit()
for ip in Hostfiles.readlines():
	ip=ip.replace("\n","")
	print(ip)
	if ip =="172.20.254.1":
		ftp=FTP()
		ftp.set_debuglevel(2)
		ftp.connect(ip,port)
		ftp.login(username,Password)
		os.chdir("//root//network_back")
		print(ftp.getwelcome())
		bufsize=1024
		nametime=ip+"-"+Today
		filename="{0}.zip".format(nametime)
		file_handle=open(filename,"wb").write
		ftp.retrbinary("RETR 20180413.zip",file_handle,bufsize)
		ftp.set_debuglevel(0)
		ftp.quit()
	elif ip == "172.19.254.254" or ip=="172.19.250.250":
		ftp=FTP()
		ftp.set_debuglevel(2)
		ftp.connect(ip,port)
		ftp.login(username,Pass)
		os.chdir("//root//network_back")
		print(ftp.getwelcome())
		bufsize=1024
		nametime=ip+"-"+Today
		filename="{0}.zip".format(nametime)
		file_handle=open(filename,"wb").write
		ftp.retrbinary("RETR vrpcfg.zip",file_handle,bufsize)
		ftp.set_debuglevel(0)
		ftp.quit()
	elif ip == "172.20.252.252" or ip== "172.20.252.253" or ip=="172.19.252.2" or ip=="172.20.254.5":
		ftp=FTP()
		ftp.set_debuglevel(2)
		ftp.connect(ip,port)
		ftp.login(username,password)
		os.chdir("//root//network_back")
		print(ftp.getwelcome())
		bufsize=1024
		nametime=ip+"-"+Today
		filename="{0}.zip".format(nametime)
		file_handle=open(filename,"wb").write
		ftp.retrbinary("RETR vrpcfg.zip",file_handle,bufsize)
		ftp.set_debuglevel(0)
		ftp.quit()
	else:
		ftp=FTP()
		ftp.set_debuglevel(2)
		ftp.connect(ip,port)
		ftp.login(username,Password)
		os.chdir("//root//network_back")
		print(ftp.getwelcome())
		bufsize=1024
		nametime=ip+"-"+Today
		filename="{0}.zip".format(nametime)
		file_handle=open(filename,"wb").write
		ftp.retrbinary("RETR vrpcfg.zip",file_handle,bufsize)
		ftp.set_debuglevel(0)
		ftp.quit()

交换机配置ftp server

 ftp server enable
aaa
 local-user wu.kangbao ftp-directory flash:/
 local-user wu.kangbao service-type ftp

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值