-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackup_switch.py
More file actions
53 lines (39 loc) · 1.29 KB
/
Copy pathbackup_switch.py
File metadata and controls
53 lines (39 loc) · 1.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/usr/bin/python
import pexpect
import sys
import time
from datetime import datetime
logfilepath = ('/var/log/backup_switch.log')
now = datetime.now()
dateformat = "%Y%m%d%H%M"
dataname = now.strftime(dateformat)
ip=sys.argv[1]
user=sys.argv[2]
password=sys.argv[3]
switchname=sys.argv[4]
tftp_server=sys.argv[5]
filename = (switchname + "_" + datetime.now().strftime(dateformat))
child = pexpect.spawn("ssh "+user+"@"+ip)
child.expect ("Password:")
child.sendline (password)
try:
child.expect(b"#")
child.sendline ("copy running-config tftp:")
child.sendline (tftp_server)
child.sendline (filename)
if child.expect(b"#") == 0:
mensagem = (datetime.now().strftime(dateformat) + ": OK Backup " +filename+ " realizado com sucesso\n")
logfile = open (logfilepath, 'a')
logfile.write(mensagem)
logfile.close()
else:
mensagem = (datetime.now().strftime(dateformat) + ': ERROR Backup nao realizado.\n')
logfile = open (logfilepath, 'a')
logfile.write(mensagem)
logfile.close()
except:
mensagem = (datetime.now().strftime(dateformat) + ': ERROR Backup nao realizado.\n')
logfile = open (logfilepath, 'a')
logfile.write(mensagem)
logfile.close()
child.close()