-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathimportConfig.py
More file actions
executable file
·131 lines (114 loc) · 4.21 KB
/
importConfig.py
File metadata and controls
executable file
·131 lines (114 loc) · 4.21 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#! /usr/bin/env python3
#################################################################################
#
# File: importConfig.py
# Date: June 17, 2016
# Author: Fred Mota (fmota@ixiacom.com)
#
# History:
# February 8, 2019:
# - Updated copyright note.
# - Use the ksvisionlib library.
#
# July 13, 2022:
# - Change the script to Python 3.
#
# Description:
# This script will import a configuration (.ata) file to an NTO or an GSC
# device.
# The script will import the same configuration file simultaneously to multiple
# hosts by creating one thread per host.
#
# COPYRIGHT 2016-2019 Keysight Technologies.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights to
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
# of the Software, and to permit persons to whom the Software is furnished to do
# so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#
################################################################################
import sys
import getopt
import threading
from ksvisionlib import *
def importConfig(host_ip, port, username, password, config_file):
nto = VisionWebApi(host=host_ip, username=username, password=password, port=port)
nto.importConfig({'import_type': 'FULL_IMPORT_FROM_BACKUP', 'file_name': config_file})
argv = sys.argv[1:]
username = ''
password = ''
host = ''
hosts_file = ''
config_file = ''
port = 8000
try:
opts, args = getopt.getopt(argv,"u:p:h:f:r:c:", ["username=", "password=", "host=", "hosts_file=", "port=", "config="])
except getopt.GetoptError:
print ('importConfig.py -u <username> -p <password> -c <config_file> [-h <host> | -f <host_file>] [-r port]')
sys.exit(2)
for opt, arg in opts:
if opt in ("-u", "--username"):
username = arg
elif opt in ("-p", "--password"):
password = arg
elif opt in ("-h", "--host"):
host = arg
elif opt in ("-f", "--hosts_file"):
hosts_file = arg
elif opt in ("-r", "--port"):
port = arg
elif opt in ("-c", "--config"):
config_file = arg
if username == '':
print ('importConfig.py -u <username> -p <password> -c <config_file> [-h <host> | -f <host_file>] [-r port]')
sys.exit(2)
if password == '':
print ('importConfig.py -u <username> -p <password> -c <config_file> [-h <host> | -f <host_file>] [-r port]')
sys.exit(2)
if (host == '') and (hosts_file == ''):
print ('importConfig.py -u <username> -p <password> -c <config_file> [-h <host> | -f <host_file>] [-r port]')
sys.exit(2)
if config_file == '':
print ('importConfig.py -u <username> -p <password> -c <config_file> [-h <host> | -f <host_file>] [-r port]')
sys.exit(2)
hosts_list = []
if (hosts_file != ''):
f = open(hosts_file, 'r')
for line in f:
line = line.strip()
if (line != '') and (line[0] != '#'):
hosts_list.append(line.split(' '))
f.close()
else:
hosts_list.append([host, host])
threads_list = []
for host in hosts_list:
host_ip = host[0]
thread = threading.Thread(name=host, target=importConfig, args=(host_ip, port, username, password, config_file))
threads_list.append(thread)
for thread in threads_list:
thread.daemon = True
thread.start()
try:
while threading.active_count() > 1:
for thread in threads_list:
thread.join(1)
sys.stdout.write('.')
sys.stdout.flush()
except KeyboardInterrupt:
print ("Ctrl-c received! Sending kill to threads...")
sys.exit()
print ("")