def _load_ips_gethostbyname(): """load ip addresses with socket.gethostbyname_ex This can be slow. """ global LOCALHOST try: LOCAL_IPS[:] = socket.gethostbyname_ex('localhost')[2] except socket.error: # assume common default LOCAL_IPS[:] = ['127.0.0.1'] try: hostname = socket.gethostname() PUBLIC_IPS[:] = socket.gethostbyname_ex(hostname)[2] # try hostname.local, in case hostname has been short-circuited to # loopback if not hostname.endswith('.local') and all(ip.startswith('127') for ip in PUBLIC_IPS): PUBLIC_IPS[:] = socket.gethostbyname_ex( socket.gethostname() + '.local')[2] except socket.error: pass finally: PUBLIC_IPS[:] = uniq_stable(PUBLIC_IPS) LOCAL_IPS.extend(PUBLIC_IPS) # include all-interface aliases: 0.0.0.0 and '' LOCAL_IPS.extend(['0.0.0.0', '']) LOCAL_IPS[:] = uniq_stable(LOCAL_IPS) LOCALHOST = LOCAL_IPS[0]
def __init__(self): self.closeAll = False try: self.host = [ip for ip in socket.gethostbyname_ex(socket.gethostname())[2] if not ip.startswith("127.")][1] #arrumar para todos pcs try except except: self.host = [ip for ip in socket.gethostbyname_ex(socket.gethostname())[2] if not ip.startswith("127.")][0] #pega o IP da máquina self.port = 80 #a porta da conexão é a porta 80 web (sempre aberta, não precisa)
def hostInfo(): hostName = socket.gethostname() print hostName print socket.gethostbyname(hostName) print socket.gethostbyname_ex(hostName) print socket.getfqdn(hostName) print socket.getaddrinfo("www.baidu.com", 80)
def get_source_domain_of_target_sqli_urls(url): import socket import re sqli_url_domain=re.sub(r'(https://)|(http://)|(\s)|(/.*)|(:.*)',"",url) #print sqli_url_domain f=open("targets.txt","r+") targets_list=f.readlines() f.close() all_list=[] for each in targets_list: each=re.sub(r'(https://)|(http://)|(\s)|(/.*)|(:.*)',"",each) domain=[] domain.append(each) try: all_nics_ip=socket.gethostbyname_ex(each)[2] each_list=all_nics_ip+domain all_list.append(each_list) except: pass #print all_list for single_list in all_list: try: sqli_url_ip=socket.gethostbyname_ex(sqli_url_domain)[2] #print sqli_url_ip #print 55555555 #print single_list if sqli_url_ip[0] in single_list: #print 66666666 #print single_list[-1] return single_list[-1] except: pass
def __setitem__(self, key, value): if key == "Stack": self.set_stack(value) elif key == "node-limit": self.data[key] = value self.filter_nodes() elif key == "nodes": self.Nodes = [] for node in value: # I don't think I need the IP address, etc. but this validates # the node name against /etc/hosts and/or DNS, so it's a # GoodThing(tm). try: n = node.strip() if self.data["docker"] == 0: socket.gethostbyname_ex(n) self.Nodes.append(n) except: self.logger.log(node+" not found in DNS... aborting") raise self.filter_nodes() else: self.data[key] = value
def get_remote_machine_info(): remote_host = 'www.079l.com' try: print "IP address: %s" % socket.gethostbyname(remote_host) print socket.gethostbyname_ex(remote_host) except socket.error, err_msg: print "%s: %s" % (remote_host, err_msg)
def connection(self, list, module): print("bla") for i in range(len(list)): for i in range(len(self.addresses)): address = socket.gethostbyname_ex(self.addresses[i][1]) if list[i][1][0] == address[2][0]: return list[i][0] for i in range(len(self.addresses)): print("searching") print(self.addresses[i]) if(self.addresses[i][0] == module): while 1: try: print("adres gevonden") s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect((self.addresses[i][1], int(self.addresses[i][2]))) address = socket.gethostbyname_ex(self.addresses[i][1]) list.append((s, (address[2][0], int(self.addresses[i][2])))) return s except EnvironmentError as exc: if exc.errno == errno.ECONNREFUSED: print 'trying again in 10 seconds\n\r' time.sleep(10) else: raise else: break
def new_site(domain_name): if request.method == 'GET': top_nav = get_top_nav() domain = Domain.get(domain_name) if domain: if domain.website_status == "complete": return redirect(domain.url) else: try: domain_name = utils.sanitize_domain_name(domain_name) socket.gethostbyname_ex(domain_name) domain = Domain.objects.create(domain_name=domain_name) except: return render_template('domain/not_existing.html', **locals()) panno_mascot = '/static/img/panno_mascot/HomePage.png' return render_template('domain/new_site.html', **locals()) else: domain = Domain.get(domain_name, create=True) if domain and domain.website_status == "complete": return redirect(domain.url) response = make_response(redirect("/subscribe/%s" % domain.name)) response.set_cookie("subscription_email", request.form.get("email", "")) return response
def connectRecv(port, networkNum, timeout): # configuration parameters; purpose unknown sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # Bind the socket to the port # if networkNum = 0, connect to relay stations on LAN, if networkNum = 1, connect on Wi-Fi try: name = socket.gethostbyname_ex(socket.gethostname())[-1][networkNum] except: name = socket.gethostbyname_ex(socket.gethostname())[-1][1-networkNum] server_address = (name, port) print >>sys.stderr, 'starting up on %s port %s' % server_address sock.bind(server_address) sock.settimeout(timeout) # Listen for incoming connections sock.listen(1) # Wait for a connection print >>sys.stderr, 'waiting for a connection' try: connection, client_address = sock.accept() except: return None print >>sys.stderr, 'connection from', client_address # make connection nonblocking connection.settimeout(0) return connection
def _load_ips(): """load the IPs that point to this machine This function will only ever be called once. """ global LOCALHOST try: LOCAL_IPS[:] = socket.gethostbyname_ex('localhost')[2] except socket.error: pass try: hostname = socket.gethostname() PUBLIC_IPS[:] = socket.gethostbyname_ex(hostname)[2] # try hostname.local, in case hostname has been short-circuited to loopback if not hostname.endswith('.local') and all(ip.startswith('127') for ip in PUBLIC_IPS): PUBLIC_IPS[:] = socket.gethostbyname_ex(socket.gethostname() + '.local')[2] except socket.error: pass finally: PUBLIC_IPS[:] = uniq_stable(PUBLIC_IPS) LOCAL_IPS.extend(PUBLIC_IPS) # include all-interface aliases: 0.0.0.0 and '' LOCAL_IPS.extend(['0.0.0.0', '']) LOCAL_IPS[:] = uniq_stable(LOCAL_IPS) LOCALHOST = LOCAL_IPS[0]
def Open(self, timeout): if self.socket: return if self.port <= 0: # Create an Unix Data Socket instead of a # normal TCP socket try: socktype = socket.AF_UNIX except AttributeError: logging.error('The system does not support ' + 'Unix Domain Sockets') raise sockaddr = self.address else: # Create a TCP socket socktype = socket.AF_INET sockaddr = (self.address, self.port) # Validate address try: socket.gethostbyname_ex(self.address) except: logging.error('Could not resolve address for host ' + self.address) raise self.socket = socket.socket(socktype, socket.SOCK_STREAM) self.socket.settimeout(timeout) self.socket.connect(sockaddr)
def getmachinetype(): #print "running on host ",myhost if myhost.startswith('ncsrv-'): try: es_cdaq_list_ip = socket.gethostbyname_ex('es-cdaq')[2] es_local_list_ip = socket.gethostbyname_ex('es-local')[2] for es in es_cdaq_list: try: es_cdaq_list_ip.append(socket.gethostbyname_ex(es)[2][0]) except Exception as ex: print ex for es in es_local_list: try: es_local_list_ip.append(socket.gethostbyname_ex(es)[2][0]) except Exception as ex: print ex myaddr = socket.gethostbyname(myhost) if myaddr in es_cdaq_list_ip: return 'es','escdaq','prod' elif myaddr in es_local_list_ip: return 'es','eslocal','prod' else: return 'unknown','unknown' except socket.gaierror, ex: print 'dns lookup error ',str(ex) raise ex
def getmachinetype(): #print "running on host ",myhost if myhost.startswith('dvrubu-') or myhost.startswith('dvfu-') : return 'daq2val','fu' elif myhost.startswith('dvbu-') : return 'daq2val','bu' elif myhost.startswith('fu-') : return 'daq2','fu' elif myhost.startswith('hilton-') : return 'hilton','fu' elif myhost.startswith('bu-') : return 'daq2','bu' elif myhost.startswith('srv-') or myhost.startswith('ncsrv-'): try: es_cdaq_list_ip = socket.gethostbyname_ex('es-cdaq')[2] es_tribe_list_ip = socket.gethostbyname_ex('es-tribe')[2] for es in es_cdaq_list: try: es_cdaq_list_ip.append(socket.gethostbyname_ex(es)[2][0]) except Exception as ex: print ex for es in es_tribe_list: try: es_tribe_list_ip.append(socket.gethostbyname_ex(es)[2][0]) except Exception as ex: print ex myaddr = socket.gethostbyname(myhost) if myaddr in es_cdaq_list_ip: return 'es','escdaq' elif myaddr in es_tribe_list_ip: return 'es','tribe' else: return 'unknown','unknown' except socket.gaierror, ex: print 'dns lookup error ',str(ex) raise ex
def test_gethostbyname_ex(): # 查看服务器更详细的信息 host = 'www.baidu.com' try: print socket.gethostbyname_ex(host) except socket.error: pass
def handleEvent(self, event): eventName = event.eventType srcModuleName = event.module eventData = event.data addrs = None sf.debug("Received event, " + eventName + ", from " + srcModuleName) # Don't look up stuff twice if self.results.has_key(eventData): sf.debug("Skipping " + eventData + " as already resolved.") return None else: self.results[eventData] = True try: if eventName != 'IP_ADDRESS': if '://' in eventData: addrs = socket.gethostbyname_ex(sf.urlFQDN(eventData)) else: addrs = socket.gethostbyname_ex(eventData) else: addrs = socket.gethostbyaddr(eventData) except socket.error as e: sf.info("Unable to resolve " + eventData) return None for addr in addrs: if type(addr) == list: for host in addr: self.processHost(host, event) else: self.processHost(addr, event) return None
def test(self): # We need the version for this try: socket.gethostbyname_ex(self.server)[2][0] except socket.gaierror as e: raise_from(TransportError("Server '%s' does not exist" % self.server), e) return test_credentials(protocol=self)
def __validate_cookie(self, name, param): try: (ipv4, port) = f5bigip_decode(param) socket.gethostbyname_ex(ipv4) return True except Exception as e: print e.message return False
def validate_whitelist(self): """Make sure ip's and domain name can be resolved.""" for addr in self.whitelist: try: socket.gethostbyname_ex(addr) except: raise APIException("'{}' does not look like an ip or domain name".format(addr), 400)
def get_names(self): if FileHandler.names is None: try: FileHandler.names = tuple(socket.gethostbyname_ex('localhost')[2] + socket.gethostbyname_ex(socket.gethostname())[2]) except socket.gaierror: FileHandler.names = (socket.gethostbyname('localhost'),) return FileHandler.names
def getIp(domain): try: (a, b, ips) = socket.gethostbyname_ex(domain) except Exception, msg: try: (a, b, ips) = socket.gethostbyname_ex("www."+domain) except Exception, msg: pass return "0.0.0.0"
def test19(): try: print(socket.gethostbyname_ex('www.baidu.com')) print(socket.gethostbyname_ex('lion2016')) print(socket.gethostbyname_ex('localhost')) print(socket.gethostbyname_ex('nosuchhost')) except socket.error as msg: print(msg) pass
def getLocalIp(): name = socket.getfqdn(socket.gethostname()) addr = socket.gethostbyname(name) print name print socket.gethostbyname(socket.gethostname()) print socket.gethostbyname_ex(socket.gethostname()) return addr
def is_valid_dns(name, dns, **kwargs): """Valid the format of DNS.""" if is_valid_ip(dns): return True try: socket.gethostbyname_ex(dns) except Exception: return False return True
def _checkDomain(self,domain): x = True try: socket.gethostbyname_ex(domain) except socket.gaierror: x = False except socket.error: x = False finally: return x
def find_first_available_server(options): if 'servernames' in options: for s in options['servernames']: try: print(socket.gethostbyname_ex(s)) return socket.gethostbyname_ex(s)[0] except: pass else: return 'scibernetic.de'
def __init__(self): self.enabled = True # dict of (schedd_name,pool_name)=>(cms arg schedd string,env) self.cache = {} self.my_ips = socket.gethostbyname_ex(socket.gethostname())[2] try: self.my_ips += socket.gethostbyname_ex('localhost')[2] except socket.gaierror,e: pass # localhost not defined, ignore
def tail_messages(self, endpoints, topic="", passive=False, **kw): """ Tail messages on the bus. Generator that yields tuples of the form: ``(name, endpoint, topic, message)`` """ # TODO -- the 'passive' here and the 'active' are ambiguous. They # don't actually mean the same thing. This should be resolved. method = passive and 'bind' or 'connect' failed_hostnames = [] subs = {} for _name, endpoint_list in endpoints.iteritems(): for endpoint in endpoint_list: # First, some sanity checking. zeromq will potentially # segfault if we don't do this check. hostname = endpoint.split(':')[1][2:] if hostname in failed_hostnames: continue if hostname != '*': try: socket.gethostbyname_ex(hostname) except: failed_hostnames.append(hostname) log.warn("Couldn't resolve %r" % hostname) continue # OK, sanity checks pass. Create the subscriber and connect. subscriber = self.context.socket(zmq.SUB) subscriber.setsockopt(zmq.SUBSCRIBE, topic) getattr(subscriber, method)(endpoint) subs[endpoint] = subscriber timeout = kw['timeout'] tic = time.time() try: while True: for _name, endpoint_list in endpoints.iteritems(): for e in endpoint_list: if e not in subs: continue try: _topic, message = \ subs[e].recv_multipart(zmq.NOBLOCK) tic = time.time() encoded = fedmsg.encoding.loads(message) yield _name, e, _topic, encoded except zmq.ZMQError: if timeout and (time.time() - tic) > timeout: return finally: for endpoint, subscriber in subs.items(): subscriber.close()
def __InputName(self): "Function that asks for user for the hostname of a system" flag = 0 while not flag: try: hostname = raw_input("Please enter the hostname: ") socket.gethostbyname_ex(hostname) flag = 1 except Exception: print "Unkown hostname, please try again" return hostname
def _test_hostname(hostname): try: import socket except ImportError: logging.warn("Socket module does not exist!") return False try: socket.gethostbyname_ex(hostname) return True except socket.gaierror: return False
def _is_blacklisted(dnsbl_uri, addr): try: reversed_ip = '.'.join(addr.split('.')[::-1]) addr = '.'.join([reversed_ip, dnsbl_uri]) # For a given ip such 1.2.3.4, addr should look like: # 4.3.2.1.dnsbl_uri socket.gethostbyname_ex(addr) return True except (IndexError, socket.error, socket.gaierror, socket.herror, socket.timeout): # Not blacklisted return False
def __init__( self, searchstring, searchtype, nasporttype, timemode, timestamp, timestampslack, days, userdns, nasdns, sortfield, sortorder, ): """ Construct search query from user input """ # Make "*" wildcard character if searchstring: searchstring = searchstring.lower().replace("*", "%") self.userdns = userdns self.nasdns = nasdns self.ips_to_lookup = set() self.query = ( """(SELECT radacctid, acctuniqueid, username, realm, framedipaddress, nasipaddress, nasporttype, acctstarttime, acctstoptime, acctsessiontime, acctoutputoctets, acctinputoctets FROM %s """ % ACCT_TABLE ) if (searchstring and searchstring != "%") or nasporttype or timemode: self.query += " WHERE" # Contains all parameters we want to escape self.parameters = () # Check what we are searching for. It's either a username, realm, # or an IP address/hostname. If searching for all users, skip this # where clause all together. if (searchtype == "username" or searchtype == "realm") and searchstring != "%": self.query += " LOWER(%s) LIKE %%s" % searchtype self.parameters += (searchstring,) # Address if searchtype == "framedipaddress" or searchtype == "nasipaddress": # Split search string into hostname and, if entered, cisco nas # port. match = re.search( r"^(?P<host>[[a-zA-Z0-9\.\-]+)[\:\/]{0,1}" r"(?P<swport>[\S]+){0,1}$", searchstring, ) # Get all ip addresses, if a hostname is entered try: addresses = gethostbyname_ex(match.group("host"))[2] except (AttributeError, gaierror): # AttributeError triggered when regexp found no match, and # thus is None addresses = ["255.255.255.255"] self.query += " (" for address in addresses: self.query += "%s = INET(%%s)" % searchtype self.parameters += (address,) if address != addresses[-1]: self.query += " OR " self.query += ")" # Search for Cisco NAS port, if it has been entered if match.group("swport"): self.query += " AND LOWER(cisconasport) = %s" self.parameters += tuple(match.group("swport").lower().split()) if searchtype == "iprange": if searchstring.find('%'): if re.search('/32', searchstring): self.query += " (%s = INET(%%s) OR %s = INET(%%s))" % ( 'framedipaddress', 'nasipaddress', ) self.parameters += (searchstring[:-3], searchstring[:-3]) else: self.query += " (%s << INET(%%s) OR %s = INET(%%s))" % ( 'framedipaddress', 'nasipaddress', ) self.parameters += (searchstring, searchstring) if nasporttype: if nasporttype.lower() == "isdn": nasporttype = "ISDN" if nasporttype.lower() == "vpn": nasporttype = "Virtual" if nasporttype.lower() == "modem": nasporttype = "Async" if nasporttype.lower() == "dot1x": nasporttype = "Ethernet" else: if searchstring != "%": self.query += " AND " self.query += " nasporttype = %s" self.parameters += (nasporttype,) # Searching for entries in a specified time interval. # # This might be a bit confusing, so I'll try to explain.. # # First, some notes about the accounting entries: # 1) All entries have a date+time in the 'acctstarttime' field. # 2) All entries does not necessarily have an entry in 'acctstoptime' # 3) All entries for sessions that have lasted longer than the # re-authentication-interval, have an integer value in the # 'acctsessiontime' field. # 4) Since the date in 'acctstarttime' is actually just the time # when freeradius received EITHER a Start message, or an Alive # message with a Acct-Unique-Session-Id that wasn't in the # database, a session can have started prior to the 'acctstarttime' # Thus if 'acctstoptime' != NULL, we might have actually gotten a # Stop message with an Acct-Session-Time that tells ut how long the # session has really lasted. We can therefore extract the real # starting time by subtracting 'acctsessiontime' from # 'acctstoptime' # 5) To match entries for sessions that have not yet ended, we have to # add 'acctsessiontime' to 'acctstarttime' # and see if the resulting time interval touches our search # interval. if timemode: # If we have already specified some criteria, we need to AND # it with the date search if self.query.find("WHERE", 0, -5) != -1: self.query += " AND " if timemode == "days": # Search for entries active from x*24 hours ago, until now. searchtime = float(days) * 86400 searchstart = time.strftime( DATEFORMAT_SEARCH, time.localtime(time.time() - searchtime) ) # searchstop = time.strftime(DATEFORMAT_SEARCH, # time.localtime(time.time())) # Ok, let's make this really hairy. We want to separate the # following clauses into two queries, which we then UNION # together. This is done to be able to utilize the indices. # For some reason postgres doesn't use the indices when we # OR all three of these clauses together. tmp_where_clause = "" tmp_where_clause += """ ( ( -- Finding sessions that ended within our interval acctstoptime >= timestamp '%(searchstart)s' ) OR ( -- Finding sessions that started within our interval acctstarttime >= timestamp '%(searchstart)s') ) ) UNION """ % { "searchstart": searchstart } self.query += tmp_where_clause + self.query self.parameters += self.parameters self.query += """ ( -- Find sessions without acctstoptime, but where -- acctstarttime+acctsessiontime is in our interval (acctstoptime is NULL AND (acctstarttime + (acctsessiontime * interval '1 sec' )) >= timestamp '%(searchstart)s') ) """ % { "searchstart": searchstart } if timemode == "timestamp": if timestampslack == "": timestampslack = 0 # Search for entries between (given timestamp - # timestampslack) and (given timestamp + timestampslack) unixtimestamp = time.mktime(time.strptime(timestamp, DATEFORMAT_SEARCH)) searchstart = time.strftime( DATEFORMAT_SEARCH, time.localtime(unixtimestamp - (int(timestampslack) * 60)), ) searchstop = time.strftime( DATEFORMAT_SEARCH, time.localtime(unixtimestamp + (int(timestampslack) * 60)), ) # We pull the same trick here as in the section where # timemode == "days", and UNION two queries together, to # make use of the indices. self.query += """ ( ( -- Finding sessions that ended within our interval acctstoptime BETWEEN timestamp '%(searchstart)s' AND timestamp '%(searchstop)s' ) OR ( -- Finding sessions that started within our interval acctstarttime BETWEEN timestamp '%(searchstart)s' AND timestamp '%(searchstop)s' ) OR ( -- Finding sessions that seems to have started after our -- interval, but where acctsessiontime reveals that our -- registered acctstarttime wasn't the actual starttime (acctstarttime >= timestamp '%(searchstop)s') AND (acctstoptime-(acctsessiontime * interval '1 sec') <= timestamp '%(searchstop)s') ) OR ( -- Finding sessions that started before our interval (acctstarttime <= timestamp '%(searchstop)s') AND ( -- .. and either stopped inside or after our -- interval (acctstoptime >= timestamp '%(searchstart)s') -- .. or where the starttime+acctsessiontime -- reveals that -- this session was active in the interval OR (acctstoptime is NULL AND (acctstarttime+(acctsessiontime * interval '1 sec') >= timestamp '%(searchstart)s') ) ) ) ) """ % { "searchstart": searchstart, "searchstop": searchstop, } self.query += ")" # End select self.query += " ORDER BY %(sortfield)s %(sortorder)s" % { "sortfield": sortfield, "sortorder": sortorder, }
# import the Game Class from game import Game # Flask is used as the websocket in order to communicate with all clients from flask import Flask, render_template from flask_socketio import SocketIO, join_room, emit # thearding is used to run the socketio server in a different thread import threading # socket to get the IP Adresse of the Server import socket import logging # initialize a dicstionary with all games in progress games = {} # start socket on Port 9191 IP = (([ ip for ip in socket.gethostbyname_ex(socket.gethostname())[2] if not ip.startswith("127.") ] or [[(s.connect(("8.8.8.8", 53)), s.getsockname()[0], s.close()) for s in [socket.socket(socket.AF_INET, socket.SOCK_DGRAM)]][0][1]]) + ["no IP found"])[0] app = Flask(__name__) socketio = SocketIO(app) # Define Logger log = logging.getLogger('werkzeug') log.disabled = True app.logger.disabled = True # define event handlers, which indentify the right game and call the respective action @socketio.on('startSession')
import socket import sys try: print("gethostbyname") print(socket.gethostbyname_ex('www.google.com')) print("\ngethostbyaddr") print(socket.gethostbyaddr('216.58.211.228')) print("\ngetfqdn") print(socket.getfqdn('www.google.com')) except socket.error as error: print(str(error)) print("Error de conexión") sys.exit()
# RX packets:463737 errors:0 dropped:0 overruns:0 frame:0 # TX packets:418383 errors:0 dropped:0 overruns:0 carrier:0 # collisions:0 txqueuelen:1000 # RX bytes:264083575 (264.0 MB) TX bytes:110666042 (110.6 MB) # Interrupt:16 Memory:fc500000-fc520000 # #lo Link encap:Local Loopback # inet addr:127.0.0.1 Mask:255.0.0.0 # inet6 addr: ::1/128 Scope:Host # UP LOOPBACK RUNNING MTU:16436 Metric:1 # RX packets:10107 errors:0 dropped:0 overruns:0 frame:0 # TX packets:10107 errors:0 dropped:0 overruns:0 carrier:0 # collisions:0 txqueuelen:0 # RX bytes:788561 (788.5 KB) TX bytes:788561 (788.5 KB) # #$./get_localip.py #host name: quietheart #local name: quietheart #local addr: 127.0.1.1 #ipList: ('quietheart', [], ['127.0.1.1']) import socket host_name = socket.gethostname() local_name = socket.getfqdn(socket.gethostname()) local_addr = socket.gethostbyname(local_name) ip_list = socket.gethostbyname_ex(socket.gethostname()) print "host name:", host_name print "local name:", local_name print "local addr:", local_addr print "ipList:", ip_list
# https://django-debug-toolbar.readthedocs.io/en/latest/installation.html#prerequisites INSTALLED_APPS += ["debug_toolbar"] # noqa F405 # https://django-debug-toolbar.readthedocs.io/en/latest/installation.html#middleware MIDDLEWARE += ["debug_toolbar.middleware.DebugToolbarMiddleware"] # noqa F405 # https://django-debug-toolbar.readthedocs.io/en/latest/configuration.html#debug-toolbar-config DEBUG_TOOLBAR_CONFIG = { "DISABLE_PANELS": ["debug_toolbar.panels.redirects.RedirectsPanel"], "SHOW_TEMPLATE_CONTEXT": True, } # https://django-debug-toolbar.readthedocs.io/en/latest/installation.html#internal-ips INTERNAL_IPS = ["127.0.0.1", "10.0.2.2"] {% if cookiecutter.use_docker == 'y' -%} if env("USE_DOCKER") == "yes": import socket hostname, _, ips = socket.gethostbyname_ex(socket.gethostname()) INTERNAL_IPS += [".".join(ip.split(".")[:-1] + ["1"]) for ip in ips] {%- if cookiecutter.js_task_runner == 'Gulp' %} try: _, _, ips = socket.gethostbyname_ex("node") INTERNAL_IPS.extend(ips) except socket.gaierror: # The node container isn't started (yet?) pass {%- endif %} {%- endif %} # django-extensions # ------------------------------------------------------------------------------ # https://django-extensions.readthedocs.io/en/latest/installation_instructions.html#configuration INSTALLED_APPS += ["django_extensions"] # noqa F405
def tryTld(self, target): try: addrs = socket.gethostbyname_ex(target) self.tldResults[target] = True except BaseException as e: self.tldResults[target] = False
print('Latitude and Longitud: {}').format(latlng) print(hotspotInfo) print(networkInfo) #WRITING EVIDENCE with open(evidenceDir+'/hostpost.json', 'w') as file: json.dump(hotspotInfo, file) with open(evidenceDir+'/network.json', 'w') as file: json.dump(networkInfo, file) #DNSTEST USING TOP ALEXA i = 0 for i in range(0, CrawlNum): try: dnsInfo[alexaTop[i].lower()] = socket.gethostbyname_ex(alexaTop[i].lower()) except Exception as e: print("Error: {}").format(e) #NXDOMAIN AGAINST FAUX DOMAIN faux = ['dom.falso', 'abcxxxxxdsa.com', 'queloooooo.ca', 'casdadascas.com'] wrong = 0 execution = 0 for domain in faux: try: dnsInfo['NXDOMAIN:'+domain] = socket.gethostbyname_ex(domain) wrong += 1 chrome.get('http://'+domain) dnsInfo['NXDOMAIN-Cookies:'+domain] = chrome.get_cookies() dnsInfo['NXDOMAIN-URL'+domain] = chrome.current_url chrome.save_screenshot(evidenceDir+'/NXDOMAIN-' + domain + '.png')
def getIp(): for ip in socket.gethostbyname_ex(socket.gethostname())[2]: if not ip.startswith('127'): return ip return socket.gethostbyname(socket.gethostname())
def hello_process(self, *args, **kwargs): """ The 'hello' action is merely to 'speak' with the server. The server can return current date/time, echo back a string, query the startup command line args, etc. This method is a simple means of checking if the server is "up" and running. :param args: :param kwargs: :return: """ self.dp.qprint("hello_process()", comms='status') b_status = False d_ret = {} d_request = {} for k, v in kwargs.items(): if k == 'request': d_request = v d_meta = d_request['meta'] if 'askAbout' in d_meta.keys(): str_askAbout = d_meta['askAbout'] d_ret['name'] = S.T.cat('/this/name') d_ret['version'] = S.T.cat('/this/version') if str_askAbout == 'timestamp': str_timeStamp = datetime.datetime.today().strftime( '%Y%m%d%H%M%S.%f') d_ret['timestamp'] = {} d_ret['timestamp']['now'] = str_timeStamp b_status = True if str_askAbout == 'sysinfo': d_ret['sysinfo'] = {} d_ret['sysinfo']['system'] = platform.system() d_ret['sysinfo']['machine'] = platform.machine() d_ret['sysinfo']['platform'] = platform.platform() d_ret['sysinfo']['uname'] = platform.uname() d_ret['sysinfo']['version'] = platform.version() d_ret['sysinfo']['memory'] = psutil.virtual_memory() d_ret['sysinfo']['cpucount'] = multiprocessing.cpu_count() d_ret['sysinfo']['loadavg'] = os.getloadavg() d_ret['sysinfo']['cpu_percent'] = psutil.cpu_percent() d_ret['sysinfo']['hostname'] = socket.gethostname() d_ret['sysinfo']['inet'] = [ l for l in ([ ip for ip in socket.gethostbyname_ex( socket.gethostname())[2] if not ip.startswith("127.") ][:1], [[( s.connect(('8.8.8.8', 53)), s.getsockname()[0], s.close()) for s in [ socket.socket(socket.AF_INET, socket.SOCK_DGRAM) ]][0][1]]) if l ][0][0] b_status = True if str_askAbout == 'echoBack': d_ret['echoBack'] = {} d_ret['echoBack']['msg'] = d_meta['echoBack'] b_status = True return {'d_ret': d_ret, 'status': b_status}
def GetLocalIPByPrefix(prefix): localIP = '' for ip in socket.gethostbyname_ex(socket.gethostname())[2]: if ip.startswith(prefix): localIP = ip return localIP
def register(self): """Registers device with the flexiManage. To do that the Fwagent establishes secure HTTP connection to the manager and sends GET request with various data regarding device. When user approves device on manager, the Fwagent establishes secure WebSocket connection to the manager and starts to listen for flexiManage requests. :returns: `True` if registration succeeded, `False` otherwise. """ fwglobals.log.info("registering with flexiWAN orchestrator...") self.register_error = '' if not loadsimulator.g.enabled(): if os.path.exists(fwglobals.g.DEVICE_TOKEN_FILE): fwglobals.log.info( "register: already registered, to refresh run 'fwagent reset' and retry" ) return True try: with open(fwglobals.g.cfg.TOKEN_FILE, 'r') as f: self.token = f.readline() except: err = "register: failed to load token from %s: %s (%s)" % \ (fwglobals.g.cfg.TOKEN_FILE, format(sys.exc_info()[1]), format(sys.exc_info()[0])) fwglobals.log.error(err) return False if fwutils.vpp_does_run(): fwglobals.log.error( "register: router is running, it by 'fwagent stop' and retry by 'fwagent start'" ) return False if loadsimulator.g.enabled(): machine_id = loadsimulator.g.get_generated_machine_id( loadsimulator.g.simulate_id) else: machine_id = fwutils.get_machine_id() if machine_id == None: fwglobals.log.error( "register: get_machine_id failed, make sure you're running in sudo privileges" ) return False machine_name = socket.gethostname() all_ip_list = socket.gethostbyname_ex(machine_name)[2] interfaces = fwglobals.g.handle_request('interfaces') default_route = fwutils.get_default_route() # get up to 4 IPs ip_list = ', '.join(all_ip_list[0:min(4, len(all_ip_list))]) url = fwglobals.g.cfg.MANAGEMENT_URL + "/api/connect/register" data = uparse.urlencode({ 'token': self.token.rstrip(), 'fwagent_version': self.version, 'machine_id': machine_id, 'machine_name': machine_name, 'ip_list': ip_list, 'default_route': default_route[0], 'default_dev': default_route[1], 'interfaces': json.dumps(interfaces['message']) }).encode() req = ureq.Request(url, data) ctx = ssl.create_default_context() if fwglobals.g.cfg.BYPASS_CERT: ctx.check_hostname = False ctx.verify_mode = ssl.CERT_NONE else: ctx.verify_mode = ssl.CERT_REQUIRED try: resp = ureq.urlopen(req, context=ctx) data = resp.read().decode() if loadsimulator.g.enabled(): loadsimulator.g.simulate_device_tokens.append(data) else: with open(fwglobals.g.DEVICE_TOKEN_FILE, 'w') as fout: fout.write(data) fwglobals.log.info("Registation successful with parameters:") fwglobals.log.info(" Hostname: " + machine_name) fwglobals.log.info(" IP List: " + ip_list) fwglobals.log.info(" Device ID: " + machine_id) fwglobals.log.info( "Run connect after approving device in the orchestrator") except uerr.URLError as e: if hasattr(e, 'code'): server_response = e.read().decode() fwglobals.log.error('register: got %s - %s' % (str( e.code), hsvr.BaseHTTPRequestHandler.responses[e.code][0])) fwglobals.log.error('register: Server response: %s' % server_response) try: register_response = json.loads(server_response) if 'error' in register_response: self.register_error = register_response['error'].lower( ) except: pass elif hasattr(e, 'reason'): fwglobals.log.error('register: failed to connect to %s: %s' % (fwglobals.g.cfg.MANAGEMENT_URL, e.reason)) return False except: fwglobals.log.error('register: failed to send request to server %s: %s' % \ (fwglobals.g.cfg.MANAGEMENT_URL, format(sys.exc_info()[1]))) return False return True
# django-debug-toolbar # ------------------------------------------------------------------------------ # https://django-debug-toolbar.readthedocs.io/en/latest/installation.html#prerequisites INSTALLED_APPS += ["debug_toolbar"] # noqa F405 # https://django-debug-toolbar.readthedocs.io/en/latest/installation.html#middleware MIDDLEWARE += ["debug_toolbar.middleware.DebugToolbarMiddleware"] # noqa F405 # https://django-debug-toolbar.readthedocs.io/en/latest/configuration.html#debug-toolbar-config DEBUG_TOOLBAR_CONFIG = { "DISABLE_PANELS": ["debug_toolbar.panels.redirects.RedirectsPanel"], "SHOW_TEMPLATE_CONTEXT": True, } # https://django-debug-toolbar.readthedocs.io/en/latest/installation.html#internal-ips INTERNAL_IPS = ["127.0.0.1", "10.0.2.2"] if env("USE_DOCKER") == "yes": import socket hostname, _, ips = socket.gethostbyname_ex(socket.gethostname()) INTERNAL_IPS += [ip[:-1] + "1" for ip in ips] # django-extensions # ------------------------------------------------------------------------------ # https://django-extensions.readthedocs.io/en/latest/installation_instructions.html#configuration INSTALLED_APPS += ["django_extensions"] # noqa F405 # Celery # ------------------------------------------------------------------------------ # http://docs.celeryproject.org/en/latest/userguide/configuration.html#task-eager-propagates CELERY_TASK_EAGER_PROPAGATES = True # Your stuff... # ------------------------------------------------------------------------------
args = parser.parse_args() port = 443 # Read our input file (sample-request.txt by default) with open(args.payload, 'rb') as f: request = f.read() # Change this to False to run the script forever REQUEST_PRINT_TEMPLATE = "Request {} {}" run_forever = args.forever request_count = 1 # Get all the ips assocated with hostname hostname, aliaslist, ipaddrlist = socket.gethostbyname_ex(args.target) counts = Counter() def signal_handler(signal, frame): print("Requests:") print(counts) sys.exit(0) signal.signal(signal.SIGINT, signal_handler) signal.signal(signal.SIGTERM, signal_handler) while True: for ip in ipaddrlist:
def GetMachineIPs(): if isMac(): return socket.gethostbyname("localhost") else: return socket.gethostbyname_ex(socket.gethostname())[2]
# ! /usr/bin/env python # -*- coding: utf-8 -*- # __author__ = "liuluyang" # Datetime: 2019/12/10 13:54 from tkinter import * from tkinter import font import threading import time import socket hostname = socket.gethostname() host_list = socket.gethostbyname_ex(hostname) ip = [ip for ip in host_list[-1] if '192.168.1.' in ip][0] ADDRESS = (ip, 1902) udp = socket.socket(type=socket.SOCK_DGRAM) udp.bind(ADDRESS) for i in range(1, 255): udp.sendto('上线'.encode(), ('192.168.1.%s' % (i), 1902)) # udp.sendto('你好'.encode(), ADDRESS) root = Tk() root.title("群聊") root.geometry("800x600+100+100") # 滚动条设置 s1 = Scrollbar(root) s1.pack(side=RIGHT, fill=Y) # HORIZONTAL 设置水平方向的滚动条,默认是竖直
def showGameOverScreen(crashInfo): selfHOST = [ l for l in ([ ip for ip in socket.gethostbyname_ex(socket.gethostname())[2] if not ip.startswith("127.") ][:1], [[(s.connect(('8.8.8.8', 53)), s.getsockname()[0], s.close()) for s in [socket.socket(socket.AF_INET, socket.SOCK_DGRAM)] ][0][1]]) if l ][0][0] """crashes the player down ans shows gameover image""" score = crashInfo['score'] playerx = SCREENWIDTH * 0.2 playery = crashInfo['y'] playerHeight = IMAGES['player'][0].get_height() playerVelY = crashInfo['playerVelY'] playerAccY = 2 playerRot = crashInfo['playerRot'] playerVelRot = 7 gameMode = crashInfo['gameMode'] udpPortNumber = 44444 basex = crashInfo['basex'] upperPipes, lowerPipes = crashInfo['upperPipes'], crashInfo['lowerPipes'] # play hit and die sounds SOUNDS['hit'].play() if not crashInfo['groundCrash']: SOUNDS['die'].play() while True: for event in pygame.event.get(): if event.type == QUIT or (event.type == KEYDOWN and event.key == K_ESCAPE): pygame.quit() sys.exit() if event.type == KEYDOWN and (event.key == K_SPACE or event.key == K_UP): if playery + playerHeight >= BASEY - 1: return # player y shift if playery + playerHeight < BASEY - 1: playery += min(playerVelY, BASEY - playery - playerHeight) # player velocity change if playerVelY < 15: playerVelY += playerAccY # rotate only when it's a pipe crash if not crashInfo['groundCrash']: if playerRot > -90: playerRot -= playerVelRot # draw sprites SCREEN.blit(IMAGES['background'], (0, 0)) for uPipe, lPipe in zip(upperPipes, lowerPipes): SCREEN.blit(IMAGES['pipe'][0], (uPipe['x'], uPipe['y'])) SCREEN.blit(IMAGES['pipe'][1], (lPipe['x'], lPipe['y'])) SCREEN.blit(IMAGES['base'], (basex, BASEY)) showScore(score) if gameMode == 1: ## BROADCAST && wait for players number to be atleast 2 server = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP) server.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1) server.settimeout(0.2) server.bind(("", udpPortNumber)) message = "I'm Done" server.sendto(message.encode(), ('<broadcast>', 37020)) # time.sleep(1) client = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # UDP client.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1) client.bind(("", 37020)) data, addr = client.recvfrom(1024) address = str(addr) address = address.replace('(', '') address = address.replace(')', '') tokens = [] tokens = re.split(",", address, 2) opponentHost = tokens[0] opponentHost = opponentHost.lstrip('\'') opponentHost = opponentHost.rstrip('\'') TCPportNumber = tokens[1] TCPportNumber = tokens[1].replace(' ', '') TCPportNumber = int(TCPportNumber) print(TCPportNumber) data = data.decode() if data == "I'm Done": ackMessage = "LETS SHOW SCORES" time.sleep(1) server.sendto(ackMessage.encode(), ('<broadcast>', 37020)) print("tcp") with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as playerServerSocket: playerServerSocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) playerServerSocket.bind((selfHOST, TCPportNumber)) playerServerSocket.listen() conn, addr = playerServerSocket.accept() with conn: opponentScore = int(conn.recv( 1024).decode()) # Receive LAST OPPONENT SCORE UPDATE playerServerSocket.close() time.sleep(0.01) # wait for the server to get set. with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as playerSocket: playerSocket.connect((opponentHost, TCPportNumber)) SCORE = str(score) playerSocket.sendall(SCORE.encode()) playerSocket.close() elif data == "LETS SHOW SCORES": #send TCP then receices print("TCP") time.sleep(0.01) # wait for the server to get set. with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as playerSocket: playerSocket.connect((opponentHost, TCPportNumber)) SCORE = str(score) playerSocket.sendall(SCORE.encode()) playerSocket.close() with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as playerServerSocket: playerServerSocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) playerServerSocket.bind((selfHOST, TCPportNumber)) playerServerSocket.listen() conn, addr = playerServerSocket.accept() with conn: opponentScore = int(conn.recv( 1024).decode()) # Receive LAST OPPONENT SCORE UPDATE playerServerSocket.close() playerSurface = pygame.transform.rotate(IMAGES['player'][1], playerRot) SCREEN.blit(playerSurface, (playerx, playery)) if gameMode == 1: showOppnentScore(opponentScore) if score < opponentScore: SCREEN.blit(IMAGES['gameover'], (50, 180)) elif score > opponentScore: SCREEN.blit(IMAGES['WINNER'], (50, 180)) else: SCREEN.blit(IMAGES['DRAW'], (50, 180)) else: SCREEN.blit(IMAGES['gameover'], (50, 180)) FPSCLOCK.tick(FPS) pygame.display.update()
def retrieve_ip(): return ([l for l in ([ip for ip in socket.gethostbyname_ex(socket.gethostname())[2] if not ip.startswith("127.")][:1], [[(s.connect(('8.8.8.8', 53)),s.getsockname()[0], s.close()) for s in [socket.socket(socket.AF_INET, socket.SOCK_DGRAM)]][0][1]]) if l][0][0])
def get_ips_for_host(host): try: ips = gethostbyname_ex(host) print(ips) except gaierror: print('Error')
test_dir = 'test' test_content = 'foo\nbar\nbaz' test_fname = 'foo.txt' test_root_osfs = 'osfs_local' test_url_s3 = 'http://127.0.0.1/' test_port_s3 = '9000' test_host_smb_docker_share = socket.gethostbyname(socket.gethostname()) test_hostname_smb_docker_share = 'TESTNET' test_name_port_smb_docker_share = 3669 test_direct_tcp_smb_os_share = False test_host_smb_os_share = socket.gethostbyname_ex(socket.gethostname())[2][-1] test_smb_port_smb_os_share = 139 _test_file_model = { 'content': test_content, 'format': 'text', 'mimetype': 'text/plain', 'name': test_fname, 'path': test_fname, 'type': 'file', 'writable': True, } class _TestBase: """Contains tests universal to all PyFilesystemContentsManager flavors
def get_ip_address(): return [ ip for ip in socket.gethostbyname_ex(socket.gethostname())[2] if not ip.startswith("127.") ][0]
host_ports[:] = [] datas[:] = [] products[:] = [] scan_services[:] = [] web_tech.clear() isp = "" asn = "" has_sec_system = 0 domain = 0 device_type = "" try: isdomain = socket.gethostbyname_ex(line[2]) if isdomain[0] == isdomain[2][0]: print("\n[ \x1B[1m+\x1B[0m ] Setting host to: {}{}{}".format(YEL,line[2],NRM)) ip = isdomain[0] host = line[2] else: print("\n[ \x1B[1m+\x1B[0m ] Setting host to: {}{}{} as a domain name".format(YEL,line[2],NRM)) ip = isdomain[2][0] host = line[2] domain = 1 except: print("\n[ \x1B[1m\x1B[31mERROR\x1B[0m ] \"{}{}{}\" not appear be a valid domain or ip address".format(RED,line[2],NRM)) elif line[1] == "workmode":
"data.video.iqiyi.com", "data.video.qiyi.com", "www.iqiyi.com", "control-i.iqiyi.com", "qiqu.iqiyi.com", "search.video.iqiyi.com", "pub.m.iqiyi.com", "cache.video.iqiyi.com", "cache.video.qiyi.com", "cache.vip.iqiyi.com", "cache.vip.qiyi.com", "api.vip.iqiyi.com", "qosp.iqiyi.com", "iface.iqiyi.com", "iface2.iqiyi.com", "iplocation.geo.qiyi.com", "iplocation.geo.iqiyi.com" ] #get current records in file records = set(line.strip() for line in open('hostname.txt')) newRecords = set() #init indent for easier copy paste in ssh wrapper = textwrap.TextWrapper(initial_indent='\t', subsequent_indent='\t') #get turble from given hostnames for url in urls: try: name, aliases, addresses = socket.gethostbyname_ex(url) #check the ip is not existed in file, then add for ip in addresses: newRecords.add(ip) except: print(url + " not get resolved") #remove duplicates (convert dict to auto remove) newRecords = list(dict.fromkeys(newRecords)) #write (sort list while looping) with open('hostname.txt', 'w') as file: for record in sorted(newRecords): wrapped = wrapper.fill("list wan_fw_ips") file.write(wrapped + " '" + record + "'" + '\n')
def _syncResources(self): # resources in the DB now resourcesIn = self.rsDB.getMonitoredsList('Resource', paramsList=['ResourceName']) resourcesIn = [r[0] for r in resourcesIn] # services in the DB now servicesIn = self.rsDB.getMonitoredsList('Service', paramsList=['ServiceName']) servicesIn = [s[0] for s in servicesIn] # Site-CE mapping in CS now siteCE = getSiteCEMapping('LCG')['Value'] # Site-SE mapping in CS now siteSE = getSiteSEMapping('LCG')['Value'] # CEs in CS now CEList = [] for i in siteCE.values(): for ce in i: if ce is None: continue CEList.append(ce) # SEs in CS now SEList = [] for i in siteSE.values(): for x in i: SEList.append(x) # SE Nodes in CS now SENodeList = [] for SE in SEList: node = getSENodes(SE)['Value'][0] if node is None: continue if node not in SENodeList: SENodeList.append(node) # LFC Nodes in CS now LFCNodeList_L = [] LFCNodeList_C = [] for site in getLFCSites()['Value']: for readable in ('ReadOnly', 'ReadWrite'): LFCNode = getLFCNode(site, readable)['Value'] if LFCNode is None or LFCNode == []: continue LFCNode = LFCNode[0] if readable == 'ReadWrite': if LFCNode not in LFCNodeList_C: LFCNodeList_C.append(LFCNode) elif readable == 'ReadOnly': if LFCNode not in LFCNodeList_L: LFCNodeList_L.append(LFCNode) # FTS Nodes in CS now FTSNodeList = [] sitesWithFTS = getFTSSites() for site in sitesWithFTS['Value']: fts = getFTSEndpoint(site)['Value'] if fts is None or fts == []: continue fts = fts[0] if fts not in FTSNodeList: FTSNodeList.append(fts) # VOMS Nodes in CS now VOMSNodeList = getVOMSEndpoints()['Value'] # complete list of resources in CS now resourcesList = CEList + SENodeList + LFCNodeList_L + LFCNodeList_C + FTSNodeList + VOMSNodeList # list of services in CS now (to be done) servicesList = [] #remove resources no more in the CS for res in resourcesIn: if res not in resourcesList: self.rsDB.removeResource(res) self.rsDB.removeStorageElement(resourceName=res) # add to DB what is in CS now and wasn't before # CEs for site in siteCE.keys(): if site == 'LCG.Dummy.ch': continue for ce in siteCE[site]: if ce is None: continue siteInGOCDB = self.GOCDBClient.getServiceEndpointInfo( 'hostname', ce) if not siteInGOCDB['OK']: raise RSSException, siteInGOCDB['Message'] if siteInGOCDB['Value'] == []: trueName = socket.gethostbyname_ex(ce)[0] siteInGOCDB = self.GOCDBClient.getServiceEndpointInfo( 'hostname', trueName) try: siteInGOCDB = siteInGOCDB['Value'][0]['SITENAME'] except IndexError: continue serviceType = 'Computing' service = serviceType + '@' + site if service not in servicesList: servicesList.append(service) if service not in servicesIn: self.rsDB.addOrModifyService( service, serviceType, site, 'Active', 'init', datetime.datetime.utcnow().replace(microsecond=0), 'RS_SVC', datetime.datetime(9999, 12, 31, 23, 59, 59)) servicesIn.append(service) if ce not in resourcesIn: CEType = getCEType(site, ce)['Value'] ceType = 'CE' if CEType == 'CREAM': ceType = 'CREAMCE' self.rsDB.addOrModifyResource( ce, ceType, serviceType, site, siteInGOCDB, 'Active', 'init', datetime.datetime.utcnow().replace(microsecond=0), 'RS_SVC', datetime.datetime(9999, 12, 31, 23, 59, 59)) resourcesIn.append(ce) # SRMs for srm in SENodeList: siteInGOCDB = self.GOCDBClient.getServiceEndpointInfo( 'hostname', srm) if not siteInGOCDB['OK']: raise RSSException, siteInGOCDB['Message'] if siteInGOCDB['Value'] == []: trueName = socket.gethostbyname_ex(srm)[0] siteInGOCDB = self.GOCDBClient.getServiceEndpointInfo( 'hostname', trueName) try: siteInGOCDB = siteInGOCDB['Value'][0]['SITENAME'] except IndexError: continue siteInDIRAC = getDIRACSiteName(siteInGOCDB) if not siteInDIRAC['OK']: raise RSSException, siteInDIRAC['Message'] sites = siteInDIRAC['Value'] serviceType = 'Storage' for site in sites: service = serviceType + '@' + site if service not in servicesList: servicesList.append(service) if service not in servicesIn: self.rsDB.addOrModifyService( service, serviceType, site, 'Active', 'init', datetime.datetime.utcnow().replace(microsecond=0), 'RS_SVC', datetime.datetime(9999, 12, 31, 23, 59, 59)) servicesIn.append(service) if srm not in resourcesIn and srm is not None: self.rsDB.addOrModifyResource( srm, 'SE', serviceType, 'NULL', siteInGOCDB, 'Active', 'init', datetime.datetime.utcnow().replace(microsecond=0), 'RS_SVC', datetime.datetime(9999, 12, 31, 23, 59, 59)) resourcesIn.append(srm) # LFC_C for lfc in LFCNodeList_C: siteInGOCDB = self.GOCDBClient.getServiceEndpointInfo( 'hostname', lfc) if not siteInGOCDB['OK']: raise RSSException, siteInGOCDB['Message'] if siteInGOCDB['Value'] == []: trueName = socket.gethostbyname_ex(lfc)[0] siteInGOCDB = self.GOCDBClient.getServiceEndpointInfo( 'hostname', trueName) try: siteInGOCDB = siteInGOCDB['Value'][0]['SITENAME'] except IndexError: continue siteInDIRAC = getDIRACSiteName(siteInGOCDB) if not siteInDIRAC['OK']: raise RSSException, siteInDIRAC['Message'] sites = siteInDIRAC['Value'] serviceType = 'Storage' for site in sites: service = serviceType + '@' + site if service not in servicesList: servicesList.append(service) if service not in servicesIn: self.rsDB.addOrModifyService( service, serviceType, site, 'Active', 'init', datetime.datetime.utcnow().replace(microsecond=0), 'RS_SVC', datetime.datetime(9999, 12, 31, 23, 59, 59)) servicesIn.append(service) if lfc not in resourcesIn and lfc is not None: self.rsDB.addOrModifyResource( lfc, 'LFC_C', serviceType, 'NULL', siteInGOCDB, 'Active', 'init', datetime.datetime.utcnow().replace(microsecond=0), 'RS_SVC', datetime.datetime(9999, 12, 31, 23, 59, 59)) resourcesIn.append(lfc) # LFC_L for lfc in LFCNodeList_L: siteInGOCDB = self.GOCDBClient.getServiceEndpointInfo( 'hostname', lfc) if not siteInGOCDB['OK']: raise RSSException, siteInGOCDB['Message'] if siteInGOCDB['Value'] == []: trueName = socket.gethostbyname_ex(lfc)[0] siteInGOCDB = self.GOCDBClient.getServiceEndpointInfo( 'hostname', trueName) try: siteInGOCDB = siteInGOCDB['Value'][0]['SITENAME'] except IndexError: continue siteInDIRAC = getDIRACSiteName(siteInGOCDB) if not siteInDIRAC['OK']: raise RSSException, siteInDIRAC['Message'] sites = siteInDIRAC['Value'] serviceType = 'Storage' for site in sites: service = serviceType + '@' + site if service not in servicesList: servicesList.append(service) if service not in servicesIn: self.rsDB.addOrModifyService( service, serviceType, site, 'Active', 'init', datetime.datetime.utcnow().replace(microsecond=0), 'RS_SVC', datetime.datetime(9999, 12, 31, 23, 59, 59)) servicesIn.append(service) if lfc not in resourcesIn and lfc is not None: self.rsDB.addOrModifyResource( lfc, 'LFC_L', serviceType, 'NULL', siteInGOCDB, 'Active', 'init', datetime.datetime.utcnow().replace(microsecond=0), 'RS_SVC', datetime.datetime(9999, 12, 31, 23, 59, 59)) resourcesIn.append(lfc) # FTSs for fts in FTSNodeList: siteInGOCDB = self.GOCDBClient.getServiceEndpointInfo( 'hostname', fts) if not siteInGOCDB['OK']: raise RSSException, siteInGOCDB['Message'] if siteInGOCDB['Value'] == []: trueName = socket.gethostbyname_ex(fts)[0] siteInGOCDB = self.GOCDBClient.getServiceEndpointInfo( 'hostname', trueName) try: siteInGOCDB = siteInGOCDB['Value'][0]['SITENAME'] except IndexError: continue siteInDIRAC = getDIRACSiteName(siteInGOCDB) if not siteInDIRAC['OK']: raise RSSException, siteInDIRAC['Message'] sites = siteInDIRAC['Value'] serviceType = 'Storage' for site in sites: service = serviceType + '@' + site if service not in servicesList: servicesList.append(service) if service not in servicesIn: self.rsDB.addOrModifyService( service, serviceType, site, 'Active', 'init', datetime.datetime.utcnow().replace(microsecond=0), 'RS_SVC', datetime.datetime(9999, 12, 31, 23, 59, 59)) servicesIn.append(service) if fts not in resourcesIn and fts is not None: self.rsDB.addOrModifyResource( fts, 'FTS', serviceType, 'NULL', siteInGOCDB, 'Active', 'init', datetime.datetime.utcnow().replace(microsecond=0), 'RS_SVC', datetime.datetime(9999, 12, 31, 23, 59, 59)) resourcesIn.append(fts) # VOMSs for voms in VOMSNodeList: siteInGOCDB = self.GOCDBClient.getServiceEndpointInfo( 'hostname', voms) if not siteInGOCDB['OK']: raise RSSException, siteInGOCDB['Message'] if siteInGOCDB['Value'] == []: trueName = socket.gethostbyname_ex(voms)[0] siteInGOCDB = self.GOCDBClient.getServiceEndpointInfo( 'hostname', trueName) try: siteInGOCDB = siteInGOCDB['Value'][0]['SITENAME'] except IndexError: continue siteInDIRAC = getDIRACSiteName(siteInGOCDB) if not siteInDIRAC['OK']: raise RSSException, siteInDIRAC['Message'] site = siteInDIRAC['Value'] serviceType = 'VOMS' for site in sites: service = serviceType + '@' + site if service not in servicesList: servicesList.append(service) if service not in servicesIn: self.rsDB.addOrModifyService( service, serviceType, site, 'Active', 'init', datetime.datetime.utcnow().replace(microsecond=0), 'RS_SVC', datetime.datetime(9999, 12, 31, 23, 59, 59)) servicesIn.append(service) if voms not in resourcesIn and voms is not None: self.rsDB.addOrModifyResource( voms, 'VOMS', serviceType, 'NULL', siteInGOCDB, 'Active', 'init', datetime.datetime.utcnow().replace(microsecond=0), 'RS_SVC', datetime.datetime(9999, 12, 31, 23, 59, 59)) resourcesIn.append(voms) #remove services no more in the CS for ser in servicesIn: if ser not in servicesList: serType = ser.split('@')[0] if serType != 'VO-BOX': self.rsDB.removeService(ser) self.rsDB.removeResource(serviceName=ser) site = ser.split('@')[1] if serType == 'Storage': self.rsDB.removeStorageElement(siteName=site)
#----------------------------------------------------------------------------- # Imports #----------------------------------------------------------------------------- import socket from .data import uniq_stable #----------------------------------------------------------------------------- # Code #----------------------------------------------------------------------------- LOCAL_IPS = [] try: LOCAL_IPS = socket.gethostbyname_ex('localhost')[2] except socket.error: pass PUBLIC_IPS = [] try: hostname = socket.gethostname() PUBLIC_IPS = socket.gethostbyname_ex(hostname)[2] # try hostname.local, in case hostname has been short-circuited to loopback if not hostname.endswith('.local') and all( ip.startswith('127') for ip in PUBLIC_IPS): PUBLIC_IPS = socket.gethostbyname_ex(socket.gethostname() + '.local')[2] except socket.error: pass else:
send_receive.set_routing_manager(routing_manager) send_receive.set_packet_manager(packet_manager) keyboard = Keyboard(send_receive) keyboard.daemon = True #parse neighbours from command line try: if (len(sys.argv) >= 8 and len(sys.argv) % 3 == 2): for i in range(5, len(sys.argv), 3): host = sys.argv[i] port = sys.argv[i + 1] longid = sys.argv[i + 2] print("adding neighbour host:" + host + " port:" + port + " longid:" + longid) ipaddress = socket.gethostbyname_ex(host) longidbytes = bytes().fromhex(longid) if (len(longidbytes) == 8): host_port = (host, int(port)) send_receive.neighbours[longidbytes] = host_port routing_manager.add_neighbour(host_port, longidbytes) else: print("longid must be 8 bytes:" + sys.argv[i + 2]) keyboard.start() #start the main loop send_receive.start() except Exception as error: send_receive.exit()
else: # try to get the container name from the docker file cli = Client(base_url='unix://var/run/docker.sock') container_prefix = None for c in cli.containers(): if c["Id"].startswith(socket.gethostname()): container_prefix = c["Names"][-1].split("_", 1)[0].lstrip("/") break hostname_prefix = container_prefix + "_" if container_prefix else "" hosts = ["%sweb_1" % hostname_prefix] while True: try: hostname = "%sweb_%d" % (hostname_prefix, entry_counter) socket.gethostbyname_ex(hostname) hosts.append(hostname) entry_counter += 1 except: # host not found, that's it break with open(os.path.join("/etc/nginx/template/default.template.conf")) as f: content = f.read() server_list = "\n".join([ "\tserver %s:8000 max_fails=5 fail_timeout=15s;" % host for host in hosts ])
def refresh_jobs(configuration, protocol): """Refresh job keys based on the job state. Add user entries for all active job keys. Removes all the user entries for jobs no longer active. Returns a tuple with the daemon_conf and the list of changed job IDs. NOTE: Deprecated due to severe system load, use refresh_job_creds instead """ changed_jobs = [] conf = configuration.daemon_conf logger = conf.get("logger", logging.getLogger()) creds_lock = conf.get('creds_lock', None) if creds_lock: creds_lock.acquire() old_usernames = [i.username for i in conf['jobs']] if creds_lock: creds_lock.release() cur_usernames = [] if not protocol in ('sftp', ): logger.error("invalid protocol: %s" % protocol) return (conf, changed_jobs) for link_name in os.listdir(configuration.sessid_to_mrsl_link_home): link_path = os.path.join(configuration.sessid_to_mrsl_link_home, link_name) job_dict = None if os.path.islink(link_path) and link_path.endswith('.mRSL') and \ os.path.exists(link_path): sessionid = link_name[:-5] job_dict = unpickle(link_path, logger) # We only allow connections from executing jobs that # has a public key if job_dict is not None and isinstance(job_dict, dict) and \ job_dict.has_key('STATUS') and \ job_dict['STATUS'] == 'EXECUTING' and \ job_dict.has_key('SESSIONID') and \ job_dict['SESSIONID'] == sessionid and \ job_dict.has_key('USER_CERT') and \ job_dict.has_key('MOUNT') and \ job_dict.has_key('MOUNTSSHPUBLICKEY'): user_alias = sessionid user_dir = client_id_dir(job_dict['USER_CERT']) user_key = job_dict['MOUNTSSHPUBLICKEY'] user_ip = None # Use frontend proxy if available # otherwise use hosturl to resolve IP user_url = job_dict['RESOURCE_CONFIG'].get('FRONTENDPROXY', '') if user_url: user_url = job_dict['RESOURCE_CONFIG'].get('HOSTURL', '') try: user_ip = socket.gethostbyname_ex(user_url)[2][0] except Exception, exc: user_ip = None msg = "Skipping key due to unresolvable ip" \ + " for user %s (%s)" % (user_alias, exc) logger.warning(msg) # Make sure pub key is valid valid_pubkey = True try: _ = parse_pub_key(user_key) except Exception, exc: valid_pubkey = False logger.warning("Skipping broken key '%s' for user %s (%s)" % (user_key, user_alias, exc)) if user_ip is not None and valid_pubkey: add_job_object(configuration, user_alias, user_dir, pubkey=user_key, ip_addr=user_ip) cur_usernames.append(user_alias) changed_jobs.append(user_alias)
def refresh_job_creds(configuration, protocol, username): """Reload job credentials for username (SESSIONID) if they changed on disk. That is, add user entries in configuration.daemon_conf['jobs'] for any corresponding active job keys. Removes all job login entries if the job is no longer active, too. The protocol argument specifies which auth files to use. Returns a tuple with the updated daemon_conf and the list of changed job IDs. """ changed_jobs = [] conf = configuration.daemon_conf last_update = conf['time_stamp'] logger = conf.get("logger", logging.getLogger()) creds_lock = conf.get('creds_lock', None) if not protocol in ('sftp',): logger.error("invalid protocol: %s" % protocol) return (conf, changed_jobs) if not possible_job_id(configuration, username): # logger.debug("ruled out %s as a possible job ID" % username) return (conf, changed_jobs) link_path = os.path.join(configuration.sessid_to_mrsl_link_home, "%s.mRSL" % username) # logger.debug("Updating job creds for %s" % username) changed_paths = get_job_changes(conf, username, link_path) if not changed_paths: # logger.debug("No job creds changes for %s" % username) return (conf, changed_jobs) job_dict = None if os.path.islink(link_path) and os.path.exists(link_path) and \ last_update < os.path.getmtime(link_path): sessionid = username job_dict = unpickle(link_path, logger) # We only allow connections from executing jobs that # has a public key if job_dict is not None and isinstance(job_dict, dict) and \ job_dict.has_key('STATUS') and \ job_dict['STATUS'] == 'EXECUTING' and \ job_dict.has_key('SESSIONID') and \ job_dict['SESSIONID'] == sessionid and \ job_dict.has_key('USER_CERT') and \ job_dict.has_key('MOUNT') and \ job_dict.has_key('MOUNTSSHPUBLICKEY'): user_alias = sessionid user_dir = client_id_dir(job_dict['USER_CERT']) user_key = job_dict['MOUNTSSHPUBLICKEY'] user_ip = None # Use frontend proxy if available otherwise use hosturl to resolve IP user_url = job_dict['RESOURCE_CONFIG'].get('FRONTENDPROXY', '') if not user_url: user_url = job_dict['RESOURCE_CONFIG'].get('HOSTURL', '') try: user_ip = socket.gethostbyname_ex(user_url)[2][0] except Exception, exc: user_ip = None logger.warning("Skipping key, unresolvable ip for user %s (%s)" % (user_alias, exc)) # Make sure pub key is valid valid_pubkey = True try: _ = parse_pub_key(user_key) except Exception, exc: valid_pubkey = False logger.warning("Skipping broken key '%s' for user %s (%s)" % (user_key, user_alias, exc))
@staticmethod def error(val): print(val) address = sys.argv[1] record_type = sys.argv[2] result = iptools.resolve_domain_with_dns(address) print(result) if record_type == 'A': try: print('\n\n' + 'socket.gethostbyname_ex') hostname, aliaslist, ipaddrlist = socket.gethostbyname_ex(address) print('hostname:%s, aliaslist:%s, ipaddrlist:%s' % (hostname, aliaslist, ipaddrlist)) except Exception as e: print('Could not retrieve by socket.gethostbyname_ex: %s' % (str(e))) traceback.print_exc(file=sys.stdout) else: print('\n\n' + 'socket.gethostbyname_ex') print('Only [A] (IPv4) record types can use socket.gethostbyname_ex') try: print('\n\n' + 'dns.resolver.query') ip_list = [] result = dns.resolver.query(address, record_type) for entry in result: ip_list.append(entry)
def _get_alias_records(hostname): """Return all IPv4 A records for a given hostname """ return socket.gethostbyname_ex(hostname)[2]