def set_up_mocks(self, su=None): self.mox.StubOutWithMock(dirutil, 'safe_mkdtemp') dirutil.safe_mkdtemp().AndReturn('/tmp/test') self.mox.StubOutWithMock(log, 'init') log.init('/tmp/test/current_run').AndReturn(0) self.mox.StubOutWithMock(CommandUtil, 'execute_and_get_output') stub = CommandUtil.execute_and_get_output(['git','remote', '-v']) stub.AndReturn((0, dedent("""origin https://git.twitter.biz/science (fetch) origin https://git.twitter.biz/science (push)"""))) stub2 = CommandUtil.execute_and_get_output(['git','rev-parse', '--abbrev-ref', 'HEAD']) stub2.AndReturn((0,"test_br")) self.mox.StubOutWithMock(psutil, 'cpu_percent') psutil.cpu_percent(interval=1).AndReturn(1.0) self.mox.StubOutWithMock(psutil, 'network_io_counters') psutil.network_io_counters().AndReturn("1000,10000,1000") self.mox.StubOutWithMock(psutil, 'NUM_CPUS') psutil.NUM_CPUS = 5 self.mox.StubOutWithMock(socket, 'gethostname') socket.gethostname().AndReturn("localhost") self.mox.StubOutWithMock(socket, 'gethostbyname') socket.gethostbyname("localhost").AndReturn("localhost") self.mox.StubOutWithMock(sys, 'exit') sys.exit(0).AndReturn(0) self.mox.ReplayAll()
def get_default_bind_host(): try: gethostbyname('localhost') except gaierror: log.warning("localhost seems undefined in your hosts file, using 127.0.0.1 instead") return '127.0.0.1' return 'localhost'
def parse_address(local_address, str): check.check_is_af_inet_address(local_address) #=@R19 try: match = re.match(r"\s*(.+)\s*:\s*([0-9]+)\s*$", str) if match: ret = (socket.gethostbyname(match.group(1)), string.atoi(match.group(2))) else: match = re.match(r"\s*([0-9]+)\s*$", str) if match: ret = (local_address[0], string.atoi(match.group(1))) else: ret = (socket.gethostbyname(string.strip(str)), settings.default_ports[0]) check.check_is_af_inet_address(ret) #=@E9 # Proof: socket.gethostbyname returns a string, atoi returns an int, # settings.default_ports is a non-empty list of integers. # local_address is itself an af_inet_address (@R19), so its first # element is a string suitable for an af_inet_address. local_address # is deeply immutable from @E11. return ret except: raise error.Error(_("Could not find peer.\n"+\ "Please give a valid IP address or machine name,\n"+\ "optionally followed by a colon and a port number"))
def __init__(self): if len(sys.argv)>1: self.PNUM = int(raw_input("what port number do you want?")) self.internal_print(str(self.PNUM+1) + "\n") else: self.PNUM = 12345 self.S = socket.socket(socket.AF_INET,socket.SOCK_DGRAM) self.S.bind(("",self.PNUM)) self.ADR = {} self.name = raw_input("username: "******"What address do you want to send data to: ") if temp != "": temp = temp.split(":") if len(temp) == 2: if temp[1] == "scan": self.scan_for_connection(socket.gethostbyname(temp[0])) else: self.ADR[(socket.gethostbyname(temp[0]),int(temp[1]))] = "" else: self.ADR[(socket.gethostbyname(temp[0]),self.PNUM)] = "" self.S.settimeout(2) self.msg = "" self.internal_print("You may now converse\nType 'exit' and hit return to leave the chat\n")
def _invalid_host(self, host): try: socket.gethostbyname(host) except socket.gaierror: self.logger.error("\t%s: Cannot resolve hostname" % host) return True return False
def set_ip_dhcp(): print "SET IP ADRESS : DHCP" logging.info( "SET IP ADRESS : DHCP") host_ip=socket.gethostbyname(socket.gethostname()) get_ip=host_ip os.system('netsh interface ipv4 set address name="Ethernet" source=dhcp') i=0 while get_ip == host_ip : ip_t=socket.gethostbyname(socket.gethostname()) if ip_t!='127.0.0.1': get_ip=ip_t if i==0: sys.stdout.write("Processing ") else: sys.stdout.write(".") time.sleep(1) if i < 60: i = i + 1 else: print "WAIT ",i," SEC, STOPPED, IP ADDRESS :",get_ip logging.error( "WAIT "+i+" SEC, STOPPED, IP ADDRESS :",get_ip) break print "" print "OK, IP ADRESS HOST FROM DHCP :",get_ip logging.info("OK, IP ADRESS HOST FROM DHCP :"+get_ip)
def IsIPv4(url): import socket try: #print socket.getaddrinfo(sr[1], 80) socket.gethostbyname(url) return True except: return False
def _mozharness_pip_args(self): '''We have information in Mozharness configs that apply to pip''' c = self.config pip_args = [] # To avoid timeouts with our pypi server, increase default timeout: # https://bugzilla.mozilla.org/show_bug.cgi?id=1007230#c802 pip_args += ['--timeout', str(c.get('pip_timeout', 120))] if c.get('find_links') and not c["pip_index"]: pip_args += ['--no-index'] # Add --find-links pages to look at. Add --trusted-host automatically if # the host isn't secure. This allows modern versions of pip to connect # without requiring an override. trusted_hosts = set() for link in c.get('find_links', []): parsed = urlparse.urlparse(link) try: socket.gethostbyname(parsed.hostname) except socket.gaierror as e: self.info('error resolving %s (ignoring): %s' % (parsed.hostname, e.message)) continue pip_args += ["--find-links", link] if parsed.scheme != 'https': trusted_hosts.add(parsed.hostname) for host in sorted(trusted_hosts): pip_args += ['--trusted-host', host] return pip_args
def set_ip(host_ip): print "" print "" sys.stdout.write("SET IP STATIC ADRESS :"+host_ip[0]+" ") logging.info( "SET IP ADRESS :"+host_ip[0]) get_ip=socket.gethostbyname(socket.gethostname()) if get_ip!=host_ip[0]: os.system('netsh interface ipv4 set address name="Ethernet" source=static address='+host_ip[0]+' mask='+host_ip[1]+' gateway='+host_ip[2]) i=0 while get_ip != host_ip[0] : ip_t=socket.gethostbyname(socket.gethostname()) if ip_t!='127.0.0.1': get_ip=ip_t if i==0: sys.stdout.write("Processing ") else: sys.stdout.write(".") time.sleep(1) if i < 60: i = i + 1 else: sys.stdout.write( " WAIT "+str(i)+" SEC STOPPED, IP ADDRESS :"+get_ip) break sys.stdout.write("OK, SET STATIC IP ADRESS HOST :"+get_ip) logging.info("OK, SET STATIC IP ADRESS HOST :"+get_ip) else: sys.stdout.write("IP :"+str(host_ip)+" established") logging.error("IP :"+str(host_ip)+" established")
def _get_filter_params(self): if self.network == "all": return "" elif self.network == "whitelist": code_loc_host = self.code_loc.replace("http://", "").replace("https://", "").split("/", 1)[0] code_loc_ip = socket.gethostbyname(code_loc_host) this_ip = netifaces.ifaddresses('virbr2')[2][0]['addr'] bcast = this_ip.rsplit(".",1)[0] + ".255" ips = [ "255.255.255.255", # always include the guest comms ip bcast, this_ip, code_loc_ip ] for other_host in self.whitelisted_hosts: ips.append(socket.gethostbyname(other_host)) res = [] for ip in ips: res.append("<parameter name='WHITELIST' value='{}' />".format(ip)) return "\n".join(res)
def hostname_resolves(hostname): """Checks to see if hostname is DNS resolvable""" try: socket.gethostbyname(hostname) return 1 except socket.error: return 0
def no_test_userandaddressinit(self): server = 'http://demo.opencastproject.org' user = '******'; password = '******'; hostname = 'rubenruaHost' address = '8.8.8.8' workflow = 'mini-full' workflow_parameters = 'uno:uno' workflow_parameters_2 = 'uno:uno;dos:dos' client = MHHTTPClient(server, user, password) self.assertEqual(client.hostname, 'GC-' + socket.gethostname()) self.assertEqual(client.address, socket.gethostbyname(socket.gethostname())) client = MHHTTPClient(server, user, password, hostname) self.assertEqual(client.hostname, hostname) self.assertEqual(client.address, socket.gethostbyname(socket.gethostname())) client = MHHTTPClient(server, user, password, hostname, address) self.assertEqual(client.hostname, hostname) self.assertEqual(client.address, address) client = MHHTTPClient(server, user, password, hostname, address) self.assertEqual(client.workflow, 'full') self.assertEqual(client.workflow_parameters, {'trimHold': 'true'}) client = MHHTTPClient(server, user, password, hostname, address, workflow, workflow_parameters) self.assertEqual(client.workflow, 'mini-full') self.assertEqual(client.workflow_parameters, {'uno': 'uno'}) client = MHHTTPClient(server, user, password, hostname, address, workflow, workflow_parameters_2) self.assertEqual(client.workflow, 'mini-full') self.assertEqual(client.workflow_parameters, {'uno': 'uno', 'dos': 'dos'})
def handle_dns_req(self, pkt): if pkt.getlayer(DNS) and pkt[DNS].qd and not pkt[DNS].an: # should this hostname be spoofed? if 'hostnames' in self.parameters.keys(): if not pkt[DNS].qd.qname.rstrip('.') in self.parameters['hostnames'].split(','): # return original ip address # is this an ipv6 request? if pkt[DNS].qd.qtype == IPV6_QUERY: try: valid_ip = getaddrinfo(pkt[DNS].qd.qname, None, AF_INET6, IPPROTO_IP, AI_CANONNAME)[0][4][0] response = self.mkdnsresponse6(pkt[DNS], valid_ip) p = IPv6(src=pkt[IPv6].dst, dst=pkt[IPv6].src)/UDP(sport=pkt[UDP].dport, dport=pkt[UDP].sport)/response except: p = None # is this an ipv4 request? if pkt[DNS].qd.qtype == IPV4_QUERY: try: valid_ip = gethostbyname(pkt[DNS].qd.qname) response = self.mkdnsresponse4(pkt[DNS], valid_ip) p = IPv6(src=pkt[IPv6].dst, dst=pkt[IPv6].src)/UDP(sport=pkt[UDP].dport, dport=pkt[UDP].sport)/response except: p = None if p: send(p, verbose=0) return # is it an ipv5 query? if pkt[DNS].qd.qtype == IPV4_QUERY: # should we spoof ipv4 querys as well? if 'spoofip4' in self.parameters.keys(): # spoof! spoof_ip = self.parameters['spoofip4'] self.log("%s is looking for %s. spoofing with %s.\n" % (pkt[IPv6].src, pkt[DNS].qd.qname, spoof_ip)) response = self.mkdnsresponse4(pkt[DNS], spoof_ip) p = IPv6(src=pkt[IPv6].dst, dst=pkt[IPv6].src)/UDP(sport=pkt[UDP].dport, dport=pkt[UDP].sport)/response else: # get real ip address of IPv4 Query try: valid_ip = gethostbyname(pkt[DNS].qd.qname) response = self.mkdnsresponse4(pkt[DNS], valid_ip) p = IPv6(src=pkt[IPv6].dst, dst=pkt[IPv6].src)/UDP(sport=pkt[UDP].dport, dport=pkt[UDP].sport)/response except: p = None else: # it is an ipv6 query if 'spoofip6' in self.parameters.keys(): spoof_ip = self.parameters['spoofip6'] else: spoof_ip = pkt[IPv6].dst self.log("%s is looking for %s. spoofing with %s.\n" % (pkt[IPv6].src, pkt[DNS].qd.qname, spoof_ip)) response = self.mkdnsresponse6(pkt[DNS], spoof_ip) p = IPv6(src=pkt[IPv6].dst, dst=pkt[IPv6].src)/UDP(sport=pkt[UDP].dport, dport=pkt[UDP].sport)/response if p: send(p, verbose=0)
def test_mbox(analoguer1, analoguer2): if hasattr(socket, 'setdefaulttimeout'): socket.setdefaulttimeout(3) total = 0 success = 0 ping = 0 exception_occur = True while True: total = total + 1 try: socket.gethostbyname('www.baidu.com') ping = ping + 1 if exception_occur: analoguer1.close(); analoguer1.connect(); analoguer1.identify(); analoguer1.read(); analoguer1.subscribe(MBOX_NAME); analoguer1.read() analoguer2.close(); analoguer2.connect(); analoguer2.identify(); analoguer2.read(); analoguer2.subscribe(MBOX_NAME); analoguer2.read() data = analoguer1.get_randstr(30) if total % 2 == 0: analoguer1.publish(MBOX_NAME, data) r = analoguer1.read() assert(r['peers'] == 1) r = analoguer2.read() assert(r['body']['data'] == data) else: analoguer2.publish(MBOX_NAME, data) r = analoguer2.read() assert(r['peers'] == 1) r = analoguer1.read() assert(r['body']['data'] == data) exception_occur = False success = success + 1 except Exception, e: exception_occur = True print e print 'total: %s, success: %s, ping: %s, percentage: %s%%' % (total, success, ping, int(success*10000/total)/100.0) time.sleep(3)
def check(self): # do check of host try: # requires name resolve facility socket.gethostbyname(self.hostname) except IOError: raise BuilderError("{0} could not be resolved".format(self.hostname)) try: # check_for_ans_error(res, self.hostname) self.run_ansible_with_check("/bin/rpm -q mock rsync") except AnsibleCallError: raise BuilderError(msg="Build host `{0}` does not have mock or rsync installed" .format(self.hostname)) # test for path existence for mockchain and chroot config for this chroot try: self.run_ansible_with_check("/usr/bin/test -f {0}".format(mockchain)) except AnsibleCallError: raise BuilderError(msg="Build host `{}` missing mockchain binary `{}`" .format(self.hostname, mockchain)) try: self.run_ansible_with_check("/usr/bin/test -f /etc/mock/{}.cfg" .format(self.job.chroot)) except AnsibleCallError: raise BuilderError(msg="Build host `{}` missing mock config for chroot `{}`" .format(self.hostname, self.job.chroot))
def check_in(revip, service): try: # this must be uncached... caching is done in dronebl_lookup socket.gethostbyname('%s.%s' % (revip, service)) return True except: return False
def main(): parser = OptionParser() parser.add_option("-s", "--src", dest="src", type="string", help="Source IP address", metavar="IP") parser.add_option("-d", "--dst", dest="dst", type="string", help="Destination IP address", metavar="IP") options, args = parser.parse_args() if options.dst == None: parser.print_help() sys.exit() else: dst_host = socket.gethostbyname(options.dst) if options.src == None: # Get the current Network Interface src_host = socket.gethostbyname(socket.gethostname()) else: src_host = options.src print("[+] Local Machine: %s"%src_host) print("[+] Remote Machine: %s"%dst_host) data = "TEST!!" print("[+] Data to inject: %s"%data) # IP Header ipobj = IP(src_host, dst_host) # TCP Header tcpobj = TCP(1234, 80) response = send(ipobj, tcpobj, iface="eth0", retry=1, timeout=0.3) if response: ip = ipobj.unpack(response) response = response[ip.ihl:] tcp = tcpobj.unpack(response) print "IP Header:", ip.list print "TCP Header:", tcp.list
def resolv_check(config_obj): # Addresses validation ip_pattern = re.compile("^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$") name_pattern = re.compile("(?!-)[A-Z\d-]{1,63}(?<!-)$", re.IGNORECASE) address_for_valid = {'general': 'host', 'moose_options': 'master_host'} if config_obj.get('general', 'host') is not None: for i in address_for_valid: if not ip_pattern.match(config_obj.get(i, address_for_valid[i])): if not name_pattern.match(config_obj.get(i, address_for_valid[i])): msg = '%s address is not valid in %s section.' % (address_for_valid[i], i) logger.error(msg) raise mfs_exceptions.AddressValidError(msg) else: try: socket.gethostbyname(config_obj.get(i, address_for_valid[i])) except Exception: msg = "%s doesn't resolve" % str(config_obj.get(i, address_for_valid[i])) logger.error(msg) raise mfs_exceptions.HostResolveError(msg) # Check whether master address is valid master_host = config_obj.get('moose_options', 'master_host') try: s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect((master_host, 22)) except Exception: msg = '%s host is innaccessible' % str(master_host) logger(msg) raise mfs_exceptions.MooseConnectionFailed(msg) finally: s.close()
def get_ip(): """Provides an available network interface address, for example "192.168.1.3". A `NetworkError` exception is raised in case of failure.""" try: try: ip = socket.gethostbyname(socket.gethostname()) except socket.gaierror: # for Mac OS X ip = socket.gethostbyname(socket.gethostname() + ".local") except socket.gaierror: # sometimes the hostname doesn't resolve to an ip address, in which # case this will always fail ip = None if (ip is None or ip.startswith("127.")) and mozinfo.isLinux: interfaces = _get_interface_list() for ifconfig in interfaces: if ifconfig[0] == 'lo': continue else: return ifconfig[1] if ip is None: raise NetworkError('Unable to obtain network address') return ip
def find_eap_in_state(self, state, check_if_resolvable_hostname = False): rows = self.hawkular_api.get_hawkular_servers() for row in rows: self.web_session.logger.info("Product: {} Feed: {} State: {}". format(row.get("Product Name"), row.get("Feed"), row.get("details").get("Server State"))) if not row.get("Product Name"): self.web_session.logger.warning ("Product Name 'None'. Feed: {}.".format(row.get("Feed"))) elif (row.get("Product Name") == 'JBoss EAP' or 'wildfly' in row.get("Product Name").lower()) \ and row.get("Node Name") != 'master:server-*' \ and (state.lower() == "any" or row.get("details").get("Server State") == state.lower()) \ and "domain" not in row.get("Feed").lower(): if check_if_resolvable_hostname: hostname = row.get("details").get("Hostname") try: socket.gethostbyname(hostname) self.web_session.logger.debug("Found resolvable Hostname: {}".format(hostname)) return row except: self.web_session.logger.debug("Note a resolvable Hostname: {}".format(hostname)) else: return row return None
def is_hostname_valid(self, hostname): """ if hostname is valid """ try: socket.gethostbyname(hostname) except: return False return True
def __init__(self, args=[]): # arguments vector self.args = args # start port and end port self.start, self.stop = 1, 1024 # host name self.host = "" # check the arguments if len(self.args) == 4: self.host = self.args[1] try: self.start = int(self.args[2]) self.stop = int(self.args[3]) except ValueError: usage() return if self.start > self.stop: usage() return elif len(self.args) == 2: self.host = self.args[1] else: usage() return try: sk.gethostbyname(self.host) except: print "hostname '%s' unknown" % self.host self.scan(self.host, self.start, self.stop)
def validate(config): """Validate beacon configuration.""" # Configuration for nginx_connections beacon should be a dict if not isinstance(config, dict): return False, ('Configuration for nginx_connections ' 'beacon must be a dictionary.') else: # Verify configuration parameters if 'protocol' in config: if config['protocol'] not in VALID_PROTOCOLS: return False, ('Protocol configuration for nginx_connections ' 'should be in {}.'.format(VALID_PROTOCOLS)) if 'port' in config: try: port = int(config['port']) except ValueError: return False, ('Port configuration for nginx_connections ' 'is not a number.') else: if port < 1 or port > 65535: return False, ('Port configuration for nginx_connection ' 'is not a valid port number.') if 'host' in config: try: socket.gethostbyname(config['host']) except socket.error: return False, ('Host configuration for nginx_connection ' 'cannot be resolved.') return True, 'Valid beacon configuration.'
def test_bulkadd(self): app_id = 'test_app' bulk_add_request = taskqueue_service_pb.TaskQueueBulkAddRequest() item = bulk_add_request.add_add_request() item.set_app_id(app_id) item.set_queue_name('default') item.set_task_name('babaganoose') item.set_eta_usec(0) item.set_method(taskqueue_service_pb.TaskQueueAddRequest.GET) item.set_mode(taskqueue_service_pb.TaskQueueMode.PUSH) host = socket.gethostbyname(socket.gethostname()) item.set_url('http://' + host + ':64839/queues') host = socket.gethostbyname(socket.gethostname()) req = urllib2.Request('http://' + host + ':64839') api_request = remote_api_pb.Request() api_request.set_method("BulkAdd") api_request.set_service_name("taskqueue") api_request.set_request(bulk_add_request.Encode()) remote_request = api_request.Encode() req.add_header('Content-Length', str(len(remote_request))) req.add_header('protocolbuffertype', 'Request') req.add_header('appdata', app_id) response = urllib2.urlopen(req, remote_request) api_response = response.read() api_response = remote_api_pb.Response(api_response) bulk_add_response = taskqueue_service_pb.TaskQueueBulkAddResponse(api_response.response()) print bulk_add_response self.assertEquals(response.getcode(), 200)
def test_bulkadd(self): app_id = "test_app" bulk_add_request = taskqueue_service_pb.TaskQueueBulkAddRequest() item = bulk_add_request.add_add_request() item.set_app_id(app_id) item.set_queue_name("default") item.set_task_name("babaganoose") item.set_method(taskqueue_service_pb.TaskQueueAddRequest.GET) item.set_mode(taskqueue_service_pb.TaskQueueMode.PUSH) item.set_eta_usec(5000000) # 5 seconds host = socket.gethostbyname(socket.gethostname()) item.set_url("http://" + host + ":64839/queues") host = socket.gethostbyname(socket.gethostname()) req = urllib2.Request("http://" + host + ":64839") api_request = remote_api_pb.Request() api_request.set_method("BulkAdd") api_request.set_service_name("taskqueue") api_request.set_request(bulk_add_request.Encode()) remote_request = api_request.Encode() req.add_header("Content-Length", str(len(remote_request))) req.add_header("protocolbuffertype", "Request") req.add_header("appdata", app_id) response = urllib2.urlopen(req, remote_request) api_response = response.read() api_response = remote_api_pb.Response(api_response) bulk_add_response = taskqueue_service_pb.TaskQueueBulkAddResponse(api_response.response()) print bulk_add_response self.assertEquals(response.getcode(), 200)
def getHostLocalAddress(self,): if platform == "darwin": hostname = socket.gethostname() if '.local' in socket.gethostname() else socket.gethostname()+'.local' #OS X where hostname doesn't have the <.local> return socket.gethostbyname(hostname) else: return socket.gethostbyname(socket.gethostname())
def _authorize_client_ports(self, client_cidrs=[]): if not client_cidrs: self.logger.debug("No client CIDRs specified, using local address.") client_ip = url_get('http://checkip.amazonaws.com/').strip() client_cidrs = ("%s/32" % client_ip,) self.logger.debug("Client CIDRs: %s", client_cidrs) namenode = self._get_namenode() jobtracker = self._get_jobtracker() for client_cidr in client_cidrs: # Allow access to port 80 on namenode from client self.cluster.authorize_role(self.NAMENODE, 80, 80, client_cidr) # Allow access to jobtracker UI on master from client # (so we can see when the cluster is ready) self.cluster.authorize_role(self.JOBTRACKER, 50030, 50030, client_cidr) # Allow access to namenode and jobtracker via public address from each other namenode_ip = socket.gethostbyname(namenode.public_dns_name) jobtracker_ip = socket.gethostbyname(jobtracker.public_dns_name) self.cluster.authorize_role(self.NAMENODE, 8020, 8020, "%s/32" % namenode_ip) self.cluster.authorize_role(self.NAMENODE, 8020, 8020, "%s/32" % jobtracker_ip) self.cluster.authorize_role(self.JOBTRACKER, 8021, 8021, "%s/32" % namenode_ip) self.cluster.authorize_role(self.JOBTRACKER, 8021, 8021, "%s/32" % jobtracker_ip)
def main(): stressHosts_by_host = ['sfeserv27','sfeserv03','gba-ubun810-amd64','gba-ubun810-i386','gba-mac-ppc','gba-fbsd63-i386','gba-fbsd72-i386'] stressHosts = [] for sh in range(1,16): try: stressHosts.append(socket.gethostbyname('stress%02d.splunk.com' % sh)) except socket.gaierror: pass # do the same for pre-defined hosts for sh in stressHosts_by_host: try: stressHosts.append(socket.gethostbyname(sh)) except socket.gaierror: pass random.shuffle(stressHosts) dns = "server sfeserv31.splunk.com\nzone es.splunk.com\nupdate delete stressclients.es.splunk.com. A\n" IPs = '' for IP in stressHosts: IPs = "update add stressclients.es.splunk.com. 3600 A %s\n %s" % (IP,IPs) dns = dns + IPs + "show\nsend\nEOF" std_pipes = {} cmd = ["nsupdate","-k","Kes.splunk.com.+157+51549.key"] #try: po = Popen(cmd, stdout=PIPE, stderr=PIPE, stdin=PIPE) std_pipes = po.communicate(input=dns) print std_pipes
def proc_excluded(self,excluded): processed=set() for item in excluded: try: test=item.split(".") if (len(test)!=4): try: processed.add(socket.gethostbyname(item)) except: pass else: try: if (int(test[0])>0 and int(test[0])<256): if (int(test[0])>0 and int(test[0])<256): if (int(test[0])>0 and int(test[0])<256): if (int(test[0])>0 and int(test[0])<256): processed.add(item) except: processed.add(socket.gethostbyname(item)) except: try: processed.add(socket.gethostbyname(item)) except: pass return processed
def _analyze_ips(self, ip_address_list, fuzzable_request): ''' Search all IP addresses in Bing and determine if they have more than one domain hosted on it. Store findings in KB. ''' bing_wrapper = bing(self._uri_opener) # This is the best way to search, one by one! for ip_address in ip_address_list: results = bing_wrapper.get_n_results('ip:' + ip_address, self._result_limit) results = [r.URL.base_url() for r in results] results = list(set(results)) # not vuln by default is_vulnerable = False if len(results) > 1: # We may have something... is_vulnerable = True if len(results) == 2: # Maybe we have this case: # [Mon 09 Jun 2008 01:08:26 PM ART] - http://216.244.147.14/ # [Mon 09 Jun 2008 01:08:26 PM ART] - http://www.business.com/ # Where www.business.com resolves to 216.244.147.14; so we don't really # have more than one domain in the same server. try: res0 = socket.gethostbyname(results[0].get_domain()) res1 = socket.gethostbyname(results[1].get_domain()) except: pass else: if res0 == res1: is_vulnerable = False if is_vulnerable: desc = 'The web application under test seems to be in a shared' \ ' hosting. This list of domains, and the domain of the ' \ ' web application under test, all point to the same IP' \ ' address (%s):\n' % ip_address domain_list = kb.kb.raw_read(self, 'domains') for url in results: domain = url.get_domain() desc += '- %s\n' % domain domain_list.append(domain) kb.kb.raw_write(self, 'domains', domain_list) v = Vuln.from_fr('Shared hosting', desc, severity.MEDIUM, 1, self.get_name(), fuzzable_request) v['also_in_hosting'] = results om.out.vulnerability(desc, severity=severity.MEDIUM) kb.kb.append(self, 'shared_hosting', v)
import socket, time import settings host = socket.gethostbyname(socket.gethostname()) clients = [] server_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) server_socket.bind((settings.SERVER_ADDRESS, settings.SERVER_PORT)) quit_server = False print("[ Server Started ]") while not quit_server: try: data, addr = server_socket.recvfrom(1024) if addr not in clients: clients.append(addr) current_time = time.strftime("%Y-%m-%d-%H.%M.%S", time.localtime()) print("[{0}]=[{1}]=[{2}]/".format(addr[0], str(addr[1]), current_time), end="") print(data.decode(settings.ENCODING_FORMAT)) for client in clients: if addr != client: server_socket.sendto(data, client) except: print("[ Server Stopped ]") quit_server = True
"bg", "fr-be", "lb", "es-ar", "be", "fr-ca", "mk", "es-bo", "bn", "fr-fr", "ms", "es-cl", "bs", "fr-lu", "ml", "es-co", "br", "fr-mc", "mt", "es-cr", "bg", "fr-ch", "mi", "es-do", "my", "fy", "mr", "es-ec", "ca", "fur", "mo", "es-sv", "ch", "gd", "nv", "es-gt", "ce", "gd-ie", "ng", "es-hn", "zh", "gl", "ne", "es-mx", "zh-hk", "ka", "no", "es-ni", "zh-cn", "de", "nb", "es-pa", "zh-sg", "de-at", "nn", "es-py", "zh-tw", "de-de", "oc", "es-pe", "cv", "de-li", "or", "es-pr", "co", "de-lu", "om", "es-es", "cr", "de-ch", "fa", "es-uy", "fa-ir", "es-ve" ] global pt pt = ["/"] n = 0 while n < 1: try: url = raw_input('\n\n\nTarget: (www.example.com or IP)\n>') socket.gethostbyname(url) if url != "": n += 1 else: print "Don't leave your target blank" except: print "Write your the domain or IP correctly" while n < 2: try: port = input("\n\nPort: (80 or 443)\n>") if port in [80, 443]: n += 1 else: print "Enter: 80 or 443" except: print "Enter a number"
def fabao(): b=0 c=0 i=0 one=1 while 1: try: #创建socket s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) i=i+1 printcn("socket 接口创建" + str( i)) except: s.close() printcn("socket 创建失败 " + str( i)) printcn("socket 正在重建") time.sleep(0.1) continue if one: one=0 try: #解析地址端口 host = 'mob.huanghuai.edu.cn' remote_ip= socket.gethostbyname( host ) socket.gethostbyname( host ) print remote_ip printcn("解析服务器地址成功" ) printcn( str(host) + " " + str(remote_ip)) except: s.close() printcn("无法解析服务器地址 请检查网络") printcn("即将尝试重启socket") time.sleep(0.1) one=1 continue ip=remote_ip try: # 连接服务器 s.connect((ip,80)) printcn("连接至服务器" + str(remote_ip) +"成功" ) except: printcn("服务器无法链接 检查服务器状态") printcn("即将尝试重启socket") s.close() time.sleep(0.1) continue a = 0 while a <10: a=a+1 try : #Set the whole string time.sleep(0.05) printcn("数据即将发送 "+'学号'+str(xh)+' ' + str(b+1)) s.sendall(message) b=b+1 printcn('数据已经发送 ' +'座位号'+str(zwh)+' ' + str(b)) except: #Send failed c=c+1 printcn("发送失败" + str(c)) s.close() time.sleep(0.03) continue
def jiancha(): one_1=1 while 1: time.sleep(1) try: #创建socket s_1 = socket.socket(socket.AF_INET, socket.SOCK_STREAM) printcn("socket检测 接口创建" ) except: s_1.close() printcn("socket检测 创建失败 " ) printcn("socket检测 正在重建") continue if one_1: one_1=0 try: #解析地址端口 host_1 = 'mob.huanghuai.edu.cn' remote_ip_1= socket.gethostbyname( host_1 ) socket.gethostbyname( host_1 ) print remote_ip_1 printcn("解析服务器地址成功" ) printcn( str(host_1) + " " + str(remote_ip_1)) except: s_1.close() printcn("无法解析服务器地址 请检查网络") printcn("即将尝试重启socket") one_1=1 continue ip_1=remote_ip_1 try: # 连接服务器 s_1.connect((ip_1,80)) printcn("连接至服务器" + str(remote_ip_1) +"成功" ) except: printcn("服务器无法链接 检查服务器状态") printcn("即将尝试重启socket") s_1.close() continue try : #Set the whole string s_1.sendall(message) except: #Send failed s_1.close() continue reply_1 = s_1.recv(2048) s_1.close() time.sleep(1) reply1_1=reply_1[295:] printcn (reply1_1) if '冻结' in reply1_1: printcn("账户被冻结 需要更换账号") my_txt='学号'+str(xh)+'冻结状态 失败 即将退出' mail(my_txt) break if '成功' in reply1_1: printcn("预约成功 ") my_txt='学号'+str(xh)+'如果你幸运的看到了这封邮件\r\n那么希望已经预约成功了,你可以观察预约一下预约时间是否为你所预期的,在此刻之后 ,系统将会尝试继续锁定十五分钟,十五分钟内你可以尝试通过取消预约来重置预约状态' mail(my_txt) ys=0 while ys<900: ys=ys+1 ysjs=900-ys time.sleep(1) printcn('预约成功 *************延时锁定状态 ********* 延时计数'+str(ysjs)) printcn('延时计数归零 即将退出') break if '用户已经选择座位' in reply1_1: printcn("预约成功") my_txt='学号'+str(xh)+'如果你幸运的看到了这封邮件\r\n那么希望已经预约成功了,你可以观察预约一下预约时间是否为你所预期的,在此刻之后 ,系统将会尝试继续锁定十五分钟,十五分钟内你可以通过尝试取消预约来重置预约状态' mail(my_txt) ys=0 while ys<900: ys=ys+1 ysjs=900-ys time.sleep(1) printcn('预约成功 *************延时锁定状态 ********* 延时计数'+str(ysjs)) printcn('延时计数归零 即将退出') break if '座位已经被选择' in reply1_1: printcn("****************************继续运行中****************************。\r\n\r\n" )
def dispatch(self, *args): """ This is dispatch queue of scheduler where test from different framework wait in the waiting queue for scheduler to bind it with trigger thread. This procedure continuously iterate over testmap populated by DBMon with tests started by user from UI or CLI. This keep track of all running tests and it allows one test per framework. Once this procedure find an open test spot for a test from particular framework, this procedure will pop it from testmap, put it in dispatch queue and assign trigger thread for this test to start test setup and then execution. """ dispatch_threads = defaultdict() while True: # Continuously iterate on testmap for initiating any test execution for k in self.testmap: # iterating for all frameworkid k in testmap which contains list of waiting tests for a particular framework found = False # If test for a particular framework is already in running or dispatch queue then this new test need to # wait until previous test gets finish, hence we do nothing and just continue if k in self.dispatch_queue or k in self.running_tests: found = True else: lctx.debug("Found spot for test") if found: continue # Proceed if spot is available for executing test for this framework try: tmp_t = self.testmap[k][0] except Exception as e: lctx.debug("No test object found in map") continue if tmp_t is None: continue alive = False h = tmp_t.testobj.TestInputData.exechostname # Initiating test logger for capturing test life cycle on scheduler, all logs are logged in file <testid>.log test_logger = LOG.init_testlogger(tmp_t, "EXEC") test_logger.info("Test execution starts") try: # Sending heartbeat on exec host to check if it agent is up on exec host retsend = self.cl.send( h, self.CPORT, self.ev.construct("DAYTONA_HEARTBEAT", "")) if retsend and len(retsend.split(",")) > 2: status = retsend.split(",")[1] else: raise Exception( "Execution host not avaliable - No Heartbeat ", tmp_t.testobj.TestInputData.testid) if "ALIVE" == status: test_logger.info( "HeartBeat received from execution host " + h) # Sending DAYTONA_HANDSHAKE for verifying connectivity between scheduler and agent on exec host # using custom daytona ports ret = self.cl.send( h, self.CPORT, self.ev.construct( "DAYTONA_HANDSHAKE", "handshake1," + self.HOST + "," + str(self.PORT) + "," + str(tmp_t.testobj.TestInputData.testid) + "," + h)) if ret == "SUCCESS": alive = True test_logger.info( "Handshake successful with execution host " + h) lctx.debug( "Handshake successful in scheduler, adding ip/hostname to reg hosts" ) server.serv.registered_hosts[h] = h addr = socket.gethostbyname(h) lctx.debug(addr) server.serv.registered_hosts[addr] = addr else: raise Exception( "Unable to handshake with agent on executuion host " + h) except Exception as e: lctx.error(e) test_logger.error(e) # pause the dbmon here as we dont want the same test to be picked again after we pop self.dbinstance.mon_thread[0].pause() self.dbinstance.lock.acquire() t = self.testmap[k].pop(0) t.updateStatus("waiting", "failed") self.dbinstance.lock.release() lctx.debug("Removed test from map : " + str(t.testobj.TestInputData.testid)) self.dbinstance.mon_thread[0].resume() LOG.removeLogger(tmp_t) continue if alive == True and found == False: # for each framework pick one and move it to running, iff running has an empty slot. lctx.debug( "-------Found empty slot in dispatch and running Q-------" ) # pause the dbmon here as we dont want the same test to be picked again after we pop self.dbinstance.mon_thread[0].pause() self.dbinstance.lock.acquire() t = self.testmap[k].pop(0) self.dbinstance.lock.release() lctx.info("< %s" % t.testobj.TestInputData.testid) # put the test in dispatch queue self.dispatchQ__lock.acquire() self.dispatch_queue[k] = t self.dispatchQ__lock.release() t.updateStatus("waiting", "setup") self.dbinstance.mon_thread[0].resume() try: # Bind a seperate trigger thread for this test to start test execution trigger_thread = common.FuncThread( self.trigger, True, t) dispatch_threads[t.testobj.TestInputData.testid] = ( trigger_thread, t) trigger_thread.start() except Exception as e: lctx.error("Trigger error : " + str(t.testobj.TestInputData.testid)) test_logger.error("Test setup failed " + str(t.testobj.TestInputData.testid)) LOG.removeLogger(tmp_t) self.dispatchQ__lock.acquire() del self.dispatch_queue[k] self.dispatchQ__lock.release() lctx.debug(e) try: # Log list of test currently present in dispatch queue in scheduler debug file d = "DISPATCH [S/R] : " for k in self.dispatch_queue: d = d + " |" + str( self.dispatch_queue[k].testobj.TestInputData.testid) except: lctx.debug("ERROR : Dispatch Q empty") lctx.debug(d) d = "" time.sleep(2)
def trigger(self, *args): """ trigger starts in a thread, keep track of all triggers and they should complete in a specified time, otherwise signal a close. triggers are used for test setup, then starting test and then taking test into running state on agent. """ t = args[1] serialize_str = t.serialize() t2 = testobj.testDefn() t2.deserialize(serialize_str) time.sleep(6) # Setting up test logger for capturing test life cycle on scheduler test_logger = LOG.gettestlogger(t2, "EXEC") test_logger.info("Test setup started") try: if t.testobj.TestInputData.testid != t2.testobj.TestInputData.testid: lctx.error("testobj not same") raise Exception("test trigger error", t2.testobj.TestInputData.testid) # Sending DAYTONA_SETUP_TEST command on execution host, on receiving this command agent will perform basic # test setup by creating test directories for saving log files and it will copy execution script in test # directory for starting execution ip = t.testobj.TestInputData.exechostname retsend = self.cl.send( ip, self.CPORT, self.ev.construct("DAYTONA_SETUP_TEST", serialize_str + ",EXEC")) lctx.debug(retsend) if retsend and len(retsend.split(",")) > 1: if retsend.split(",")[1] != "SUCCESS": lctx.error(retsend) raise Exception("test trigger error", t2.testobj.TestInputData.testid) else: raise Exception("Test Setup Failure : Test - ", t2.testobj.TestInputData.testid) test_logger.info("Test setup complete on exec host " + ip) # Triggering test setup on all stat hosts for s in t.testobj.TestInputData.stathostname.split(','): if len(s.strip()) == 0: break test_logger.info("Starting test setup on stat host " + s) lctx.debug(s.strip()) p = self.CPORT # Sending hearbeat message to check whether stat host is up or not retsend = self.cl.send( s, p, self.ev.construct("DAYTONA_HEARTBEAT", "")) if retsend and len(retsend.split(",")) > 1: if retsend.split(",")[1] != "ALIVE": raise Exception( "Remove host not avaliable - No Heartbeat ", t2.testobj.TestInputData.testid) else: test_logger.info("Hearbeat received from stat host " + s) # Trigger DAYTONA_HANDSHAKE to verify that both agent and scheduler are able to communicate with # each other on custom daytona ports retsend = self.cl.send( s, p, self.ev.construct( "DAYTONA_HANDSHAKE", "handshake1," + self.HOST + "," + str(self.PORT) + "," + str(t2.testobj.TestInputData.testid) + "," + s)) lctx.debug(retsend) if retsend == "SUCCESS": alive = True server.serv.registered_hosts[s] = s addr = socket.gethostbyname(s) server.serv.registered_hosts[addr] = addr test_logger.info( "Handshake successfull with agent on stat host " + s) else: raise Exception( "Unable to handshake with agent on stats host:" + s, t2.testobj.TestInputData.testid) else: raise Exception("Stat host " + s + " not avaliable - No Heartbeat") # Trigger test setup on stat hosts, this will create test directory for saving log files retsend = self.cl.send( s.strip(), p, self.ev.construct("DAYTONA_SETUP_TEST", serialize_str + ",STAT")) lctx.debug(retsend) if retsend and len(retsend.split(",")) > 1: if retsend.split(",")[1] != "SUCCESS": lctx.error(retsend) raise Exception("test trigger error", t2.testobj.TestInputData.testid) else: raise Exception("Test Setup Failure : Test - ", t2.testobj.TestInputData.testid) test_logger.info("Test setup complete on stat host " + s) # Trigger the start of test on exec host retsend = self.cl.send( ip, self.CPORT, self.ev.construct( "DAYTONA_START_TEST", str(t2.testobj.TestInputData.testid) + ",EXEC")) lctx.debug(retsend) if retsend and len(retsend.split(",")) > 1: if retsend.split(",")[1] != "SUCCESS": lctx.error(retsend) raise Exception("test trigger error", t2.testobj.TestInputData.testid) else: raise Exception("Failed to start Test : ", t2.testobj.TestInputData.testid) test_logger.info("Test started on exec host " + ip) # Trigger the start of test on STAT hosts (This is for initiating system metric data collection) for s in t.testobj.TestInputData.stathostname.split(','): if len(s.strip()) == 0: break p = self.CPORT s = s.strip() retsend = self.cl.send( s, p, self.ev.construct( "DAYTONA_START_TEST", str(t2.testobj.TestInputData.testid) + ",STAT")) lctx.debug(retsend) if retsend and len(retsend.split(",")) > 1: if retsend.split(",")[1] != "SUCCESS": lctx.error(retsend) raise Exception("Failed to start test on STAT host : ", s) else: raise Exception("Failed to start test on STAT host : ", s) test_logger.info("Test started on stat host " + s) # Get status from exec host retsend = self.cl.send( ip, self.CPORT, self.ev.construct("DAYTONA_GET_STATUS", str(t2.testobj.TestInputData.testid))) if retsend and len(retsend.split(",")) > 1: if "RUNNING" == retsend.split( ",")[1] or "MONITOR_ON" == retsend.split(",")[1]: # update from setup to running lctx.debug("Updating test status to running in DB") t.updateStatus("setup", "running") now = time.time() tstr = str( time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime(now))) t.updateStartTime(tstr) self.dispatchQ__lock.acquire() del self.dispatch_queue[ t.testobj.TestInputData.frameworkid] self.dispatchQ__lock.release() self.lock.acquire() self.running_tests[t.testobj.TestInputData.frameworkid] = t self.lock.release() else: lctx.error( "Unable to determine status, testmon will garbage collect this testid" ) # Garbage collect testid in runnning state that fails to give status # Killing of threads on client host and remove from list with status=fail is done in testmon except Exception as e: # If any trigger fails, then abort test startup with error lctx.error(e) test_logger.error(e) lctx.error("ERROR : Unknown trigger error : " + str(t.testobj.TestInputData.testid)) t.updateStatus("setup", "failed") lctx.debug(traceback.print_exc()) lctx.error("Removing Test " + str(t.testobj.TestInputData.testid) + " from dispatch Q") self.dispatchQ__lock.acquire() del self.dispatch_queue[t.testobj.TestInputData.frameworkid] self.dispatchQ__lock.release() # This will abort test and perform cleanup on exec host with trigger was successful on exec host retsend = self.cl.send( ip, self.CPORT, self.ev.construct("DAYTONA_ABORT_TEST", str(t2.testobj.TestInputData.testid))) lctx.debug(retsend) test_logger.info("Test aborted on exec host " + ip) # On all other stat hosts we send cleanup in case trigger was successful on any stat host for s in t.testobj.TestInputData.stathostname.split(','): if len(s.strip()) == 0: break retsend = self.cl.send( s.strip(), self.CPORT, self.ev.construct("DAYTONA_CLEANUP_TEST", str(t2.testobj.TestInputData.testid))) lctx.debug(retsend) test_logger.info("Test abort on stat host " + s) LOG.removeLogger(t2) return "FAILED" return "SUCCESS"
from calvin.utilities.calvin_callback import CalvinCB from calvin.utilities import calvinlogger from calvin.utilities.utils import get_home from calvin.utilities import runtime_credentials from calvin.runtime.south.plugins.storage.twistedimpl.securedht.append_server import * from calvin.runtime.south.plugins.storage.twistedimpl.securedht.dht_server import * from calvin.runtime.south.plugins.storage.twistedimpl.securedht.service_discovery_ssdp import * from calvin.runtime.south.plugins.storage.twistedimpl.securedht.dht_server_commons import drawNetworkState from kademlia.node import Node from kademlia.utils import deferredDict, digest from calvin.runtime.south.plugins. async import threads from calvin.utilities import calvinconfig import socket ip_addr = socket.gethostbyname(socket.gethostname()) _log = calvinlogger.get_logger(__name__) name = "node2:" homefolder = get_home() domain = "sec-dht-security-test" testdir = os.path.join(homefolder, ".calvin", "sec_dht_security_test") configdir = os.path.join(testdir, domain) runtimesdir = os.path.join(testdir, "runtimes") runtimes_truststore = os.path.join(runtimesdir, "truststore_for_transport") _conf = calvinconfig.get() _conf.add_section("security") _conf.set('security', "runtimes_path", runtimesdir) _conf.set('security', "domain_name", domain) _conf.set('security', "security_path", testdir)
def check_localhost(): localhost = socket.gethostbyname("localhost") return localhost == '127.0.0.1'
def find_sparc_clients(lservices, sname=None): """ find_sparc_clients() searches /etc/netboot for all clients and returns a dictionary that contains a list of dictionaries. The service name is the key for the main dictionary and the client and image path are members of the subdictionary. The dictionary will look something like: { 'service1': [ { 'ipath':<path1>, 'client':<client1> }, .... ], .... } The information is spread out across a couple of different files within the server. The Client is embedded within a directory path (/etc/netboot/<IP Network>/01<client>). The Image Path is in the wanboot.conf file pointed to by the Client path. The Service Name is contained in the install.conf file pointed to by the Image Path. We first get the IP addresses for the host. Then while only using IPv4 address we iterate over the client directories within /etc/netboot/<IP Network> to get the Image Path and Service Name. The client and image path are then added to the named service dictionary list. Args lservices = sname = Returns dictionary of a list of dictionaries Raises None """ installconf = 'install.conf' wanbootconf = 'wanboot.conf' def get_image_path(lpath): """ gets the Image Path for the client pointed to by lpath. The Image Path on Sparc is stored in the wanboot.conf file. Args lpath = path for directory which contains wanboot.conf file Returns image path for client Raises None """ try: confpath = os.path.join(lpath, wanbootconf) sinfo = os.stat(confpath) fp = open(confpath) fstr = fp.read(sinfo.st_size) fp.close() except (OSError, IOError): sys.stderr.write("%s: error: while accessing wanboot.conf file" % \ sys.argv[0]) return start = fstr.find('boot_file=') + len('boot_file=') end = fstr[start:].find('/platform') return fstr[start:start + end] def get_service_name(lpath): """ gets the Service Name for the client from the lpath pointed to by the wanboot.conf file. The Service Name is in the Image Path install.conf file. Args lpath = path to directory containing install.conf file Returns install service for client Raises None """ try: confpath = os.path.join(lpath, installconf) sinfo = os.stat(confpath) fp = open(confpath) fstr = fp.read(sinfo.st_size) fp.close() except (OSError, IOError): sys.stderr.write("%s: error: while accessing " "install.conf file\n" % \ sys.argv[0]) return start = fstr.find('install_service=') + len('install_service=') end = fstr[start:].find('\n') return fstr[start:start + end] # start of find_sparc_clients if not os.path.exists('/etc/netboot'): return {} sdict = {} hostname = socket.getfqdn() ipaddr = socket.gethostbyname(hostname) # get the Network IP path for the host end = ipaddr.rfind('.') path = os.path.join('/etc/netboot', ipaddr[:end] + '.0') try: for clientdir in os.listdir(path): # strip off the 01 in the clientdir client = AIdb.formatValue('mac', clientdir[2:]) # get the Image from the clientdir/wanboot.conf file ipath = get_image_path(os.path.join(path, clientdir)) if not os.path.exists(ipath): continue # get the service name from the ipath/install.conf file servicename = get_service_name(ipath) # store the client and image path in the # dictionary under the service name. First # check to see if the service name key # already exists. if servicename in lservices and \ (not sname or servicename == sname): tdict = {'client': client, 'ipath': [ipath], 'arch': 'Sparc'} if sdict.has_key(servicename): # existing service name key slist = sdict[servicename] slist.extend([tdict]) sdict[servicename] = slist else: # new service name key sdict[servicename] = [tdict] except OSError: return {} return sdict
def __init__(self, address, port, threaded, dbdata, registered_filenames, uploaddir, *, bg = False, use_db_pool = True, scriptdir = None, only_registered=False, cafile=None, whitelist=None, timeout_hint=None): self.name = "HTTP server" self.logger = logging.getLogger(self.name) self.logger.setLevel(logging.NOTSET) self.address = address if address else "0.0.0.0" self.url_address = address if address else socket.gethostname() self.cafile = cafile self.only_registered = only_registered upload_scriptname = "upload.py" self.serving_wus = [True] self.timeout_hint = timeout_hint # formatter = logging.Formatter( # fmt='%(address_string)s - - [%(asctime)s] %(message)s') #self.ch = logging.StreamHandler() #self.ch.setFormatter(formatter) #self.logger.addHandler(self.ch) #self.logger.propagate = False # We need to find out which addresses to put as SubjectAltNames (SAN) # in the certificate. # The server address might be given by the user in one of four ways: # Not specified, then url_address is the (possibly short) hostname # Specified as short hostname # Specified as FQDN # Specified as numeric IP address # We can always fill in the IP address ipaddr = socket.gethostbyname(self.url_address) self.SAN = "IP.1 = %s\n" % ipaddr fqdn = socket.getfqdn(self.url_address) # If address was specified as IP address and fqdn could not find a # hostname for it, we don't store it. Then only the IP address will # be given in the SAN list dns_counter = 1 if not fqdn == ipaddr: self.SAN += "DNS.%d = %s\n" % (dns_counter, fqdn) dns_counter += 1 # If the address was given as a short host name, or if # gethostname() produced a short host name, we store that if self.url_address != fqdn and self.url_address != ipaddr: self.SAN += "DNS.%d = %s\n" % (dns_counter, self.url_address) dns_counter += 1 # If localhost is given explicitly as the listen address, then # the above adds localhost to the SAN. If nothing is given, then # we listen on all addresses (including localhost), and we # should add "localhost" as a SAN. if self.address == "0.0.0.0": self.SAN += "DNS.%d = localhost\n" % dns_counter dns_counter += 1 self.SAN += "IP.2 = 127.0.0.1\n" self.bg = bg if use_db_pool: self.db_pool = wudb.DbThreadPool(dbdata, 1) else: self.db_pool = None # Generate a class with parameters stored in class variables upload_url_path = "/cgi-bin/upload.py" handler_params = { "registered_filenames": registered_filenames, "logger": self.logger, "dbdata": dbdata, "db_pool": self.db_pool, "uploaddir": uploaddir, "cgi_directories" : ['/cgi-bin'], "upload_path": upload_url_path, "only_registered": only_registered, "serving_wus": self.serving_wus, "timeout_hint": self.timeout_hint } MyHandlerWithParams = type("MyHandlerWithParams", (MyHandler, ), handler_params) # Find the upload.py script upload_path = self.findscript(upload_scriptname, scriptdir) # Always register the upload script if upload_path is None: raise IOError("%s script not found" % upload_scriptname) self.logger.debug("Found %s at %s" % (upload_scriptname, upload_path)) registered_filenames[upload_url_path.lstrip('/')] = upload_path # Set shell environment variables which the upload.py script needs if # spawned as subprocess os.environ[upload.DBURIKEY] = dbdata.uri os.environ[upload.UPLOADDIRKEY] = uploaddir if not os.path.isdir(uploaddir): os.mkdir(uploaddir) # See if we can use HTTPS scheme = "http" self.cert_sha1 = None if self.create_certificate(): self.cert_sha1 = self.get_certificate_hash() if not self.cert_sha1 is None: scheme = "https" addr = (self.address, port) try: if threaded and scheme == "http": self.logger.info("Using threaded HTTP server") self.httpd = ThreadedHTTPServer(addr, MyHandlerWithParams, whitelist=whitelist) elif not threaded and scheme == "http": self.logger.info("Using non-threaded HTTP server") self.httpd = FixedHTTPServer(addr, MyHandlerWithParams, whitelist=whitelist) elif threaded and scheme == "https": self.logger.info("Using threaded HTTPS server") self.httpd = ThreadedHTTPSServer(addr, MyHandlerWithParams, whitelist=whitelist, certfile=self.cafile) elif not threaded and scheme == "https": self.logger.info("Using non-threaded HTTPS server") self.httpd = HTTPSServer(addr, MyHandlerWithParams, whitelist=whitelist, certfile=self.cafile) else: assert False except socket_error as e: if e.errno == errno.EADDRINUSE: self.logger.critical("Address %s:%d is already in use (maybe " "another cadofactor running?)", address, port) self.logger.critical("You can choose a different port with " "server.port=<integer>.") sys.exit(1) else: self.logger.critical("Socket error while setting up server " "on %s:%d : %s", address, port, str(e)); sys.exit(1) self.port = self.httpd.server_address[1] self.url = "%s://%s:%d" % (scheme, self.url_address, self.port) self.url_loc = "%s://localhost:%d" % (scheme, self.port) self.httpd.server_name = self.name if self.address == "localhost" or self.httpd.server_address[0].startswith("127."): self.logger.warn("Server is listening on the loopback device. " "Clients on other hosts will not be able to connect.")
sysOpts = [opt] values[opt] = [ item.strip() for item in re.findall("%s:\w*(.*?)\n" % (opt), source, re.IGNORECASE) ][0] #return values["OS Name"] return values[opt] # return true if the log file exists = It's not first time running value = {} value["Hostname"] = os.environ['COMPUTERNAME'] value["IP"] = socket.gethostbyname(socket.gethostname()) value["Mac"] = ':'.join(re.findall('..', '%012x' % uuid.getnode())) value["AvailableMemory"] = SysInfo("Available Physical Memory") usage = psutil.disk_usage('/') value["HD-Percent"] = usage.percent logging.basicConfig(filename='./Inventory_log.log', level=logging.INFO, format='%(asctime)s %(message)s') logging.info('IP = %s, Mac = %s, AvailableMemory = %s, HD-Percent = %s', value["IP"], value["Mac"], value["AvailableMemory"],
def find_originating_country(hostname, country_code_list=None, orig_host=None): """ Find country associated with hostname, using whois """ if not hasattr(hostname, 'split') or '.' not in hostname or \ len(hostname.split('.')) < 2: return None if not orig_host: orig_host = hostname def _worker(hostname): output = [] ents = hostname.split('.') if len(ents) > 2 and country_code_list and ents[-1].upper() in \ country_code_list: return ents[-1].upper() pipe = Popen('whois %s' % hostname, shell=True, stdin=PIPE, stdout=PIPE, close_fds=True) wfile = pipe.stdout output = [l for l in wfile] pipe.wait() output = ''.join(['%s' % s.decode(errors='ignore') for s in output]) if 'Your connection limit exceeded. Please slow down and try again ' \ 'later.' in output or 'Timeout' in output: time.sleep(10) print(hostname) return find_originating_country( hostname, country_code_list=country_code_list, orig_host=orig_host) country = None for line in output.split('\n'): if 'country' in line or 'Country' in line: cn_ = line.split()[-1] if cn_ in country_code_list.values(): _dict = {v: k for (k, v) in country_code_list.items()} return _dict[cn_] cn_ = line.split()[-1][-2:].upper() if country != cn_: if country is not None: print('country? %s %s %s' % (country, cn_, hostname)) country = cn_ if 'Brazilian resource' in line: country = 'BR' if not country: if 'whois.nic.ad.jp' in hostname: country = 'JP' elif 'KOREAN' in output: country = 'KR' elif 'hinet.net' in hostname: country = 'CN' elif 'contabo.host' in hostname: country = 'DE' elif hostname.endswith('.eu'): country = 'FR' elif 'jp-east' in hostname: country = 'JP' return country country = _worker(hostname) if not country: try: tmp = gethostbyname(hostname) country = _worker(tmp) except gaierror: print('failed host %s' % hostname) raise if not country and hostname: return find_originating_country('.'.join(hostname.split('.')[1:]), country_code_list=country_code_list, orig_host=orig_host) return country
def getIpAddress(self): return socket.gethostbyname(self.getFqdn().lower())
""" Client Code """ import socket HEADER = 64 PORT = 5050 FORMAT = 'utf-8' DISCONNECT_MESSAGE = "!DISCONNECT" SERVER = socket.gethostbyname(socket.gethostname()) ADDR = (SERVER, PORT) client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) client.connect(ADDR) def send(msg): message = msg.encode(FORMAT) msg_length = len(message) send_length = str(msg_length).encode(FORMAT) send_length += b' ' * (HEADER - len(send_length)) client.send(send_length) client.send(message) print(client.recv(2048).decode(FORMAT)) send("Hello World!") input() send("Hello Everyone!") input()
def get_Hostname(): #print('Host Name is: {0}'.format(sc.gethostbyname(sc.gethostname()))) return sc.gethostbyname(sc.gethostname())
from socket import socket, gethostbyname, AF_INET, SOCK_STREAM target = "localhost" targetIP = gethostbyname(target) port = 80 s = socket(AF_INET, SOCK_STREAM) result = s.connect_ex((targetIP, port)) if (result == 0): print 'Port %d is open' % (port, ) s.close()
def _prepare_monitoring_fixture(): # upurl = urlparse(os.environ['SITEURL']) # net_scheme = upurl.scheme # net_loc = upurl.netloc pub_ip = _geonode_public_host_ip() print(f"Public Hostname or IP is {pub_ip}") pub_port = _geonode_public_port() print(f"Public PORT is {pub_port}") try: geonode_ip = socket.gethostbyname('geonode') except Exception: geonode_ip = pub_ip try: geoserver_ip = socket.gethostbyname('geoserver') except Exception: geoserver_ip = pub_ip d = '1970-01-01 00:00:00' default_fixture = [{ "fields": { "active": True, "ip": str(geonode_ip), "name": str(os.environ['MONITORING_HOST_NAME']) }, "model": "monitoring.host", "pk": 1 }, { "fields": { "active": True, "ip": str(geoserver_ip), "name": "geoserver" }, "model": "monitoring.host", "pk": 2 }, { "fields": { "name": str(os.environ['MONITORING_SERVICE_NAME']), "url": str(os.environ['SITEURL']), "notes": "", "last_check": d, "active": True, "host": 1, "check_interval": "00:01:00", "service_type": 1 }, "model": "monitoring.service", "pk": 1 }, { "fields": { "name": "geoserver-hostgeonode", "url": str(os.environ['SITEURL']), "notes": "", "last_check": d, "active": True, "host": 1, "check_interval": "00:01:00", "service_type": 3 }, "model": "monitoring.service", "pk": 2 }, { "fields": { "name": "geoserver-hostgeoserver", "url": str(os.environ['GEOSERVER_PUBLIC_LOCATION']), "notes": "", "last_check": d, "active": True, "host": 2, "check_interval": "00:01:00", "service_type": 4 }, "model": "monitoring.service", "pk": 3 }, { "fields": { "name": "default-geoserver", "url": "http://geoserver:8080/geoserver/", "notes": "", "last_check": d, "active": True, "host": 2, "check_interval": "00:01:00", "service_type": 2 }, "model": "monitoring.service", "pk": 4 }] with open('/tmp/default_monitoring_apps_docker.json', 'w') as fixturefile: json.dump(default_fixture, fixturefile)
""" M2M Lab Exercise 3 - Client that sends the TCP requests (Random Values of Temperature and Humidity) to the Server Built by - Shubh Pathak (MSM19B018) """ import socket,time,random PORT = 10001 SERVER = socket.gethostbyname(socket.gethostname()) # Automatically gets the Local IP Address server_address = (SERVER,PORT) client = socket.socket(socket.AF_INET,socket.SOCK_STREAM) client.connect(server_address) # Sends the request to Server def send(message): print(f"Sending {message} (Temperature,Humidity) to Server") message = message.encode('utf-8') client.send(message) if __name__ == '__main__': try: while True: temperature = random.randint(1,50) humidity = random.randint(0,100) message = f"{temperature},{humidity}" send(message)
def getHostIp(): """ returne current facing IP address """ return(socket.gethostbyname(socket.gethostname()))
def hostname_resolve(self, hostname): try: socket.gethostbyname(hostname) return True except socket.error: return False
resolver = dns.resolver.Resolver() resolver.timeout = 0.10 resolver.lifetime = 0.10 except: print('Error : Unable To Load dns Module.') print('For python3 : pip3 install dnspython3') print('For python2 : pip install dnspython3') sys.exit(0) # set api key SHODAN_API_KEY = "<api_key>" VIRUSTOTAL_API_KEY = "<api_key" # set url and ip DOMAIN = f"{argv[1]}" IP_Q = socket.gethostbyname(f'{DOMAIN}') # virustotal request headers = { "Accept-Encoding": "gzip, deflate", "User-Agent": "gzip, My Python requests library example client or username" } params = {'apikey': f'{VIRUSTOTAL_API_KEY}', 'resource': f'{DOMAIN}'} response = requests.post('https://www.virustotal.com/vtapi/v2/url/report', params=params, headers=headers) json_response = response.json() # shodan query with urllib.request.urlopen(
def update(ctx): print( "***************************setting env*********************************" ) ctx.run("env", pty=True) pub_ip = _geonode_public_host_ip() print(f"Public Hostname or IP is {pub_ip}") pub_port = _geonode_public_port() print(f"Public PORT is {pub_port}") pub_protocol = 'https' if pub_port == '443' else 'http' if pub_protocol == 'https' or pub_port == '80': pub_port = None db_url = _update_db_connstring() geodb_url = _update_geodb_connstring() service_ready = False while not service_ready: try: socket.gethostbyname('geonode') service_ready = True except Exception: time.sleep(10) override_env = "$HOME/.override_env" if os.path.exists(override_env): os.remove(override_env) else: print(f"Can not delete the {override_env} file as it doesn't exists") if pub_port: siteurl = f'{pub_protocol}://{pub_ip}:{pub_port}/' gs_pub_loc = f'http://{pub_ip}:{pub_port}/geoserver/' else: siteurl = f'{pub_protocol}://{pub_ip}/' gs_pub_loc = f'http://{pub_ip}/geoserver/' envs = { "local_settings": str(_localsettings()), "siteurl": os.environ.get('SITEURL', siteurl), "geonode_docker_host": str(socket.gethostbyname('geonode')), "public_protocol": pub_protocol, "public_fqdn": str(pub_ip) + str(f':{pub_port}' if pub_port else ''), "public_host": str(pub_ip), "dburl": os.environ.get('DATABASE_URL', db_url), "geodburl": os.environ.get('GEODATABASE_URL', geodb_url), "static_root": os.environ.get('STATIC_ROOT', '/mnt/volumes/statics/static/'), "media_root": os.environ.get('MEDIA_ROOT', '/mnt/volumes/statics/uploaded/'), "geoip_path": os.environ.get('GEOIP_PATH', '/mnt/volumes/statics/geoip.db'), "monitoring": os.environ.get('MONITORING_ENABLED', True), "monitoring_host_name": os.environ.get('MONITORING_HOST_NAME', 'geonode'), "monitoring_service_name": os.environ.get('MONITORING_SERVICE_NAME', 'local-geonode'), "monitoring_data_ttl": os.environ.get('MONITORING_DATA_TTL', 7), "geonode_geodb_passwd": os.environ.get('GEONODE_GEODATABASE_PASSWORD', 'geonode_data'), "default_backend_datastore": os.environ.get('DEFAULT_BACKEND_DATASTORE', 'datastore'), "geonode_db_passwd": os.environ.get('GEONODE_DATABASE_PASSWORD', 'geonode'), "geonode_geodb": os.environ.get('GEONODE_GEODATABASE', 'geonode_data'), "db_url": os.environ.get('DATABASE_URL', 'postgis://*****:*****@db:5432/geonode'), "geodb_url": os.environ.get('GEODATABASE_URL', 'postgis://*****:*****@db:5432/geonode_data'), "geonode_db": os.environ.get('GEONODE_DATABASE', 'geonode'), "gs_loc": os.environ.get('GEOSERVER_LOCATION', 'http://geoserver:8080/geoserver/'), "gs_web_ui_loc": os.environ.get('GEOSERVER_WEB_UI_LOCATION', gs_pub_loc), "gs_pub_loc": os.environ.get('GEOSERVER_PUBLIC_LOCATION', gs_pub_loc), "gs_admin_pwd": os.environ.get('GEOSERVER_ADMIN_PASSWORD', 'geoserver'), "override_fn": override_env } try: current_allowed = ast.literal_eval( os.getenv('ALLOWED_HOSTS') or "['{public_fqdn}', '{public_host}', 'localhost', 'django', 'geonode',]" .format(**envs)) except ValueError: current_allowed = [] current_allowed.extend([str(pub_ip), f'{pub_ip}:{pub_port}']) allowed_hosts = [str(c) for c in current_allowed] + ['"geonode"', '"django"'] ctx.run("echo export DJANGO_SETTINGS_MODULE=\ {local_settings} >> {override_fn}".format(**envs), pty=True) ctx.run("echo export MONITORING_ENABLED=\ {monitoring} >> {override_fn}".format(**envs), pty=True) ctx.run("echo export MONITORING_HOST_NAME=\ {monitoring_host_name} >> {override_fn}".format(**envs), pty=True) ctx.run("echo export MONITORING_SERVICE_NAME=\ {monitoring_service_name} >> {override_fn}".format(**envs), pty=True) ctx.run("echo export MONITORING_DATA_TTL=\ {monitoring_data_ttl} >> {override_fn}".format(**envs), pty=True) ctx.run("echo export GEOIP_PATH=\ {geoip_path} >> {override_fn}".format(**envs), pty=True) ctx.run("echo export GEONODE_GEODATABASE_PASSWORD=\ {geonode_geodb_passwd} >> {override_fn}".format(**envs), pty=True) ctx.run("echo export DEFAULT_BACKEND_DATASTORE=\ {default_backend_datastore} >> {override_fn}".format(**envs), pty=True) ctx.run("echo export GEONODE_DATABASE_PASSWORD=\ {geonode_db_passwd} >> {override_fn}".format(**envs), pty=True) ctx.run("echo export GEONODE_GEODATABASE=\ {geonode_geodb} >> {override_fn}".format(**envs), pty=True) ctx.run("echo export DATABASE_URL=\ {db_url} >> {override_fn}".format(**envs), pty=True) ctx.run("echo export GEODATABASE_URL=\ {geodb_url} >> {override_fn}".format(**envs), pty=True) ctx.run("echo export GEONODE_DATABASE=\ {geonode_db} >> {override_fn}".format(**envs), pty=True) ctx.run("echo export GEOSERVER_LOCATION=\ {gs_loc} >> {override_fn}".format(**envs), pty=True) ctx.run("echo export GEOSERVER_WEB_UI_LOCATION=\ {gs_web_ui_loc} >> {override_fn}".format(**envs), pty=True) ctx.run("echo export GEOSERVER_PUBLIC_LOCATION=\ {gs_pub_loc} >> {override_fn}".format(**envs), pty=True) ctx.run("echo export GEOSERVER_ADMIN_PASSWORD=\ {gs_admin_pwd} >> {override_fn}".format(**envs), pty=True) ctx.run("echo export SITEURL=\ {siteurl} >> {override_fn}".format(**envs), pty=True) ctx.run('echo export ALLOWED_HOSTS=\ "\\"{}\\"" >> {override_fn}'.format(allowed_hosts, **envs), pty=True) ctx.run("echo export DATABASE_URL=\ {dburl} >> {override_fn}".format(**envs), pty=True) ctx.run("echo export GEODATABASE_URL=\ {geodburl} >> {override_fn}".format(**envs), pty=True) ctx.run("echo export STATIC_ROOT=\ {static_root} >> {override_fn}".format(**envs), pty=True) ctx.run("echo export MEDIA_ROOT=\ {media_root} >> {override_fn}".format(**envs), pty=True) ctx.run("echo export GEOIP_PATH=\ {geoip_path} >> {override_fn}".format(**envs), pty=True) ctx.run("echo export LOGIN_URL=\ {siteurl}account/login/ >> {override_fn}".format(**envs), pty=True) ctx.run("echo export LOGOUT_URL=\ {siteurl}account/logout/ >> {override_fn}".format(**envs), pty=True) ctx.run("echo export LOGIN_REDIRECT_URL=\ {siteurl} >> {override_fn}".format(**envs), pty=True) ctx.run("echo export LOGOUT_REDIRECT_URL=\ {siteurl} >> {override_fn}".format(**envs), pty=True) ctx.run(f"source {override_env}", pty=True) print( "****************************finalize env**********************************" ) ctx.run("env", pty=True)
def main(initializer_module, heartbeat_stop_callback=None): global config global home_dir parser = OptionParser() parser.add_option("-v", "--verbose", dest="verbose", action="store_true", help="verbose log output", default=False) parser.add_option( "-e", "--expected-hostname", dest="expected_hostname", action="store", help= "expected hostname of current host. If hostname differs, agent will fail", default=None) parser.add_option("--home", dest="home_dir", action="store", help="Home directory", default="") (options, args) = parser.parse_args() expected_hostname = options.expected_hostname home_dir = options.home_dir logging_level = logging.DEBUG if options.verbose else logging.INFO setup_logging(logger, AmbariConfig.AmbariConfig.getLogFile(), logging_level) global is_logger_setup is_logger_setup = True setup_logging(alerts_logger, AmbariConfig.AmbariConfig.getAlertsLogFile(), logging_level) setup_logging(alerts_logger_2, AmbariConfig.AmbariConfig.getAlertsLogFile(), logging_level) setup_logging(alerts_logger_global, AmbariConfig.AmbariConfig.getAlertsLogFile(), logging_level) setup_logging(apscheduler_logger, AmbariConfig.AmbariConfig.getAlertsLogFile(), logging_level) setup_logging(apscheduler_logger_global, AmbariConfig.AmbariConfig.getAlertsLogFile(), logging_level) Logger.initialize_logger('resource_management', logging_level=logging_level) #with Environment() as env: # File("/abc") # init data, once loggers are setup to see exceptions/errors of initialization. initializer_module.init() if home_dir != "": # When running multiple Ambari Agents on this host for simulation, each one will use a unique home directory. Logger.info("Agent is using Home Dir: %s" % str(home_dir)) # use the host's locale for numeric formatting try: locale.setlocale(locale.LC_ALL, '') except locale.Error as ex: logger.warning( "Cannot set locale for ambari-agent. Please check your systemwide locale settings. Failed due to: {0}." .format(str(ex))) default_cfg = {'agent': {'prefix': '/home/ambari'}} config.load(default_cfg) if (len(sys.argv) > 1) and sys.argv[1] == 'stop': stop_agent() if (len(sys.argv) > 2) and sys.argv[1] == 'reset': reset_agent(sys.argv) # Check for ambari configuration file. resolve_ambari_config() # Add syslog hanlder based on ambari config file add_syslog_handler(logger) # Starting data cleanup daemon data_cleaner = None if config.has_option('agent', 'data_cleanup_interval') and int( config.get('agent', 'data_cleanup_interval')) > 0: data_cleaner = DataCleaner(config) data_cleaner.start() perform_prestart_checks(expected_hostname) # Starting ping port listener try: #This acts as a single process machine-wide lock (albeit incomplete, since # we still need an extra file to track the Agent PID) ping_port_listener = PingPortListener(config) except Exception as ex: err_message = "Failed to start ping port listener of: " + str(ex) logger.error(err_message) sys.stderr.write(err_message) sys.exit(1) ping_port_listener.start() update_log_level(config) update_open_files_ulimit(config) if not config.use_system_proxy_setting(): logger.info('Agent is configured to ignore system proxy settings') #reconfigure_urllib2_opener(ignore_system_proxy=True) if not OSCheck.get_os_family() == OSConst.WINSRV_FAMILY: daemonize() # # Iterate through the list of server hostnames and connect to the first active server # active_server = None server_hostnames = hostname.server_hostnames(config) connected = False stopped = False # Keep trying to connect to a server or bail out if ambari-agent was stopped while not connected and not stopped: for server_hostname in server_hostnames: server_url = config.get_api_url(server_hostname) try: server_ip = socket.gethostbyname(server_hostname) logger.info('Connecting to Ambari server at %s (%s)', server_url, server_ip) except socket.error: logger.warn( "Unable to determine the IP address of the Ambari server '%s'", server_hostname) # Wait until MAX_RETRIES to see if server is reachable netutil = NetUtil(config, initializer_module.stop_event) (retries, connected, stopped) = netutil.try_to_connect(server_url, MAX_RETRIES, logger) # if connected, launch controller if connected: logger.info('Connected to Ambari server %s', server_hostname) # Set the active server active_server = server_hostname # Launch Controller communication run_threads(initializer_module) # # If Ambari Agent connected to the server or # Ambari Agent was stopped using stop event # Clean up if not Windows OS # if connected or stopped: ExitHelper().exit() logger.info("finished") break pass # for server_hostname in server_hostnames pass # while not (connected or stopped) return active_server
import socket import tqdm ns = list() with open("ns.txt", "r") as nsf: for l in nsf: ns.append(l.split(" ")[-1]) for i in tqdm.tqdm(set(ns)): nsaddr = i.strip()[:-1].lower() try: socket.gethostbyname(nsaddr) except socket.gaierror: print("unable to get address for", nsaddr)
import os import socket sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) local_hostname = socket.gethostname() local_fqdn = socket.getfqdn() ip_addr = socket.gethostbyname(local_hostname) print(f'Local hostname: {local_hostname}') print(f'Local FQDN: {local_fqdn}') print(f'Ip Address: {ip_addr}') server_addr = (ip_addr, 23456) sock.connect(server_addr) filename = 'testfile.txt' script_dir = os.path.dirname(__file__) rel_path = f'receive/{filename}' abs_file_path = os.path.join(script_dir, rel_path) with open(abs_file_path, 'wb') as f: print('file opened') while True: print('receiving data') data = sock.recv(64) print(f'data={data}') if not data: break f.write(data)
from os import listdir import os from os.path import isfile, join #Create socket object serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) host = socket.gethostname() print("Socket successfully created on host %s" % host) port = 55555 ip = '192.168.1.44' #Computer's local ip address serversocket.bind(('', port)) time = strftime("%Y-%m-%d %H:%M:%S", gmtime()) print(time, "- Socked bound to port: ", port) print("IP: ", socket.gethostbyname(host)) serversocket.listen(5) sockapp_lock = threading.Lock() clientsockets = {} path = "C:\\Users\\Kevin\\Desktop\\Host" def timeNow(): return strftime("%Y-%m-%d %H:%M:%S", gmtime()) #Gets all the files in the host directory def getFilesInHost(): files = [] for f in listdir(path):
def importLoanUser(self): core_user='******' core_auth_card='core_auth_card' core_bank_card='core_bank_card' module_user = '******' module_identy = 'module_identity_card' parserFile = ParserFile('../storages/files/'+self.file) start_time = datetime.datetime.now() #获取解析的execl文件的内容 execl_data = parserFile.parser_execl() if execl_data is None: print(self.common.log_template%('Error', 'execl is empty!')) j = m = 0 faild_loan_user = [] # 获取银行卡ID方法 parser_yaml = ParserYaml() methods = parser_yaml.get_bank_id_method() for i in range(1, execl_data.nrows): try: # 获取每一行的数据 line = execl_data.row_values(i) if line is None: continue #检测手机号 self.common.checkPhone(int(line[2])) # 插入用户表借款人信息 sql = '''insert into `%s` (phone, real_name, identity_card,password_hash,trading_password, note) VALUES ('%s', '%s', '%s','','','jx_loan_user')''' % (core_user, int(line[2]), line[0], line[1]) print(self.common.log_template % ('SQL', sql)) self.cursor.execute(sql) # 获取最新的ID信息 sql = '''select last_insert_id() as id''' print(self.common.log_template % ('SQL', sql)) self.cursor.execute(sql) result = self.cursor.fetchone() print(self.common.log_template % ('Get the auto_increment user_id', result['id'])) # 获取银行卡ID func = 'getBankIdBy'+methods bank_id = getattr(self, func)(line[3]) # 插入到充值银行卡表中 sql = '''insert into `%s` (user_id, bank_id, card_number, created_at, updated_at) VALUES (%d,%d,'%s','%s', '%s')''' % (core_auth_card, result['id'], bank_id, line[3], self.common.timestamp, self.common.timestamp) print(self.common.log_template % ('SQL', sql)) self.cursor.execute(sql) # 插入到提现银行卡表中 sql = '''insert into `%s` (user_id, bank_id, card_number, created_at, updated_at) VALUES (%d,%d, '%s','%s', '%s')''' % (core_bank_card, result['id'], bank_id, line[3],self.common.timestamp,self.common.timestamp) print(self.common.log_template % ('SQL', sql)) self.cursor.execute(sql) self.db.commit() # 插入到user_info sql = ''' insert into `%s` (username, user_id, ip, source_code) VALUES ('%s', %d, '%s',1)''' % ( module_user, int(line[2]), result['id'], socket.gethostbyname(socket.gethostname())) print(self.common.log_template % ('SQL', sql)) self.module_cursor.execute(sql) # 插入到identity_card表中 sql = '''insert into `%s` (name, identity_card) VALUES ('%s', '%s')''' % ( module_identy, line[0], line[1]) print(self.common.log_template % ('SQL', sql)) self.module_cursor.execute(sql) self.module_db.commit() j = j + 1 print("\n") except Exception as e: print(self.common.log_template % ('Exception', str(e))) m = m + 1 line.append(str(e)) faild_loan_user.append(line) self.db.rollback() self.module_db.rollback() #失败的信息导出 if len(faild_loan_user) > 0: faild_loan_user.insert(0,['姓名','身份证号','手机','银行卡号','失败原因']) parserFile = ParserFile('../storages/files/failed_loan_user_%s.xls'%self.common.file_extends_time) parserFile.export_data_to_execl(faild_loan_user) print(self.common.log_template%('msg', 'failed data export compelete!')) end_time = datetime.datetime.now() print("insert loan user data compelete, successly %s lines, faild %s lines, total %s lines, spent about %s seconds"%(j,m,execl_data.nrows-1, (end_time-start_time).seconds))
def getselfip(): import socket return socket.gethostbyname(socket.gethostname())
import re import random import datetime import time import webbrowser import sys import platform import json import requests import socket import birthdayReminder import google hostname = socket.gethostname() IPAddr = socket.gethostbyname(hostname) os=platform.system() date = [] today = str(datetime.date.today()) date.append(today) localtime = time.asctime( time.localtime(time.time()) ) value = 0 songs = ["Freaky Friday-Lil Dicky ft.Chris Brown","Delicate-Taylor Swift"] food = ["Sambhar","Idli","Dosa"] class Tracy: def __init__(self): self.keys = list(map(lambda x:re.compile(x[0], re.IGNORECASE),gPats)) self.values = list(map(lambda x:x[1],gPats)) #---------------------------------------------------------------------- # translate: take a string, replace any words found in dict.keys()
import socket import sys try: s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) except socket.error as error: print('It Broke Cuz') host = input("Imput the IP or Hostname: ") hostIP = socket.gethostbyname(host) port_1 = input("Enter the First port: ") port_1 = int(port_1) port_2 = input("Enter the Last Port: ") port_2 = int(port_2) print(hostIP + " Is Your Target") def pscan(port): try: s.connect((hostIP, port)) s.shutdown(2) return (True) except: return (False) def main(): for x in range(port_1, port_2): if pscan(x):