Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- #! -*- coding: utf-8 -*-
- import argparse
- from collections import OrderedDict
- from datetime import datetime
- import os
- import paramiko
- import re
- def connect(host, port, log, pas):
- s = paramiko.SSHClient()
- s.load_system_host_keys()
- s.set_missing_host_key_policy(paramiko.AutoAddPolicy())
- s.connect(host, port, log, pas)
- return s
- def findHosts(host, port, log, pas):
- fabHosts = []
- s = connect(host, port, log, pas)
- (stdin, stdout, stderr) = s.exec_command('alishow ESXi*')
- for line in stdout.readlines():
- alias = re.findall('alias:\t(.+)\t\n', line)
- if alias:
- fabHosts.append(alias[0])
- s.close()
- return fabHosts
- def findCommon(src):
- src.sort(key=len, reverse=True)
- src1 = {}
- for i, s in enumerate(src):
- for j in range(0, len(s)):
- sn = s if not j else s[:-j]
- for k, s2 in enumerate(src[i+1:]):
- for l in range(0, len(s)):
- sn1 = s2 if not l else s2[:-l]
- if sn == sn1:
- if sn not in src1:
- src1[sn] = set()
- src1[sn].add(s)
- src1[sn].add(s2)
- found = True
- src2 = OrderedDict(sorted(src1.items(), key=lambda t: len(t[0]), reverse=True))
- src2_keys = src2.keys()
- for i, k in enumerate(src2_keys):
- if not len(src2[k]):
- src2.pop(k)
- if i == len(src2_keys):
- break
- if k in src2:
- for value in src2[k]:
- is_break = False
- for j, k2 in enumerate(src2_keys[i+1:]):
- if value in src2[k2]:
- if len(src2[k]) == 1:
- src2.pop(k)
- is_break = True
- break
- else:
- src2[k2].remove(value)
- if is_break:
- break
- src2_keys = src2.keys()
- exception = ['_initiator', '_p', '_CL']
- combined = "(" + ")|(".join(exception) + ")"
- for i, k in enumerate(src2_keys):
- result = re.sub(combined, '', k)
- result = re.sub('_$', '', result)
- src2_keys[i] = result
- return src2_keys
- def findArrays(host, port, log, pas):
- fabArrays = {}
- aliases = []
- exception = ['ESXi*', 'CentOS_TSM_*', 'StdNode1_p*', 'TL_3500_*', 'TRK_MIR_*', 'Zabbix_*']
- combined = "(" + ")|(".join(exception) + ")"
- s = connect(host, port, log, pas)
- (stdin, stdout, stderr) = s.exec_command('alishow *')
- for line in stdout.readlines():
- alias = re.findall('alias:\t(.+)\t\n', line)
- if alias:
- if not re.match(combined, alias[0]):
- aliases.append(alias[0])
- arrays = findCommon(aliases)
- for array in arrays:
- r = re.compile(array + '_.*')
- arrayAliases = filter(r.match, aliases)
- fabArrays.update({array:arrayAliases})
- s.close()
- return fabArrays
- m = argparse.ArgumentParser(description='Генератор конфигурации зон SAN коммутаторов Brocade в Onlanta Oncloud')
- m.add_argument('-type', nargs=1, type=str, help='''Тип добавляемого устройства''')
- m.add_argument('-d', nargs=1, type=str, help='''Имя устройства''')
- m.add_argument('-a1', nargs=1, type=str, help='''Alias 1''')
- m.add_argument('-w1', nargs=1, type=str, help='''WWN 1''')
- m.add_argument('-a2', nargs=1, type=str, help='''Alias 2''')
- m.add_argument('-w2', nargs=1, type=str, help='''WWN 2''')
- m.add_argument('-f', nargs=1, type=int, help='''Номер фабрики''')
- m.add_argument('-c', nargs=1, type=str, help='''Имя конфигурации''')
- options = m.parse_args()
- if options.f[0] == 1:
- host = 'IP'
- login = 'admin'
- password = 'pass'
- if options.f[0] == 2:
- host = 'IP'
- login = 'admin'
- password = 'pass'
- print 'Generating config start.'
- if options.type[0] == 'storage':
- fabric = findHosts(host, 22, login, password)
- if options.type[0] == 'server':
- fabric = findArrays(host, 22, login, password)
- filename = 'config_' + datetime.strftime(datetime.now(), '%Y%m%d%H%M%S') + '.txt'
- f = open(filename, 'w')
- s = 'alicreate "' + options.a1[0] + '", "' + options.w1[0] + '"'
- f.write(s + '\n')
- s = 'alicreate "' + options.a2[0] + '", "' + options.w2[0] + '"'
- f.write(s + '\n')
- if options.type[0] == 'storage':
- for host in fabric:
- s = 'zonecreate "'+ host +'_'+ options.d[0] +'", "'+ host +';' + options.a1[0] + ';' + options.a2[0] + '"'
- f.write(s + '\n')
- s = 'cfgadd "'+ options.c[0] +'", "'
- for host in fabric:
- s += host +'_'+ options.d[0] + ';'
- s += '"'
- f.write(s + '\n')
- if options.type[0] == 'server':
- for key, value in fabric.items():
- s = 'zonecreate "'+ options.a1[0] +'_'+ key +'", "' + options.a1[0] + ';'
- for alias in value:
- s += alias + ';'
- s += '"'
- f.write(s + '\n')
- s = 'zonecreate "'+ options.a2[0] +'_'+ key +'", "' + options.a2[0] + ';'
- for alias in value:
- s += alias + ';'
- s += '"'
- f.write(s + '\n')
- s = 'cfgadd "'+ options.c[0] +'", "'
- for key, value in fabric.items():
- s += options.a1[0] +'_'+ key + ';'
- s += options.a2[0] +'_'+ key + ';'
- s += '"'
- f.write(s + '\n')
- s = 'cfgsave'
- f.write(s + '\n')
- s = 'cfgenable "'+ options.c[0] +'"'
- f.write(s + '\n')
- print 'Generating config done.'
- print 'Config file name: ' + filename;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement