Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- #! -*- coding: utf-8 -*-
- import argparse
- import paramiko
- import re
- import sys
- host = {
- '10': {
- 'address' : 'IP',
- 'login' : 'admin',
- 'password' : 'pass',
- 'ports' : 12
- },
- '11': {
- 'address' : 'IP',
- 'login' : 'admin',
- 'password' : 'pass',
- 'ports' : 12
- },
- '12': {
- 'address' : 'IP',
- 'login' : 'admin',
- 'password' : 'pass',
- 'ports' : 12
- },
- '13': {
- 'address' : 'IP',
- 'login' : 'admin',
- 'password' : 'pass',
- 'ports' : 12
- },
- '18': {
- 'address' : 'IP',
- 'login' : 'admin',
- 'password' : 'pass',
- 'ports' : 12
- },
- '19': {
- 'address' : 'IP',
- 'login' : 'admin',
- 'password' : 'pass',
- 'ports' : 12
- },
- '23': {
- 'address' : '1IP',
- 'login' : 'admin',
- 'password' : 'pass',
- 'ports' : 24
- },
- '24': {
- 'address' : 'IP',
- 'login' : 'admin',
- 'password' : 'pass',
- 'ports' : 24
- },
- }
- 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)
- print '*** Connected to', host
- return s
- def findali(s, port):
- wwn = findWWN(s, port)
- if wwn == 'Trunk port':
- alias = 'Trunk port'
- else:
- alias = findAlias(s, wwn)
- if alias is None:
- alias = wwn
- print 'port', port, ':', alias, '(', wwn, ')'
- def findWWN(conn, port):
- command = 'portshow ' + str(port)
- (stdin, stdout, stderr) = s.exec_command(command)
- for line in stdout.readlines():
- wwn = re.findall('\t(([0-9A-Fa-f]{2}:?){8})', line)
- if len(wwn) > 0:
- return wwn[0][0]
- trunk = re.findall('Trunk (master )?port', line)
- if len(trunk) > 0:
- return 'Trunk port'
- def findAlias(conn, wwn):
- command = 'nodefind ' + str(wwn)
- (stdin, stdout, stderr) = s.exec_command(command)
- for line in stdout.readlines():
- aliases = re.findall('Aliases: (.+)\n', line)
- if aliases:
- return aliases[0]
- def switchshow(conn):
- command = 'switchshow'
- (stdin, stdout, stderr) = s.exec_command(command)
- for line in stdout.readlines():
- line = line.rstrip()
- if line != '':
- print line
- def process():
- if command == 'findali':
- try:
- port = options.p[0]
- if port == 'all':
- for i in range(0, host[switch]['ports']):
- globals()[command](s, i)
- elif ',' in port:
- for i in port:
- if i == ',':
- continue
- globals()[command](s, i)
- else:
- globals()[command](s, port)
- except TypeError:
- print 'Port not defined.';
- exit()
- m = argparse.ArgumentParser(description='''Работа с SAN коммутаторами Brocade в Onlanta Oncloud''')
- m.add_argument('-c', nargs=1, type=str, help='''Комманда для выполнения. findali''')
- m.add_argument('-switchall', action='store_true', help='Выполнение на всех SAN коммутаторах.')
- m.add_argument('-a', nargs=1, type=str, help='Адресс SAN коммутатора (указываются последние 2 цифры IP адреса).')
- m.add_argument('-dc', nargs=1, type=str, help='Выбор всех коммутаторов ЦОДа (SafeData или IXCellerate).')
- m.add_argument('-f', nargs=1, type=str, help='Выбор всех коммутаторов фабрики (1 и 2).')
- m.add_argument('-st', nargs=1, type=str, help='Выбор типа коммутаторов фабрик (core и edge).')
- m.add_argument('-p', nargs=1, type=str, help='Порт коммутатора. Можно указать <all>. Нумерация портов начинается с 0.')
- options = m.parse_args()
- if options.a:
- switch = options.a[0]
- s = connect(host[switch]['address'], 22, host[switch]['login'], host[switch]['password'])
- command = options.c[0]
- process()
- s.close()
- print '*** Disconnect...'
- exit(0)
- if options.switchall:
- collection = ['10', '11', '12', '13','18', '19', '23', '24']
- if options.dc:
- datacenter = options.dc[0]
- if (datacenter == 'SafeData'):
- collection = ['18', '19', '23', '24']
- if (datacenter == 'IXCellerate'):
- collection = ['10', '11', '12', '13']
- if options.f:
- fabric = options.f[0]
- if (fabric == '1'):
- collection = ['10', '12', '18', '24']
- if (fabric == '2'):
- collection = ['11', '13', '19', '23']
- if options.st:
- switchtype = options.st[0]
- if (switchtype == 'core'):
- collection = ['10', '11', '18', '19']
- if (switchtype == 'edge'):
- collection = ['12', '13', '23', '24']
- for switch in collection:
- s = connect(host[switch]['address'], 22, host[switch]['login'], host[switch]['password'])
- command = options.c[0]
- process()
- s.close()
- print '*** Disconnect...'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement