Advertisement
Guest User

Untitled

a guest
Jul 24th, 2017
440
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.92 KB | None | 0 0
  1. #!/usr/bin/env python
  2. #! -*- coding: utf-8 -*-
  3.  
  4. import argparse
  5. from collections import OrderedDict
  6. from datetime import datetime
  7. import os
  8. import paramiko
  9. import re
  10.  
  11. def connect(host, port, log, pas):
  12.     s = paramiko.SSHClient()
  13.     s.load_system_host_keys()
  14.     s.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  15.     s.connect(host, port, log, pas)
  16.     return s
  17.  
  18. def findHosts(host, port, log, pas):
  19.     fabHosts = []
  20.     s = connect(host, port, log, pas)
  21.     (stdin, stdout, stderr) = s.exec_command('alishow ESXi*')
  22.     for line in stdout.readlines():
  23.         alias = re.findall('alias:\t(.+)\t\n', line)
  24.         if alias:
  25.             fabHosts.append(alias[0])
  26.     s.close()
  27.     return fabHosts
  28.    
  29. def findCommon(src):
  30.     src.sort(key=len, reverse=True)
  31.  
  32.     src1 = {}
  33.     for i, s in enumerate(src):
  34.         for j in range(0, len(s)):
  35.             sn = s if not j else s[:-j]
  36.             for k, s2 in enumerate(src[i+1:]):
  37.                 for l in range(0, len(s)):
  38.                     sn1 = s2 if not l else s2[:-l]
  39.                     if sn == sn1:
  40.                         if sn not in src1:
  41.                             src1[sn] = set()
  42.                             src1[sn].add(s)
  43.                         src1[sn].add(s2)
  44.                         found = True
  45.  
  46.     src2 = OrderedDict(sorted(src1.items(), key=lambda t: len(t[0]), reverse=True))
  47.  
  48.     src2_keys = src2.keys()
  49.     for i, k in enumerate(src2_keys):
  50.         if not len(src2[k]):
  51.             src2.pop(k)
  52.         if i == len(src2_keys):
  53.             break
  54.         if k in src2:
  55.             for value in src2[k]:
  56.                 is_break = False
  57.                 for j, k2 in enumerate(src2_keys[i+1:]):
  58.                     if value in src2[k2]:
  59.                         if len(src2[k]) == 1:
  60.                             src2.pop(k)
  61.                             is_break = True
  62.                             break
  63.                         else:
  64.                             src2[k2].remove(value)
  65.                 if is_break:
  66.                     break
  67.  
  68.     src2_keys = src2.keys()
  69.     exception = ['_initiator', '_p', '_CL']
  70.     combined = "(" + ")|(".join(exception) + ")"
  71.     for i, k in enumerate(src2_keys):
  72.         result = re.sub(combined, '', k)
  73.         result = re.sub('_$', '', result)
  74.         src2_keys[i] = result
  75.        
  76.     return src2_keys
  77.  
  78. def findArrays(host, port, log, pas):
  79.     fabArrays = {}
  80.     aliases = []
  81.     exception = ['ESXi*', 'CentOS_TSM_*', 'StdNode1_p*', 'TL_3500_*', 'TRK_MIR_*', 'Zabbix_*']
  82.     combined = "(" + ")|(".join(exception) + ")"
  83.     s = connect(host, port, log, pas)
  84.     (stdin, stdout, stderr) = s.exec_command('alishow *')
  85.     for line in stdout.readlines():
  86.         alias = re.findall('alias:\t(.+)\t\n', line)
  87.         if alias:
  88.             if not re.match(combined, alias[0]):
  89.                 aliases.append(alias[0])
  90.     arrays = findCommon(aliases)
  91.     for array in arrays:
  92.         r = re.compile(array + '_.*')
  93.         arrayAliases = filter(r.match, aliases)
  94.         fabArrays.update({array:arrayAliases})
  95.    
  96.     s.close()
  97.     return fabArrays
  98.  
  99. m = argparse.ArgumentParser(description='Генератор конфигурации зон SAN коммутаторов Brocade в Onlanta Oncloud')
  100. m.add_argument('-type', nargs=1, type=str, help='''Тип добавляемого устройства''')
  101. m.add_argument('-d', nargs=1, type=str, help='''Имя устройства''')
  102. m.add_argument('-a1', nargs=1, type=str, help='''Alias 1''')
  103. m.add_argument('-w1', nargs=1, type=str, help='''WWN 1''')
  104. m.add_argument('-a2', nargs=1, type=str, help='''Alias 2''')
  105. m.add_argument('-w2', nargs=1, type=str, help='''WWN 2''')
  106. m.add_argument('-f', nargs=1, type=int, help='''Номер фабрики''')
  107. m.add_argument('-c', nargs=1, type=str, help='''Имя конфигурации''')
  108. options = m.parse_args()
  109.  
  110. if options.f[0] == 1:
  111.     host = 'IP'
  112.     login = 'admin'
  113.     password = 'pass'
  114. if options.f[0] == 2:
  115.     host = 'IP'
  116.     login = 'admin'
  117.     password = 'pass'
  118.  
  119. print 'Generating config start.'
  120. if options.type[0] == 'storage':
  121.     fabric = findHosts(host, 22, login, password)
  122.  
  123. if options.type[0] == 'server':
  124.     fabric = findArrays(host, 22, login, password)
  125.        
  126. filename = 'config_' + datetime.strftime(datetime.now(), '%Y%m%d%H%M%S') + '.txt'
  127. f = open(filename, 'w')
  128.  
  129. s = 'alicreate "' + options.a1[0] + '", "' + options.w1[0] + '"'
  130. f.write(s + '\n')
  131.  
  132. s = 'alicreate "' + options.a2[0] + '", "' + options.w2[0] + '"'
  133. f.write(s + '\n')
  134.  
  135. if options.type[0] == 'storage':
  136.     for host in fabric:
  137.         s = 'zonecreate "'+ host +'_'+ options.d[0] +'", "'+ host +';' + options.a1[0] + ';' + options.a2[0] + '"'
  138.         f.write(s + '\n')
  139.  
  140.     s = 'cfgadd "'+ options.c[0] +'", "'
  141.     for host in fabric:
  142.         s += host +'_'+ options.d[0] + ';'
  143.     s += '"'
  144.     f.write(s + '\n')
  145.    
  146. if options.type[0] == 'server':
  147.     for key, value in fabric.items():
  148.         s = 'zonecreate "'+ options.a1[0] +'_'+ key +'", "' + options.a1[0] + ';'
  149.         for alias in value:
  150.             s += alias + ';'
  151.         s += '"'
  152.         f.write(s + '\n')
  153.        
  154.         s = 'zonecreate "'+ options.a2[0] +'_'+ key +'", "' + options.a2[0] + ';'
  155.         for alias in value:
  156.             s += alias + ';'
  157.         s += '"'
  158.         f.write(s + '\n')
  159.    
  160.     s = 'cfgadd "'+ options.c[0] +'", "'
  161.     for key, value in fabric.items():
  162.         s += options.a1[0] +'_'+ key + ';'
  163.         s += options.a2[0] +'_'+ key + ';'
  164.     s += '"'
  165.     f.write(s + '\n')
  166.    
  167. s = 'cfgsave'
  168. f.write(s + '\n')
  169. s = 'cfgenable "'+ options.c[0] +'"'
  170. f.write(s + '\n')
  171.  
  172. print 'Generating config done.'
  173. print 'Config file name: ' + filename;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement