Advertisement
Guest User

Untitled

a guest
Jul 24th, 2017
349
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.59 KB | None | 0 0
  1. #!/usr/bin/env python
  2. #! -*- coding: utf-8 -*-
  3.  
  4. import argparse
  5. import paramiko
  6. import re
  7. import sys
  8.  
  9. host = {
  10.     '10': {
  11.         'address' : 'IP',
  12.         'login' : 'admin',
  13.         'password' : 'pass',
  14.         'ports' : 12
  15.     },
  16.     '11': {
  17.         'address' : 'IP',
  18.         'login' : 'admin',
  19.         'password' : 'pass',
  20.         'ports' : 12
  21.     },
  22.     '12': {
  23.         'address' : 'IP',
  24.         'login' : 'admin',
  25.         'password' : 'pass',
  26.         'ports' : 12
  27.     },
  28.     '13': {
  29.         'address' : 'IP',
  30.         'login' : 'admin',
  31.         'password' : 'pass',
  32.         'ports' : 12
  33.     },
  34.     '18': {
  35.         'address' : 'IP',
  36.         'login' : 'admin',
  37.         'password' : 'pass',
  38.         'ports' : 12
  39.     },
  40.     '19': {
  41.         'address' : 'IP',
  42.         'login' : 'admin',
  43.         'password' : 'pass',
  44.         'ports' : 12
  45.     },
  46.     '23': {
  47.         'address' : '1IP',
  48.         'login' : 'admin',
  49.         'password' : 'pass',
  50.         'ports' : 24
  51.     },
  52.     '24': {
  53.         'address' : 'IP',
  54.         'login' : 'admin',
  55.         'password' : 'pass',
  56.         'ports' : 24
  57.     },
  58. }
  59.  
  60. def connect(host, port, log, pas):
  61.     s = paramiko.SSHClient()
  62.     s.load_system_host_keys()
  63.     s.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  64.     s.connect(host, port, log, pas)
  65.     print '*** Connected to', host
  66.     return s
  67.    
  68. def findali(s, port):
  69.     wwn = findWWN(s, port)
  70.     if wwn == 'Trunk port':
  71.         alias = 'Trunk port'
  72.     else:
  73.         alias = findAlias(s, wwn)
  74.     if alias is None:
  75.         alias = wwn
  76.     print 'port', port, ':', alias, '(', wwn, ')'
  77.    
  78. def findWWN(conn, port):
  79.     command = 'portshow ' + str(port)
  80.     (stdin, stdout, stderr) = s.exec_command(command)
  81.    
  82.     for line in stdout.readlines():
  83.         wwn = re.findall('\t(([0-9A-Fa-f]{2}:?){8})', line)
  84.         if len(wwn) > 0:
  85.             return wwn[0][0]
  86.         trunk = re.findall('Trunk (master )?port', line)
  87.         if len(trunk) > 0:
  88.             return 'Trunk port'
  89.  
  90. def findAlias(conn, wwn):
  91.     command = 'nodefind ' + str(wwn)
  92.     (stdin, stdout, stderr) = s.exec_command(command)
  93.    
  94.     for line in stdout.readlines():
  95.         aliases = re.findall('Aliases: (.+)\n', line)
  96.         if aliases:
  97.             return aliases[0]
  98.            
  99. def switchshow(conn):
  100.     command = 'switchshow'
  101.     (stdin, stdout, stderr) = s.exec_command(command)
  102.  
  103.     for line in stdout.readlines():
  104.         line = line.rstrip()
  105.         if line != '':
  106.             print line
  107.            
  108. def process():
  109.     if command == 'findali':
  110.         try:
  111.             port = options.p[0]
  112.             if port == 'all':
  113.                 for i in range(0, host[switch]['ports']):
  114.                     globals()[command](s, i)
  115.             elif ',' in port:
  116.                 for i in port:
  117.                     if i == ',':
  118.                         continue
  119.                     globals()[command](s, i)
  120.             else:
  121.                 globals()[command](s, port)
  122.         except TypeError:
  123.             print 'Port not defined.';
  124.             exit()
  125.  
  126. m = argparse.ArgumentParser(description='''Работа с SAN коммутаторами Brocade в Onlanta Oncloud''')
  127. m.add_argument('-c', nargs=1, type=str, help='''Комманда для выполнения. findali''')
  128. m.add_argument('-switchall', action='store_true', help='Выполнение на всех SAN коммутаторах.')
  129. m.add_argument('-a', nargs=1, type=str, help='Адресс SAN коммутатора (указываются последние 2 цифры IP адреса).')
  130. m.add_argument('-dc', nargs=1, type=str, help='Выбор всех коммутаторов ЦОДа (SafeData или IXCellerate).')
  131. m.add_argument('-f', nargs=1, type=str, help='Выбор всех коммутаторов фабрики (1 и 2).')
  132. m.add_argument('-st', nargs=1, type=str, help='Выбор типа коммутаторов фабрик (core и edge).')
  133. m.add_argument('-p', nargs=1, type=str, help='Порт коммутатора. Можно указать <all>. Нумерация портов начинается с 0.')
  134. options = m.parse_args()
  135.  
  136. if options.a:
  137.     switch = options.a[0]
  138.     s = connect(host[switch]['address'], 22, host[switch]['login'], host[switch]['password'])
  139.     command = options.c[0]
  140.     process()
  141.     s.close()
  142.     print '*** Disconnect...'
  143.     exit(0)
  144.    
  145. if options.switchall:
  146.     collection = ['10', '11', '12', '13','18', '19', '23', '24']
  147.  
  148. if options.dc:
  149.     datacenter = options.dc[0]
  150.     if (datacenter == 'SafeData'):
  151.         collection = ['18', '19', '23', '24']
  152.     if (datacenter == 'IXCellerate'):
  153.         collection = ['10', '11', '12', '13']
  154.  
  155. if options.f:
  156.     fabric = options.f[0]
  157.     if (fabric == '1'):
  158.         collection = ['10', '12', '18', '24']
  159.     if (fabric == '2'):
  160.         collection = ['11', '13', '19', '23']
  161.  
  162. if options.st:
  163.     switchtype = options.st[0]
  164.     if (switchtype == 'core'):
  165.         collection = ['10', '11', '18', '19']
  166.     if (switchtype == 'edge'):
  167.         collection = ['12', '13', '23', '24']
  168.                
  169. for switch in collection:
  170.     s = connect(host[switch]['address'], 22, host[switch]['login'], host[switch]['password'])
  171.     command = options.c[0]
  172.     process()
  173.     s.close()
  174.     print '*** Disconnect...'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement