def get_host_info(hostname): if(not hostname): raise Exception("Null Parameter", "Hostname parameter is empty.") #Try hostname. If it fails, try host name for interface. If that also fails, finally do dns resolution queryString = HOST_NAME+"="+hostname response = query(queryString) hosts=[] if response: uris=__get_interface_uris_for_host(response) interfaces = __get_interfaces(uris) #get interfaces for result in response: result[HOST_INTERFACE]=[] for interface in interfaces: result[HOST_INTERFACE].append(interface) hosts=response else: queryString = INTERFACE_ADDRESS+"="+hostname response = query(queryString) if response: hosts = __get_hosts_for_interfaces(response) else: #DNS resolution ipaddr = socket.getaddrinfo(hostname, None) ipaddresses={} #ipaddr is a 5-tuple and the ipv4 or ipv6 is the last element in the tuple. for ip in ipaddr: if(ip[4][0]): ipaddresses[ip[4][0]]=1 ipaddresses[hostname]=1 hosts=[] for ip in ipaddresses: queryString = INTERFACE_ADDRESS+"="+ip response = query(queryString) if response: hosts += __get_hosts_for_interfaces(response) for host in hosts: admin = __get_admin_info(host) host[HOST_ADMINISTRATORS] = admin return hosts
def get_ma_for_host(hostname): ipaddresses = {} if (not hostname): raise Exception("Null Parameter", "Hostname parameter is empty.") ipaddr = socket.getaddrinfo(hostname, None) ipaddresses[hostname] = 1 #ipaddr is a 5-tuple and the ipv4 or ipv6 is the last element in the tuple. for ip in ipaddr: if (ip[4][0]): ipaddresses[ip[4][0]] = 1 response = [] result = {} queryKeys = ['psmetadata-dst-address', 'psmetadata-src-address'] for queryKey in queryKeys: for ip in ipaddresses: queryString = queryKey + "=" + ip response += query(queryString) for record in response: malocators = record[u'psmetadata-ma-locator'] for malocator in malocators: malocatorString = malocator.encode("ascii") result[malocatorString] = 1 return result.keys()
def __get_admin_info(host): adminUris = host[HOST_ADMINISTRATORS] response=[] if(adminUris): for adminUri in adminUris: queryString = RECORD_URI+"="+adminUri response += query(queryString) return response
def __get_hosts_for_interfaces(list_of_interfaces): hostResponse=[] for interface in list_of_interfaces: uri = interface[RECORD_URI] hostQuery=HOST_INTERFACE+"="+uri response = query(hostQuery) for host in response: host[HOST_INTERFACE] = interface hostResponse+=response return hostResponse
def search(record,text): queryRecord = 'type='+record response = query(queryRecord) queryPattern=''+text result=[] for record in response: if u'ls-host' in record: record.pop(u'ls-host') for key in record: if queryPattern in record[key]: result.append(record) return result
def search_domains(domain, record_type): if (not domain): raise Exception("Null Parameter", "Domain is empty.") #Try hostname. If it fails, try host name for interface. If that also fails, finally do dns resolution queryString = GROUP_DOMAINS + "=" + hostname if record_type: queryString = "&" + RECORD_TYPE + "=" + record_type response = query(queryString) hosts = [] if response: for record in response: hosts.append(record['host-name']) for host in hosts: admin = __get_admin_info(host) host[HOST_ADMINISTRATORS] = admin return hosts
from __future__ import print_function from sls_client.records import * from sls_client.query import * from sls_client.find_ma import * ##query all services #queryString = 'type=services' #response = query(queryString) #print response #for record in response: # print record[u'host-name'] #query host in a domain queryString = 'type=host&group-domains=anl.gov' response = query(queryString) for record in response: print(record[u'host-name']) #query for MA containing host data #hostname = '202.179.252.18' #result = get_ma_for_host(hostname) #print result
def get_record(): queryString = '' response = query(queryString) for record in response: yield record
def __get_interfaces(list_of_uris): interfaceResponse = [] for uri in list_of_uris: interfaceQuery= RECORD_URI+'='+uri interfaceResponse += query(interfaceQuery) return interfaceResponse