python 备份交换机_python3备份juniper交换机

这是一个Python3脚本,用于自动备份Juniper和SSG交换机的配置。脚本通过telnet连接到设备,读取配置信息,并将其保存到指定目录。同时,脚本还会将备份结果通过电子邮件发送给指定收件人。
摘要由CSDN通过智能技术生成

#!/usr/bin/env python3#-*- coding: utf-8 -*-

"""Author: Linxy -- <592901071@qq.com>

Purpose: Juniper备份脚本

Created: 2017-6-23"""

importdatetimeimportsysimportosimporttelnetlibfrom email importencodersfrom email.header importHeaderfrom email.mime.text importMIMETextfrom email.utils importparseaddr, formataddrimportsmtplib

su=[]

fa=[]#获取年月日的str数据

defgetymd (ymd):

d=datetime.datetime.now()if ymd=='y':returnstr(d.year)elif ymd=='m':returnstr(d.month)elif ymd=='d':returnstr(d.day)#p1='./switch/'+getymd('y')+'/'+getymd('m')+'/'+getymd('d')

l1=[getymd('y'),getymd('m'),getymd('d')]#print(l1)

p1='./switch'

ifos.path.isdir(p1):pass

else:

os.mkdir(p1)for i inl1:

p1=p1+'/'+iifos.path.isdir(p1):pass

else:

os.mkdir(p1)

filename1='./sw.txt'#交换机IP地址列表存放位置 注意要把最后一个IP以后的空格和回车全部删除

filename2='./ssg.txt'

def fc_srx(p2): #文件处理部分的函数

if os.path.getsize(p2)==0:'''密码不对时候 telnet的执行是成功的只是read_all的时候没有结尾的标记所以无法读出返回值

但仍然会建立空txt文档所以需要在此再进行一次判断并把需要的值写入fa[]中'''fa.append(host+'\n')print(host+"is failed")else:

with open(p2,'r') as f:

lines=f.readlines()

with open(p2,'w') as w:for I inlines:

I=I.replace('---(more)---','')

I=I.replace(' ','')

I=I.replace('\r','')

I=I.replace('\n','')if I=='':pass

else:

w.write(I+'\r\n')deffc_ssg(p2):if os.path.getsize(p2)==0:#print(host+' is failed')

'''密码不对时候 telnet的执行是成功的只是read_all的时候没有结尾的标记所以无法读出返回值

但仍然会建立空txt文档所以需要在此再进行一次判断并把需要的值写入fa[]中'''fa.append(host+'\n')print(host+"is failed")else:

with open(p2,'r') as f:

lines=f.readlines()

with open(p2,'w') as w:for I inlines:

I=I.replace('--- more ---','')

I=I.replace(' ','')

I=I.replace('\r','')

I=I.replace('\n','')if I=='':pass

else:

w.write(I+'\r\n')def juniper_bak (host):#备份juniper函数

user='这里是交换机帐号'password='这里是交换机密码'

print ('Backing up:'+host)

tn=telnetlib.Telnet(host.encode('utf-8'),port=23,timeout=4)

tn.read_until(b"login:")

tn.write(user.encode('utf-8')+'\n'.encode('utf-8'))

tn.read_until(b"Password:")

tn.write(password.encode('utf-8')+'\n'.encode('utf-8'))

tn.read_until(b'>')

tn.write(b'show configuration | display set'+'\n'.encode('utf-8'))for i in range(300) :

tn.write(b' ')

tn.write(b'q\n')

tn.write(b'q\n')

p2=p1+'/'+host+'/'+host+'.txt'

if os.path.isdir(p1+'/'+host):pass

else:

os.mkdir(p1+'/'+host)

with open(p2,'w') as f:

f.write(tn.read_all().decode())

tn.close()

fc_srx(p2)def ssg_bak (host):#备份ssg函数

user='这里是ssg设备帐号'password='这里是ssg设备密码'

print ('Backing up:'+host)

tn=telnetlib.Telnet(host.encode('utf-8'),port=23,timeout=4)

tn.read_until(b"login:")

tn.write(user.encode('utf-8')+'\n'.encode('utf-8'))

tn.read_until(b"password:")

tn.write(password.encode('utf-8')+'\n'.encode('utf-8'))

tn.read_until(b'>')

tn.write(b'get config'+'\n'.encode('utf-8'))for i in range(50000) :#看具体设备有些设备输入200个空格即可获取到全部回显

tn.write(b' ')

tn.write(b'exit\n')

p2=p1+'/'+host+'/'+host+'.txt'

if os.path.isdir(p1+'/'+host):pass

else:

os.mkdir(p1+'/'+host)

with open(p2,'w') as f:

f.write(tn.read_all().decode(encoding='gbk'))

tn.close()

fc_ssg(p2)def sendmail_cxr(tx):#发送邮件函数

def _format_addr(s): #注意此函数实际传入的S格式为‘字符串 ’

name, addr =parseaddr(s)return formataddr((Header(name, 'utf-8').encode(), addr))

from_addr= '这里是发送邮件的邮箱地址'password= '这里是邮箱密码'to_addr= '接收邮件的地址1'to_addr2='接收邮件的地址2'smtp_server= 'SMT服务的域名要写这里'msg= MIMEText(tx, 'plain', 'utf-8')

msg['From'] = _format_addr('邮件相关的格式不要细究 ' %from_addr)

msg['To'] = _format_addr('邮件相关的格式不要细究 '%to_addr)

msg['Subject'] = Header('今日备份情况', 'utf-8').encode() #邮件主题

server= smtplib.SMTP(smtp_server, 25)#server.set_debuglevel(1)

server.login(from_addr, password)

server.sendmail(from_addr, [to_addr,to_addr2], msg.as_string())

server.quit()if __name__=='__main__':

with open (filename2,'r')as fo:for line info.readlines():if line.split(' ')[0]!='':

line=line.replace('\n','')

host=line.split(' ')[0]try:

ssg_bak(host)

su.append(host+'\n')except:print(host+"is failed")

fa.append(host+'\n')

with open (filename1,'r')as fo:for line info.readlines():if line.split(' ')[0]!='':

line=line.replace('\n','')

host=line.split(' ')[0]try:

juniper_bak(host)

su.append(host+'\n')except:print(host+"is failed")

fa.append(host+'\n')'''密码不对时候 telnet的执行是成功的只是read_all的时候没有结尾的标记所以无法读出返回值

但仍然会建立空txt文档所以需要在文本处理环节再进行一次判断并把需要的值写入fa[]中'''

#print(su)

#print(fa)

tx=''

if len(su)!=0:

txsu='备份成功\n'+''.join(su)

csu=0for i insu:

csu=csu+1tx=tx+txsu+'总计成功'+str(csu)+'个'+'\n\n'

if len(fa)!=0:

cfa=0for i infa:

cfa=cfa+1txfa='备份失败\n'+''.join(fa)

tx=tx+txfa+'总计失败'+str(cfa)+'个'+'\n\n'sendmail_cxr(tx)

描述了juniper的基本配置方法。 1 系统配置...................................................................................................................2 1.1 系统信息基本配置..............................................................................................2 1.2 系统用户信息.....................................................................................................2 1.3 系统服务配置.....................................................................................................3 2 端口配置...................................................................................................................4 2.1 juniper 端口介绍.................................................................................................4 2.2 端口配置............................................................................................................4 3 SNMP配置...............................................................................................................6 4 Routing-options配置..................................................................................................7 4.1 静态路由配置.....................................................................................................7 4.2 route-id&as.........................................................................................................7 4.3 bgp聚合配置.....................................................................................................7 5 Route protocal配置....................................................................................................8 5.1 Ospf配置...........................................................................................................8 5.2 Bgp 配置...........................................................................................................9 6 Policy 配置...............................................................................................................9 7 Firewall 配置...................................................
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值