forked from scottharney/python-mdszoning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdozones.py
executable file
·126 lines (110 loc) · 3.91 KB
/
dozones.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
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
#!/usr/bin/python
# dozones.py - Scott Harney <[email protected]>
#
# from generated zoning list, create zones on Cisco MDS switches
# requires netmiko from https://github.com/ktbyers/netmiko/tree/master/netmiko
import sys
import argparse
import os
import getpass
import time
sys.path.append("./library")
from na_funcs import *
from cisco_funcs import *
debug = False
# parse command line arguments and optional environment variables
arguments = argparse.ArgumentParser(
description='push zoning configuration to MDS switch from generated command file. Zoneset name and VSAN are derived form the input file.')
arguments.add_argument(
'--hostname', required=True, type=str,
help='MDS switch fqdn or IP.')
arguments.add_argument(
'--filename', required=True, type=str,
help='Generated file with zoning commands to push to the mds switch.')
arguments.add_argument(
'--username', required=False, type=str,
help='optional username to ssh into mds switch. Alternate: set environment variable MDS_USERNAME. If neither exists, defaults to current OS username')
arguments.add_argument(
'--password', required=False, type=str,
help='optional password to ssh into mds switch. Alternate: set environment variable MDS_PASSWORD. If unset use_keys defaults to True.')
arguments.add_argument(
'--use_keys', action='store_true',
help='use ssh keys to log into switch')
arguments.add_argument(
'--dry_run', default=False, action='store_true',
help='don\'t do anything. just print some debug output')
arguments.add_argument(
'--key_file', required=False, type=str,
help='filename for ssh key file')
arguments.add_argument(
'--activate_zoneset', action='store_true',
help='add the \'zoneset activate\' command to activate the updated zoneset')
args = arguments.parse_args()
if args.password :
use_keys = False
password = args.password
elif os.getenv('MDS_PASSWORD') :
use_keys = False
password = os.getenv('MDS_PASSWORD')
else :
use_keys = True
password = ''
if args.username :
username = args.username
elif os.getenv('MDS_USERNAME') :
username = os.getenv('MDS_USERNAME')
else:
username = getpass.getuser()
if args.activate_zoneset :
activate = True
else :
activate = False
mds = args.hostname
zone_commands_filename = args.filename
keyfile = args.key_file
dry_run = args.dry_run
if not has_netmiko :
print "netmiko is required to use this script, download installation from:"
print "https://github.com/ktbyers/netmiko/tree/master/netmiko"
exit(1)
# call nonblank_lines to clean up input and load command set into list variable.
commands = []
with open(zone_commands_filename) as f_in:
for line in nonblank_lines(f_in) :
if "zoneset name ZS_" in line : # populating zoneset & vsan based on simple pattern matching. this is a hack.
zoneset_line = line.split()
zoneset = zoneset_line[2]
vsan = zoneset_line[4]
else:
commands.append(line)
# connect to mds
mds = {
'device_type': 'cisco_nxos',
'ip': mds,
'verbose': False,
'username': username,
'password': password,
'use_keys': use_keys
}
net_connect = ConnectHandler(**mds)
if dry_run :
output = net_connect.find_prompt()
print "DRY RUN: prompt = %s" % output
#uncomment lines below to actually do this
if not dry_run :
output = net_connect.send_config_set(commands)
print output
else :
print "DRY RUN: command list = %r" % commands
zoneset_command = "zoneset activate name %s vsan %s\n" % (zoneset,vsan)
if activate and not dry_run :
net_connect.config_mode()
output = handle_mds_continue(net_connect, cmd=zoneset_command)
print output
net_connect.exit_config_mode()
net_connect.send_command('copy run start')
elif dry_run and not activate :
print "DRY RUN: no activate command"
else :
print "DRY RUN: activate command = %r" % zoneset_command
net_connect.disconnect()