def search(name=None, type=None, domain="local"): """ Search available Zeroconf services The result is a dictionary with service (name, type, domain) keys and data values ; data are dictionaries with "hostname", "address", "port" and "txt" keys. """ def name_match(service): name_, _, _ = service return name is None or name_ == name if sys.platform.startswith("linux"): options = {"terminate": True, "resolve": True, "parsable": True, "no-db-lookup": True, "domain": domain} if type: results = sh.avahi_browse(type, **options) else: results = sh.avahi_browse(all=True, **options) results = [line.split(";") for line in results.splitlines()] info = {} for result in results: if result[0] == "=": symbol, _, ip_version, name_, type_, domain_, hostname, address, port, txt = result name_ = decode(name_) if ip_version == "IPv4": info[(name_, type_, domain_)] = {"hostname": hostname, "address": address, "port": port, "txt": txt} elif sys.platform.startswith("win"): if not type: type = "_http._tcp" process = subprocess.Popen("dns-sd -Z " + type + " " + domain, stdout=subprocess.PIPE, startupinfo=startupinfo) time.sleep(1.0) process.kill() results = process.stdout.read() results = [line.split() for line in results.splitlines()] info = {} name_ = port = hostname = address = "" for result in results: if len(result) == 14 and result[1] == "SRV": name_ = decode(result[0]).split(".")[0] port = result[4] hostname = result[5] address = get_address(hostname) type_ = decode(result[0])[(decode(result[0]).find(".") + 1) :] if len(result) == 3 and result[1] == "TXT": txt = str.replace(result[2], '"', "") info[(name_, type_, domain)] = {"hostname": hostname, "address": address, "port": port, "txt": txt} filtered_info = [item for item in info.items() if name_match(item[0])] return dict(filtered_info)
def search(self, name=None, type=None, domain="local"): """ Search for a service in specified domain """ options = {"terminate": True, "resolve": True, "parsable": True, "no-db-lookup": True, "domain": domain} if type: results = sh.avahi_browse(type, **options).splitlines() else: results = sh.avahi_browse(all=True, **options).splitlines() info = {} for result in [line.split(';') for line in results]: if result[0] == "=": ip_version, name_, type, domain_ = result[2:6] name_ = decode(name_) if ip_version != "IPv4": continue info[(name_, type, domain_)] = dict(zip(NAMES, result[6:])) filtered_info = [i for i in info.items() if name_match(name, i[0])] return dict(filtered_info)
def discovery(hostnames=True,targets=[]): if not targets: targets = ["nodes"] discovery = sh.avahi_browse('-trp', '_ssh._tcp').split('\n') hosts = [] for host in discovery: host = host.split(';') if host[0] == '=' and ( 'avahi' in targets \ or ( 'nodes' in targets and host[3][:5] == 'node-' ) \ or ( 'special' in targets and host[3] in special_hosts ) ): if hostnames: hosts.append(_get_revdns(host[7])) else: hosts.append(host[7]) return hosts
def search(name=None, type=None, domain="local"): """ Search available Zeroconf services The result is a dictionary with service (name, type, domain) keys and data values ; data are dictionaries with "hostname", "address", "port" and "txt" keys. """ def name_match(service): name_, _, _ = service return (name is None or name_ == name) if sys.platform.startswith("linux"): options = { "terminate": True, "resolve": True, "parsable": True, "no-db-lookup": True, "domain": domain } if type: results = sh.avahi_browse(type, **options) else: results = sh.avahi_browse(all=True, **options) results = [line.split(";") for line in results.splitlines()] info = {} for result in results: if result[0] == "=": symbol, _, ip_version, name_, type_, domain_, \ hostname, address, port, txt = result name_ = decode(name_) if ip_version == "IPv4": info[(name_, type_, domain_)] = { "hostname": hostname, "address": address, "port": port, "txt": txt } elif sys.platform.startswith("win"): if not type: type = "_http._tcp" process = subprocess.Popen("dns-sd -Z " + type + " " + domain, \ stdout=subprocess.PIPE, \ startupinfo=startupinfo) time.sleep(1.0) process.kill() results = process.stdout.read() results = [line.split() for line in results.splitlines()] info = {} name_ = port = hostname = address = "" for result in results: if len(result) == 14 and result[1] == "SRV": name_ = decode(result[0]).split(".")[0] port = result[4] hostname = result[5] address = get_address(hostname) type_ = decode(result[0])[(decode(result[0]).find(".") + 1):] if len(result) == 3 and result[1] == "TXT": txt = str.replace(result[2], '"', '') info[(name_, type_, domain)] = { "hostname": hostname, "address": address, "port": port, "txt": txt } filtered_info = [item for item in info.items() if name_match(item[0])] return dict(filtered_info)