def get_peers(self): ''' {"hostname": "stratum.bitcoin.cz", "trusted": True, "weight": 0, "transports": [{"type": "http", "proto": "ipv4", address: "192.168.1.1", "port": 80}, {"type": "http", "proto": "onion", "address": "56ufgh56ygh5.onion", "port": 80}]} ''' # FIXME: Cache result/DNS lookup peers = [] # Include hardcoded peers for peer in settings.PEERS: if not peer.get('ipv4'): try: peer['ipv4'] = (yield client.getHostByName(peer['hostname'])) except Exception: log.error("Failed to resolve hostname '%s'" % peer['hostname']) continue peers.append({ 'hostname': peer['hostname'], 'trusted': peer.get('trusted', False), 'weight': peer.get('weight', 0), 'ipv4': peer.get('ipv4'), 'ipv6': peer.get('ipv6'), }) if settings.IRC_NICK: try: irc_peers = irc.get_connection().get_peers() except custom_exceptions.IrcClientException: log.error("Retrieving IRC peers failed") irc_peers = [] for peer in irc_peers: try: ipv4 = (yield client.getHostByName(peer)) except Exception: log.error("Failed to resolve hostname '%s'" % peer['hostname']) continue peers.append({ 'hostname': peer, 'trusted': False, 'weight': 0, 'ipv4': ipv4, 'ipv6': None, }) defer.returnValue(peers)
def add_contact(self, host, port, callback=None): if khashmir.ip_pat.match(host): self._got_host(host, port, callback) else: d = getHostByName(host) d.addCallback(self._got_host, port, callback) d.addErrback(log_ex, "failed to lookup address for host=%s" % (host))
def test_getHostByName(self): """ do a getHostByName of a value that should return 127.0.0.1. """ d = client.getHostByName(self.hostnameForGetHostByName) d.addCallback(self.checkGetHostByName) return d
def test_dns(callback=None): """Can we lookup www.bitblinder.com and get a response?""" #resolve the address testAddress = "bitblinder.com" d = getHostByName(testAddress) #add our callbacks def dns_succeeded(address, testAddress=testAddress): log_msg( "DNS for %s successfully resolved to %s" % (testAddress, address), 3) global _isDNSWorking _isDNSWorking = True return True d.addCallback(dns_succeeded) def dns_failed(reason, testAddress=testAddress): log_msg("DNS for %s failed: %s" % (testAddress, reason), 3) _isDNSWorking = False return False d.addErrback(dns_failed) return d
def do_dns_lookup(self, domain): def onSuccess(result, domain): log.debug('got ip address %s for domain %s' %(result, domain)) self.ip_address = result def onFailure(reason): log.error(reason) d = dns_client.getHostByName(self.host) d.addCallback(onSuccess, domain).addErrback(onFailure) return d
def __init__(self, url, *args, **kargs): client.HTTPClientFactory.__init__(self, url, *args, **kargs) self.running = [] scheme, host, port, path = client._parse(url) if socks: from twisted.names.client import getHostByName def connectToIP(ip): self.socks = SOCKS4Client(ip, port, self) reactor.connectTCP(socks_addr, socks_port, self.socks) def failed(failure): self.deferred.errback(failure) getHostByName(host).addCallback(connectToIP).addErrback(failed) else: reactor.connectTCP(host, port, self)
def _verifyDNSResolution(self, result, timeout): name = result[0][0].payload.name.name if len(name) > self.ircd.config.get("hostname_length", 64): self._cancelDNSResolution() return if not isValidHost(name): self._cancelDNSResolution() return resolveDeferred = dnsClient.getHostByName(name, ((timeout/2),)) resolveDeferred.addCallbacks(callback=self._completeDNSResolution, errback=self._cancelDNSResolution, callbackArgs=(name,))
def get_callback_auth_details(url, trusted_clients): match = ip_scheme_re.match(url) if not match or len(match.groups()) < 2: ip = client.getHostByName(url) else: scheme, ip = match.groups() for client_ip, details in trusted_clients.iteritems(): if ip == client_ip: return details['user'], details['pass'] return None
def main(reactor, *argv): try: hostname = argv[0] except IndexError: sys.stderr.write(__doc__.lstrip() + "\n" "ERROR: missing HOSTNAME argument\n") raise SystemExit(1) d = client.getHostByName(hostname) d.addCallback(printResult, hostname) d.addErrback(printError, hostname) return d
def get_routes_router(self, cfg, router): """ Get routes from a router using IP-FORWARD-MIB::ipCidrRouteTable. Alternatively, if the table is empty, use RFC1213-MIB::ipRouteTable. @param cfg: configuration @param router: name of the router @return: a deferred list of tuples (net, interface) """ results = [] ip = yield client.getHostByName(router) proxy = AgentProxy(ip=ip, community=cfg['routers'][router]) interfaces = yield self.get_interfaces(cfg, proxy) base = '.1.3.6.1.2.1.4.24.4.1.5' # IP-FORWARD-MIB::ipCidrRouteIfIndex : route + netmask + gateway + interface routes = yield proxy.walk(base) if not routes: # We use RFC1213-MIB instead base = '.1.3.6.1.2.1.4.21.1' types = yield proxy.walk("%s.8" % base) for oid in types: if types[oid] != 3: # Not a direct route continue ip = ".".join(oid.split(".")[-4:]) intoid = "%s.2.%s" % (base, ip) maskoid = "%s.11.%s" % (base, ip) info = yield proxy.get([intoid, maskoid]) interface, mask = info[intoid], info[maskoid] if not interface: continue results.append((IPNetwork("%s/%s" % (ip, mask)), interfaces.get(interface, "unknown"))) defer.returnValue(results) for oid in routes: # We use IP-FORWARD-MIB # Only keep connected routes if not oid.endswith(".0.0.0.0"): continue interface = routes[oid] oid = oid.split(".") network = ".".join(oid[-13:-9]) mask = ".".join(oid[-9:-5]) # F5 BigIP may reverse the mask... if not IPAddress(mask).is_netmask(): mask = mask.split(".") mask.reverse() mask = ".".join(mask) if not IPAddress(mask).is_netmask(): continue results.append((IPNetwork("%s/%s" % (network, mask)), interfaces.get(interface, "unknown"))) defer.returnValue(results)
def _verifyDNSResolution(self, result, timeout): name = result[0][0].payload.name.name if len(name) > self.ircd.config.get("hostname_length", 64): self._cancelDNSResolution() return if not isValidHost(name): self._cancelDNSResolution() return resolveDeferred = dnsClient.getHostByName(name, ((timeout / 2), )) resolveDeferred.addCallbacks(callback=self._completeDNSResolution, errback=self._cancelDNSResolution, callbackArgs=(name, ))
def do_lookup(self, name, flag, mydesc, myorg): """Schedule a dns lookup. name dns hostname to lookup flag true if this is the 1st time thru for this dns hostname mydesc additional description information concerning hostile ip myorg blocklist identifier If this is not the 1st time through for this hostname, then it must have come from the wait queue. So decrement the cnt of requests in the wait queue. If there is still room in the current burst of dns lookups, then schedule the actual lookup and bump the activity counter of requests currently running. If there is no room in the current burst pipeline, then send the request back to the wait queue, bump the wait counter, and re-schedule this lookup for later on. """ if cfg.debug >= cfg.DEBUG_VERBOSE: log.msg("wksrv: dns_lkup: {0}, {1} org: {2} |{3}|".format( name, flag, myorg, mydesc)) # if not 1st time call, then decrement wait cnt if (flag): self.num_wait = max(0, self.num_wait - 1) if cfg.debug >= cfg.DEBUG_VERBOSE: log.msg("wksrv: dns_lkup: wait cnt:", self.num_wait, name) # if not throttling then kick off next request if self.num_x < self.max_x: self.num_x = self.num_x + 1 if cfg.debug >= cfg.DEBUG_VERBOSE: log.msg("wksrv: dns_lkup: fire another, exec cnt", self.num_x, name) d = getHostByName(name) d.addCallback(self.lookup_done, name, mydesc, myorg) d.addErrback(self.lookup_err, name, mydesc, myorg) else: # make the request wait a bit self.num_wait = self.num_wait + 1 if cfg.debug >= cfg.DEBUG_VERBOSE: log.msg("wksrv: dns_lkup: wait another", self.num_wait, name) # schedule this lookup for later on d = task.deferLater(reactor, (self.max_x + self.num_wait) / self.max_x, self.do_lookup, name, True, mydesc, myorg)
def main(reactor, *argv): try: hostname = argv[0] except IndexError: sys.stderr.write(__doc__.lstrip() + '\n' 'ERROR: missing HOSTNAME argument\n') raise SystemExit(1) d = client.getHostByName(hostname) d.addCallback(printResult, hostname) d.addErrback(printError, hostname) return d
def main(reactor, *argv): options = Options() try: options.parseOptions(argv) except usage.UsageError as errortext: sys.stderr.write(str(options) + "\n") sys.stderr.write(f"ERROR: {errortext}\n") raise SystemExit(1) hostname = options["hostname"] d = client.getHostByName(hostname) d.addCallback(printResult, hostname) d.addErrback(printError, hostname) return d
def main(reactor, *argv): options = Options() try: options.parseOptions(argv) except usage.UsageError as errortext: sys.stderr.write(str(options) + '\n') sys.stderr.write('ERROR: %s\n' % (errortext,)) raise SystemExit(1) hostname = options['hostname'] d = client.getHostByName(hostname) d.addCallback(printResult, hostname) d.addErrback(printError, hostname) return d
def findOwnIpAddr(): def gotResult(result): return result addr_or_d = None try: s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # umm, I know google is never, never, never-ever down right? s.connect(('google.com', 0)) addr_or_d = s.getsockname()[0] except: try: addr_or_d = client.getHostByName(socket.getfqdn()) addr_or_d.addCallback(gotResult) except: addr_or_d = "127.0.0.1" return addr_or_d
def lookup_host(bot, user, channel, msg, guess_best): hostname = msg if len(hostname): if guess_best: d = client.getHostByName(hostname) d.addCallback(resultCb, hostname, bot, user, channel) d.addErrback(errorCb, hostname, bot, user, channel) else: pending = [] pending.append(client.lookupAddress(hostname)) pending.append(client.lookupIPV6Address(hostname)) allResults = defer.DeferredList(pending, consumeErrors=False) allResults.addCallback(resultCb, hostname, bot, user, channel) allResults.addErrback(errorCb, hostname, bot, user, channel) else: bot.msg(channel, 'Usage: !host <hostname>')
def ajp(self, uri, addresses=None): """Non-blocking version of Response.http() - returns a Deferred if the resolver is used.""" assert uri[0] != '/' or addresses is None or len(addresses) == 0 assert addresses is None or hasattr(addresses, '__iter__') self.packet(TRANSLATE_AJP, uri) if uri[0] != '/' and addresses is None: # parse host:port from URL d = defer.Deferred() host, port = (addresses.split(':', 1) + [None])[:2] e = getHostByName(host) e.addCallback(self._resolve_success, d, port) e.addErrback(d.errback) return d
def resolv(): get = {} gr = '/etc/watcher-server/hosts/%s' % group f = open(gr, 'r') hosts = f.readlines() hosts = [x[0:-1] for x in hosts] if os.path.isfile(tmpfile): f = open(tmpfile) data_tmp = json.load(f) f.close() else: data_tmp = {} def resolv_host(metrics, host): get[host] = metrics def resolv_failed(err, host): print >> sys.stderr, datetime.datetime.now(), host, err get[host] = host def tailer_done(_): if len(get) == len(hosts): for key, value in get.items(): matches = re.compile( '(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))' ).findall(value) if len(matches): data_tmp[key] = value else: data_tmp[key] = data_tmp.get(key, value) with open(tmpfile, 'w') as outfile: json.dump(data_tmp, outfile) r.reset() for addres in hosts: host = addres d = getHostByName(host, timeout=(2, 3)) d.addCallback(resolv_host, host) d.addErrback(resolv_failed, host) d.addBoth(tailer_done)
def main(reactor): hosts = readList() for host in hosts: inicio = datetime.datetime.now() if host[1] == 'A\n': d = client.getHostByName(host[0]) l = [time()] a = d.addCallback(printResult2, l) print l print a, type(a) b = d.addErrback(error, inicio) print b else: d = client.lookupPointer(name=reverseNameFromIPAddress( address=host[0])) d.addCallback(printResult, inicio) d.addErrback(error, inicio) return d
def resolv(): get = {} gr = '/etc/watcher-server/hosts/%s' % group f = open(gr, 'r') hosts = f.readlines() hosts = [x[0:-1] for x in hosts] if os.path.isfile(tmpfile): f = open(tmpfile) data_tmp = json.load(f) f.close() else: data_tmp = {} def resolv_host(metrics, host): get[host] = metrics def resolv_failed(err, host): print >> sys.stderr, datetime.datetime.now(), host, err get[host] = host def tailer_done(_): if len(get) == len(hosts): for key, value in get.items(): matches = re.compile( '(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))').findall(value) if len(matches): data_tmp[key] = value else: data_tmp[key] = data_tmp.get(key, value) with open(tmpfile, 'w') as outfile: json.dump(data_tmp, outfile) r.reset() for addres in hosts: host = addres d = getHostByName(host, timeout=(2, 3)) d.addCallback(resolv_host, host) d.addErrback(resolv_failed, host) d.addBoth(tailer_done)
def gotIP(self, ips, resolve=True): if not ips: if resolve: d = client.getHostByName(self.name) d.addCallbacks( lambda x: self.gotIP([[self.name, x]], resolve=False), lambda x: self.gotIP(None, resolve=False)) return d fragment = T.span["I cannot find any IP for this host"] fragment = FragmentMixIn(self.dbpool, docFactory=loaders.stan(fragment)) fragments = [fragment] else: fragments = [] for ip in ips: fragment = T.span [ "The hostname ", resolve and T.a(href="equipment/%s/" % ip[1], render=self.render_apiurl)[ip[0]] or \ T.span(_class="data")[ip[0]], " is associated with IP ", T.invisible(data=ip[1], render=T.directive("ip")), resolve and \ T.invisible[ ". You can ", T.a(href="search/%s/" % ip[1], render=self.render_apiurl) ["search on it"], " to find more results." ] or "."] fragment = FragmentMixIn(self.dbpool, docFactory=loaders.stan(fragment)) fragments.append(fragment) fragments.append(SearchHostnameInLldp(self.dbpool, self.name)) fragments.append(SearchHostnameInCdp(self.dbpool, self.name)) fragments.append(SearchHostnameInEdp(self.dbpool, self.name)) fragments.append(SearchInDescription(self.dbpool, self.name)) return fragments
def parse_clients_list(file_path): trusted_clients = None # @TODO create a service to read trusted clients from DB try: trusted_clients = open(file_path, 'r').readlines() trusted_clients = map(lambda c: c.replace('\n', ''), trusted_clients) except IOError: _clients = [] log.warn("Trusted clinets list not found.") clients_list = {} if trusted_clients: for row in csv.reader(trusted_clients, dialect='pipes', quotechar='!'): _host, _user, _pass = row if ip_re.match(_host): _ip = _host else: _host = prepare_url(_host) parsed_url = urlparse(_host) _ip = yield client.getHostByName(parsed_url.netloc) clients_list[_ip] = {'host': _host, 'user': _user, 'pass': _pass} defer.returnValue(clients_list)
def test_dns(callback=None): """Can we lookup www.bitblinder.com and get a response?""" # resolve the address testAddress = "bitblinder.com" d = getHostByName(testAddress) # add our callbacks def dns_succeeded(address, testAddress=testAddress): log_msg("DNS for %s successfully resolved to %s" % (testAddress, address), 3) global _isDNSWorking _isDNSWorking = True return True d.addCallback(dns_succeeded) def dns_failed(reason, testAddress=testAddress): log_msg("DNS for %s failed: %s" % (testAddress, reason), 3) _isDNSWorking = False return False d.addErrback(dns_failed) return d
def gotIP(self, ips, resolve=True): if not ips: if resolve: d = client.getHostByName(self.name) d.addCallbacks(lambda x: self.gotIP([[self.name,x]], resolve=False), lambda x: self.gotIP(None, resolve=False)) return d fragment = T.span [ "I cannot find any IP for this host" ] fragment = FragmentMixIn(self.dbpool, docFactory=loaders.stan(fragment)) fragments = [fragment] else: fragments = [] for ip in ips: fragment = T.span [ "The hostname ", resolve and T.a(href="equipment/%s/" % ip[1], render=self.render_apiurl)[ip[0]] or \ T.span(_class="data")[ip[0]], " is associated with IP ", T.invisible(data=ip[1], render=T.directive("ip")), resolve and \ T.invisible[ ". You can ", T.a(href="search/%s/" % ip[1], render=self.render_apiurl) ["search on it"], " to find more results." ] or "."] fragment = FragmentMixIn(self.dbpool, docFactory=loaders.stan(fragment)) fragments.append(fragment) fragments.append(SearchHostnameInLldp(self.dbpool, self.name)) fragments.append(SearchHostnameInCdp(self.dbpool, self.name)) fragments.append(SearchHostnameInEdp(self.dbpool, self.name)) fragments.append(SearchInDescription(self.dbpool, self.name)) return fragments
def resolve_dns_bootstrap_addresses(self): for (address, port) in _DNS_ADDRESSES: task = self.register_task("DNS-RESOLVE:" + address, client.getHostByName(address)) task.addCallback(lambda ip: _DEFAULT_ADDRESSES.append((ip, port)))
#!/usr/bin/env python # Copyright (c) Twisted Matrix Laboratories. # See LICENSE for details. """ Returns the IP address for a given hostname. To run this script: $ python gethostbyname.py <hostname> e.g.: $ python gethostbyname.py www.google.com """ import sys from twisted.names import client from twisted.internet import reactor def gotResult(result): print result reactor.stop() def gotFailure(failure): failure.printTraceback() reactor.stop() d = client.getHostByName(sys.argv[1]) d.addCallbacks(gotResult, gotFailure) reactor.run()
def query(reactor, server, name): resolver = client.getHostByName(name) return resolver
import sys from twisted.names import client from twisted.internet import reactor def gotResult(result): print result reactor.stop() def gotFailure(failure): failure.printTraceback() reactor.stop() domains = ['google.com', 'facebook.com', 'ankit'] #domain = 'twistedmatrix.com' for domain in domains: try: d = client.getHostByName(domain) d.addCallbacks(gotResult, gotFailure) except: pass reactor.run() #!/usr/bin/env python # Copyright (c) Twisted Matrix Laboratories. # See LICENSE for details. """ Returns the IP address for a given hostname. To run this script: $ python gethostbyname.py <hostname> e.g.:
#!/usr/bin/env python # Copyright (c) Twisted Matrix Laboratories. # See LICENSE for details. import sys from twisted.names import client from twisted.internet import reactor def gotResult(result): print result reactor.stop() def gotFailure(failure): failure.printTraceback() reactor.stop() d = client.getHostByName(sys.argv[1]) d.addCallbacks(gotResult, gotFailure) reactor.run()
import sys receiveAddress = "127.0.0.1" sendAddress = "152.78.242.119" sendHostName = "uos-13872.clients.soton.ac.uk" receivePort = 4242 sendPort = 4241 def queryResult(res=""): print( "Address query returned: %s" % str(res) ) def queryError(res=""): print( "Address query failed: %s" % str(res) ) sendAddressQuery = client.getHostByName( sendHostName ) sendAddressQuery.addCallback( queryResult ) sendAddressQuery.addErrback( queryError ) # Global variables and functions # TODO: Encapsulate them into objects processedObjectVector = { "objects": [] } def setProcessedObjectVector( vec ): global processedObjectVector processedObjectVector = vec def getProcessedObjectVector(): global processedObjectVector return processedObjectVector
def gethostbyname(hostname): return getHostByName(hostname)
def add_hostname(self, hostname): d = nameclient.getHostByName(hostname) d.addCallback(self.add_ip)
def do_lookup(self, name, flag, mydesc, myorg): """Schedule a dns lookup. name dns hostname to lookup flag true if this is the 1st time thru for this dns hostname mydesc additional description information concerning hostile ip myorg blocklist identifier If this is not the 1st time through for this hostname, then it must have come from the wait queue. So decrement the cnt of requests in the wait queue. If there is still room in the current burst of dns lookups, then schedule the actual lookup and bump the activity counter of requests currently running. If there is no room in the current burst pipeline, then send the request back to the wait queue, bump the wait counter, and re-schedule this lookup for later on. """ if cfg.debug >= cfg.DEBUG_VERBOSE: log.msg("wksrv: dns_lkup: {0}, {1} org: {2} |{3}|".format( name, flag, myorg, mydesc )) # if not 1st time call, then decrement wait cnt if (flag): self.num_wait = max(0, self.num_wait-1) if cfg.debug >= cfg.DEBUG_VERBOSE: log.msg("wksrv: dns_lkup: wait cnt:", self.num_wait, name) # if not throttling then kick off next request if self.num_x < self.max_x: self.num_x = self.num_x + 1 if cfg.debug >= cfg.DEBUG_VERBOSE: log.msg("wksrv: dns_lkup: fire another, exec cnt", self.num_x, name) d = getHostByName(name) d.addCallback(self.lookup_done, name, mydesc, myorg) d.addErrback(self.lookup_err, name, mydesc, myorg) else: # make the request wait a bit self.num_wait = self.num_wait + 1 if cfg.debug >= cfg.DEBUG_VERBOSE: log.msg("wksrv: dns_lkup: wait another", self.num_wait, name) # schedule this lookup for later on d = task.deferLater( reactor, (self.max_x + self.num_wait)/self.max_x, self.do_lookup, name, True, mydesc, myorg )