Repository navigation
Expand file tree
/
Copy pathcfgsave.py
More file actions
71 lines (64 loc) · 2.63 KB
/
Copy pathcfgsave.py
File metadata and controls
71 lines (64 loc) · 2.63 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# This script is provided with no support or guarantees.
#
# Purpose of this script is to save configuration to remote FTP server
# File is automatically generate in form of sysname_timestamp.cfg
#
# Usage:
# ftpsave.py -s <serverIP> -u <FTPusername> -p <FTPpassword> -d (to delete file after transfer)
# If you run script without arguments only local file will be created.
#
# You may schedule automatic configuration save by leveraging Comware scheduler:
# [Switch1]scheduler job ftp_archive
# [Switch1-job-ftp_archive]command 0 python cfgsave.py -s 172.16.1.3 -u ftp -p ftp -d
# [Switch1-job-ftp_archive]quit
# [Switch1]scheduler schedule autobackup
# [Switch1-schedule-autobackup]user-role network-admin
# [Switch1-schedule-autobackup]job ftp_archive
# [Switch1-schedule-autobackup]time repeating interval 60
# [Switch1-schedule-autobackup]quit
# [Switch1]
#
# Also you may use EAA to automatically push configuratio to FTP server
# every time administrator enter save command:
# [Switch1]rtm cli-policy ftp_backup
# [Switch1-rtm-ftp_backup]event cli async mode execute pattern save*
# [Switch1-rtm-ftp_backup]action 0 cli python cfgsave.py -s 172.16.1.3 -u ftp –p ftp -d
# [Switch1-rtm-ftp_backup]user-role network-admin
# [Switch1-rtm-ftp_backup]commit
# [Switch1-rtm-ftp_backup]
import comware, sys, getopt, os, datetime, socket
def main(argv):
ip = ''
username = ''
password = ''
delete = False
try:
opts, args = getopt.getopt(argv,"hds:p:u:",["server=","username=","password="])
except getopt.GetoptError:
print 'usage ftpsave.py -s <serverIP> -u <FTPusername> -p <FTPpassword> -d (to delete file after transfer)'
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
print 'usage ftpsave.py -s <serverIP> -u <FTPusername> -p <FTPpassword> -d (to delete file after transfer)'
sys.exit()
elif opt in ("-s", "--server"):
ip = arg
elif opt in ("-u", "--username"):
username = arg
elif opt in ("-p", "--password"):
password = arg
elif opt in ("-d", "--delete"):
delete = True
timestamp = datetime.datetime.now().strftime("%y%m%d%H%M")
sysname = socket.gethostname()
filename = sysname + '_' + timestamp + '.cfg'
filenamemdb = sysname + '_' + timestamp + '.mdb'
comware.CLI('save ' + filename)
if ip != '':
path = 'ftp://' + username + ':' + password + '@' + ip + '/'
comware.CLI('copy ' + filename + ' ' + path)
if delete:
comware.CLI('delete ' + filename)
comware.CLI('delete ' + filenamemdb)
if __name__ == "__main__":
main(sys.argv[1:])