Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add host lookup from mac address #28

Merged
merged 5 commits into from
Nov 3, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions pypureomapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -1189,6 +1189,62 @@ def lookup_host(self, name):
except KeyError:
raise OmapiErrorNotFound()

def lookup_host_host(self, mac):
"""Look for a host object with given mac address and return the
name, mac, and ip address

@type mac: str
@rtype: str or None
@raises ValueError:
@raises OmapiError:
@raises OmapiErrorNotFound: if no host object with the given name
could be found or the object lacks an ip address or mac
@raises socket.error:
"""
return self.lookup('host', ['ip', 'mac', 'name'], mac=mac)

def lookup(self, ltype, rvalues=[], ip=None, mac=None, name=None):
"""Generic Lookup function

@type ltype: str
@type rvalues: list
@type ip: str
@type mac: str
@type name: str
@rtype: dict or str (if len(rvalues) == 1) or None
@raises ValueError:
@raises OmapiError:
@raises OmapiErrorNotFound: if no host object with the given name
could be found or the object lacks an ip address or mac
@raises socket.error:
"""
msg = OmapiMessage.open(ltype.encode('utf-8'))
if ip:
msg.obj.append((b"ip-address", pack_ip(ip)))
if mac:
msg.obj.append((b"hardware-address", pack_mac(mac)))
msg.obj.append((b"hardware-type", struct.pack("!I", 1)))
if name:
msg.obj.append((b"name", name.encode('utf-8')))
response = self.query_server(msg)
if response.opcode != OMAPI_OP_UPDATE:
raise OmapiErrorNotFound()
try:
result = {}
for elem in rvalues:
if elem == 'ip':
result['ip'] = unpack_ip(dict(response.obj)[b"ip-address"])
if elem == 'mac':
result['mac'] = unpack_mac(dict(response.obj)[b"hardware-address"])
if elem == 'name':
hostname = dict(response.obj)[b"name"]
result['hostname'] = hostname.decode('utf-8')
if len(result) == 1:
result = list(result.values())[0]
return result
except KeyError:
raise OmapiErrorNotFound()

def add_host_supersede_name(self, ip, mac, name): # pylint:disable=E0213
"""Add a host with a fixed-address and override its hostname with the given name.
@type self: Omapi
Expand Down