Python3 Backup Juniper Switch

Source: Internet
Author: User

#!/usr/bin/env Python3
#-*-Coding:utf-8-*-
"""
Author:linxy--<[email protected]>
Purpose:juniper Backup Scripts
Created:2017-6-23
"""

Import datetime
Import Sys
Import OS
Import Telnetlib
From email import encoders
From Email.header Import Header
From Email.mime.text import Mimetext
From email.utils import parseaddr, formataddr
Import Smtplib
Su=[]
Fa=[]


#获取年月日的str数据
def getymd (YMD):
D=datetime.datetime.now ()
If ymd== ' y ':
Return str (d.year)
Elif ymd== ' m ':
Return str (d.month)
Elif ymd== ' d ':
Return str (d.day)

#p1 = './switch/' +getymd (' y ') + '/' +getymd (' m ') + '/' +getymd (' d ')
L1=[getymd (' y '), Getymd (' m '), Getymd (' d ')]
#print (L1)
P1= './switch '
If Os.path.isdir (p1):
Pass
Else
Os.mkdir (p1)
For I in L1:
p1=p1+ '/' +i
If Os.path.isdir (p1):
Pass
Else
Os.mkdir (p1)


Filename1= './sw.txt ' #交换机IP地址列表存放位置 Note to remove all subsequent spaces and carriage returns from the last IP
Filename2= './ssg.txt '
def FC_SRX (p2): #文件处理部分的函数
If Os.path.getsize (p2) ==0:

‘‘‘
When the password is incorrect, the execution of Telnet is successful, but the return value cannot be read when the Read_all is not marked with an end.
However, an empty TXT document will still be created, so you need to make another judgment here and write the required values to 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 in lines:
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 FC_SSG (p2):
If Os.path.getsize (p2) ==0:
#print (host+ ' is failed ')
‘‘‘
When the password is incorrect, the execution of Telnet is successful, but the return value cannot be read when the Read_all is not marked with an end.
However, an empty TXT document will still be created, so you need to make another judgment here and write the required values to 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 in lines:
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= ' Here is the switch account number '
Password= ' Here is the switch 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= ' Here is the SSG device account number '
Password= ' Here is the SSG device 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 ' 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格式为 ' string < email address > '
Name, addr = parseaddr (s)
Return Formataddr (Header (name, ' Utf-8 '). Encode (), addr))
From_addr = ' Here is the email address of the email '
Password = ' Here is the email password '
TO_ADDR = ' Receive mail address 1 '
To_addr2= ' receive mail address 2 '
Smtp_server = ' SMT service domain name to be written here '

msg = Mimetext (TX, ' plain ', ' utf-8 ')

msg[' from ' = _format_addr (' Message related formats do not scrutiny <%s> '% from_addr)
msg[' to ' = _format_addr (' Message related formats do not scrutiny <%s> '% to_addr)
msg[' Subject ' = Header (' Backup status Today ', ' 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 in Fo.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 in Fo.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 ')
‘‘‘
When the password is incorrect, the execution of Telnet is successful, but the return value cannot be read when the Read_all is not marked with an end.
However, an empty TXT document will still be created, so you need to make a second judgment in the text processing and write the required values to fa[]
‘‘‘
#print (SU)
#print (FA)
Tx= "
If Len (su)!=0:
txsu= ' backup succeeded \ n ' + '. Join (SU)
Csu=0
For i in SU:
Csu=csu+1
tx=tx+txsu+ ' Total Success ' +str (CSU) + ' + ' \ n '
If Len (FA)!=0:
Cfa=0
For I in FA:
Cfa=cfa+1
Txfa= ' backup failed \ n ' + '. Join (FA)
Tx=tx+txfa+ ' Total Failed ' +str (CFA) + ' + ' \ n '
SENDMAIL_CXR (TX)







This article comes from the blog title cannot be empty, so be sure to keep this source http://4830403.blog.51cto.com/4820403/1948331

Python3 Backup Juniper Switch

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.