-
Notifications
You must be signed in to change notification settings - Fork 0
/
ucs-mac-search.py
60 lines (56 loc) · 2.93 KB
/
ucs-mac-search.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
from UcsSdk import *
from UcsSdk.MoMeta.LsPower import LsPower
import re
import argparse
import getpass
from prettytable import PrettyTable
parser = argparse.ArgumentParser()
parser.add_argument('-m', '--mac-address-wildcard', required=True, help="Wildcard match for any MAC addresses that contain this string")
parser.add_argument('-l', '--login', default='admin', help="Login for UCS Manager. Default is 'admin'.")
parser.add_argument('-p', '--password', help="Password for UCS Manager.")
parser.add_argument('-u', '--ucs', nargs='*', required=True, help="Hostname or IP address of UCS Manager") # TODO make this work with multiple UCS IP's
args = parser.parse_args()
mac_address_wildcard = args.mac_address_wildcard
def create_mac_sp_dict(ucs, login, password, mac):
for attempt in range(3):
try:
handle = UcsHandle()
print "Connecting to UCS at %s as %s." % (ucs, login)
if not password:
password = getpass.getpass(prompt='UCS Password: ')
handle.Login(ucs, login, password)
output = []
mo_dn_array = {}
getRsp = handle.GetManagedObject(None, None,{"Dn":"org-root/"})
moArr = handle.GetManagedObject(getRsp, "macpoolAddr")
for mo in moArr:
origDn = str(mo.Dn)
if mac_address_wildcard.lower() in origDn.lower() and str(mo.Assigned) == "yes":
matches = {}
mac = str(mo.Id)
service_profile_and_vnic = str(mo.AssignedToDn)
service_profile_and_vnic = re.search(r'(?<=ls-)(\S*)', service_profile_and_vnic)
service_profile_and_vnic = service_profile_and_vnic.group().split("/ether-")
matches = {"UCS" : ucs, "service profile" : service_profile_and_vnic[0], "MAC address" : mac, "vNIC" : service_profile_and_vnic[1]}
output.append(matches)
return output
handle.Logout()
break
except Exception, err:
if "Authentication failed" in str(err):
print 'Authentication failed.'
password = getpass.getpass(prompt='Please re-enter password for %s at UCS %s: ' % (login, ucs))
else:
print "Exception:", str(err)
import traceback, sys
print '-' * 60
traceback.print_exc(file=sys.stdout)
print '-' * 60
matches = []
for ucs in args.ucs:
matches.extend(create_mac_sp_dict(ucs, args.login, args.password, args.mac_address_wildcard))
mac_table = PrettyTable(["MAC Address", "vNIC", "Service Profile", "UCS IP"])
for match in matches:
mac_table.add_row([match["MAC address"], match["vNIC"], match["service profile"], match["UCS"]])
mac_table.sortby = "MAC Address"
print mac_table