-
Notifications
You must be signed in to change notification settings - Fork 2
/
run_command.py
executable file
·85 lines (70 loc) · 2.77 KB
/
run_command.py
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
#!/usr/bin/python3
import pyeapi
import argparse
import ssl
import ipaddress
try:
_create_unverified_https_context = ssl._create_unverified_context
except AttributeError:
# Legacy Python that doesn't verify HTTPS certificates by default
pass
else:
# Handle target environment that doesn't support HTTPS verification
ssl._create_default_https_context = _create_unverified_https_context
parser = argparse.ArgumentParser()
parser.add_argument('--conf', required=False, action='append',
default=[], help='Config to apply to all switches')
parser.add_argument('--interface', required=False,
default='', help='Interface to configure')
parser.add_argument('--addr', required=False,
default='', help='Address range to use')
parser.add_argument('--config_file', required=False,
default='', help='File with config in to apply')
parser.add_argument('--remove', required=False, action='store_true',
default='', help='If used will remove the config in the specified file by adding "no" to each line of config')
args = parser.parse_args()
conf = args.conf
interface = args.interface
addr = args.addr
config_file = args.config_file
remove = args.remove
if addr:
network_range = ipaddress.ip_network(addr)
# from the given IP subnet, build a list of available /32 subnets
available_addr = list(ipaddress.ip_network(network_range).subnets(prefixlen_diff=(32-network_range.prefixlen)))
# Find the home directory where the .eap.conf file is located
from os.path import expanduser
home = expanduser("~")
hosts = []
n = 1
config_list = []
# read in the contents of the eapi.conf file and build a list of all the hostnames in a list called 'hosts'
with open(home + "/.eapi.conf", "r") as file_object:
line = file_object.readline()
while line:
if "connection" in line:
hostname = line.lstrip('[connection:')
hostname = hostname.rstrip(']\n\r')
hosts.append(hostname)
line = file_object.readline()
else:
line = file_object.readline()
if config_file:
with open(config_file, 'r') as config_file_object:
line = config_file_object.readline()
while line:
if remove:
config_list.append("no " + line)
line = config_file_object.readline()
else:
config_list.append(line)
line = config_file_object.readline()
for x in hosts:
switch = pyeapi.connect_to(x)
if conf:
command = switch.config(conf)
elif interface:
command = switch.config(["interface " + interface, "ip address " + str(available_addr[n])])
n += 1
elif config_file:
command = switch.config(config_list)