def start(self): try: self.private_pyro4ns = Pyro4.locateNS() print("NameServer already working") sys.exit(1) except Exception: self.priv_ns_t = threading.Thread( name="Private NameServer", target=self.create_nameserver, kwargs={'passw': self.config["nameserver_password"]}) self.priv_ns_t.start() # Thread-1 started time.sleep(1) self.private_pyro4ns = Pyro4.locateNS( host="localhost", hmac_key=self.config["nameserver_password"].encode( )) # Private NS ip = utils.get_ip_address(ifname=self.config["interface"]) self.pub_ns_t = threading.Thread(name="Public NameServer", target=self.create_nameserver, kwargs={ 'host': ip, }) self.pub_ns_t.start() time.sleep(1) self.public_pyro4ns = Pyro4.locateNS(host=ip) # Public NS
def __init__(self, run_local, port=0, client=False, callbacks={}): global networkClients global networkCallbacks self.port = port self.clients = networkClients self.callbacks = callbacks networkCallbacks = callbacks if not client: try: self.host = utils.get_ip_address() except: self.host = "127.0.0.1" print("failed to get local host name") if run_local: self.host = "127.0.0.1" for i in range(100): try: self.server = ThreadedTCPServer((self.host, self.port), ThreadedTCPRequestHandler) break except: self.port += 1 print("couldn't get requested port, trying %s"%(self.port+1)) print("Server running on %s %s"%(self.host, self.port)) # Start a thread with the server -- that thread will then start one # more thread for each request self.server_thread = threading.Thread(target=self.server.serve_forever) # Exit the server thread when the main thread terminates self.server_thread.setDaemon(True)
def create_pyro_proxy(self): """Create proxy to make connections to BigBrother. Gets the network address of the network interface indicated in json file. Obtain a connection port from the port indicated in json file. Next, it creates a daemon that is the connection proxy to BigBrother and registers itself. It is working in the background listening to requests. """ try: myip = utils.get_ip_address(ifname=self.config["interface"]) myport = utils.get_free_port(self.config["proxy_port"], ip=myip) daemon = Pyro4.Daemon(host=myip, port=myport) daemon._pyroHmacKey = self.config["proxy_password"].encode() daemon.PYRO_MAXCONNECTIONS = 20 self.uri = daemon.register(self, objectId="bigbrother") print( colored("\nBigBrother running : {}".format(self.uri), 'green')) self.public_pyro4ns.register("bigbrother", self.uri) daemon.requestLoop() except Exception: print("Error creating proxy on interface", self.config["interface"]) raise
def run(self): """Thread main loop""" while (app.running): time.sleep(self.poll_interval) if not hasattr(settings, 'server_url'): continue try: log.debug("Calling home...") data = { 'playlist': settings.content.playlist_name, 'mac_address': self.mac_address, 'ip_address': utils.get_ip_address(), 'cpu_freq': utils.get_cpu_freq(), 'cpu_temp': utils.get_cpu_temp(), 'cpu_usage': utils.get_cpu_usage(), 'browser_ram': utils.get_pid_rss(self.browser.uzbl.pid), 'uptime': utils.get_uptime() } if self.send_logs: data['logs'] = '\n'.join(utils.get_log_entries()) self.send_logs = False reply = self.call_home(data) log.debug("Got reply %s" % reply) self.do_clock(reply) try: method = getattr(self, 'do_' + reply['action']) except AttributeError: log.debug("Cannot handle reply %s", reply) if app.running: # state may have changed in the meantime method(reply['data']) except Exception as e: log.debug("Got %s while calling home" % e) pass log.info("Exiting beacon thread.")
def run(self): """Thread main loop""" while(app.running): time.sleep(self.poll_interval) if not hasattr(settings,'server_url'): continue try: log.debug("Calling home...") data = { 'playlist' : settings.content.playlist_name, 'mac_address' : self.mac_address, 'ip_address' : utils.get_ip_address(), 'cpu_freq' : utils.get_cpu_freq(), 'cpu_temp' : utils.get_cpu_temp(), 'cpu_usage' : utils.get_cpu_usage(), 'browser_ram' : utils.get_pid_rss(self.browser.uzbl.pid), 'uptime' : utils.get_uptime() } if self.send_logs: data['logs'] = '\n'.join(utils.get_log_entries()) self.send_logs = False reply = self.call_home(data) log.debug("Got reply %s" % reply) self.do_clock(reply) try: method = getattr(self, 'do_' + reply['action']) except AttributeError: log.debug("Cannot handle reply %s", reply) if app.running: # state may have changed in the meantime method(reply['data']) except Exception as e: log.debug("Got %s while calling home" % e) pass log.info("Exiting beacon thread.")
def run_server(config, peer_info, peer_id): # Open a socket sock = None while not sock: try: sock = utils.listen4clients( (utils.get_ip_address(), config["peer"][peer_id]["port"])) except socket_error as err: utils.Logging.debug( "Tried to open a socket. %s. Retrying in 3 seconds." % err) sleep(3) utils.Logging.info("Peer server is now running") # Serve incoming connections until failure while True: try: utils.Logging.info("\n\t***** We are listening for requests *****") connection, request = utils.accept_request(sock) utils.Logging.info( "\n\t***** Spawning a new thread the serve the new request *****" ) t = utils.FuncThread(process_request, connection, peer_info, request) t.setDaemon(True) t.start() except KeyboardInterrupt: break except BaseException as err: utils.Logging.info(err) sock.close()
def init_socket(iface, timeout=None): try: s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) except: # Catch: "error: [Errorno 24] too many open files" # i.e. reached max number of socket which can be opened return None # Socket configurations s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) if timeout: s.settimeout(timeout) # Connect try: s.bind((get_ip_address(iface), 0)) s.connect((HOST, PORT)) except (IOError, socket.error) as e: # IOError is caught when you try to assign a IP addres # to a virtual network interface that is already used. #sys.exit(str(e)) # Try again time.sleep(1) s = init_socket(iface, timeout) return s
def decrypt_arp_packet(pkt): if __name__ == '__main__': send_socket = create_send_socket() recv_socket = create_recv_socket() gateway_ip = get_gateway_ip() localhost_mac = mac_of_localhost() #Get the Localhost IP localhost_ip = get_ip_address('eth0') # print localhost_mac+' '+str(len(localhost_mac)) # print localhost_ip+' '+str(len(localhost_ip)) # print gateway_ip+' '+str(len(gateway_ip)) arp_packet = construct_arp_packet(localhost_mac,localhost_ip,gateway_ip) ethernet_packet = construct_ethernet_packet(localhost_mac,'ffffffffffff', 0x0806, arp_packet) send_socket.sendto(ethernet_packet,('eth0',0)) gate_mac = '' while True: pkt = recv_socket.recvfrom(4096) [dst,src,data] = decrypt_ethernet_packet(pkt, localhost_mac) ##### ELSE BREAK AFTER TIMEOUT ---- ######## if src == localhost_mac: print dst gate_mac = dst decrypt_arp_packet(data)
def login(self, username, password): # see https://wiki.earthdata.nasa.gov/display/CMR/CMR+Client+Partner+User+Guide#CMRClientPartnerUserGuide-CreatingaToken if self.token is not None: logging.warning('Already logged into %s' % self.base_url) return True url = self.base_url + '/legacy-services/rest/tokens' # self.ip = checkip() self.ip = get_ip_address() data = { 'token': { 'username': username, 'password': password, 'user_ip_address': self.ip, 'client_id': 'WRFXPY' } } try: r = requests.post(url, data=json2xml(data), headers=self.headers) except Exception as e: logging.error(e) return False if r.status_code == 201: try: self.token = xmltodict.parse(r.text)['token']['id'] logging.info('Successfully logged into %s, token=%s' % (self.base_url, self.token)) return True except: logging.error('%s did not return a token' % self.base_url) else: logging.error('%s return code %s' % (self.base_url, r.status_code)) logging.error('Cannot log into %s' % self.base_url) return False
def __init__(self, run_local, port=0, client=False, callbacks={}): global networkClients global networkCallbacks self.port = port self.clients = networkClients self.callbacks = callbacks networkCallbacks = callbacks if not client: try: self.host = utils.get_ip_address() except: self.host = "127.0.0.1" print("failed to get local host name") if run_local: self.host = "127.0.0.1" for i in range(100): try: self.server = ThreadedTCPServer((self.host, self.port), ThreadedTCPRequestHandler) break except: self.port += 1 print("couldn't get requested port, trying %s" % (self.port + 1)) print("Server running on %s %s" % (self.host, self.port)) # Start a thread with the server -- that thread will then start one # more thread for each request self.server_thread = threading.Thread( target=self.server.serve_forever) # Exit the server thread when the main thread terminates self.server_thread.setDaemon(True)
def __init__(self, *args, **kwargs): Spider.__init__(self, *args, **kwargs) self.worker_id = "%s_%s" % (socket.gethostname(), get_ip_address()) self.worker_id = self.worker_id.replace('.', '-') self.gen_field = self._yield_field() self.base_item_cls = type("RawResponseItem", (Item, ), dict(zip(BASE_FIELD, self.gen_field)))
def _first(self): to_save = False if not self.gts('ip'): self['settings']['ip'] = utils.get_ip_address() to_save = True if not self['majordomo'].get('ip'): self._print(LNG['say_ip'].format(self.gts('ip')), logger.WARN, 3) return to_save
def _first(self): to_save = False if 'ip' not in self or not self['ip']: self['ip'] = utils.get_ip_address() to_save = True if 'ip_server' not in self or not self['ip_server']: self._print( 'Терминал еще не настроен, мой IP адрес: {}'.format( self['ip']), logger.INFO, 3) return to_save
def server_discover(loop, magic=DiscoveryConfig.MAGIC, listen_ip=DiscoveryConfig.IP, port=DiscoveryConfig.PORT, password=DiscoveryConfig.PASSWORD, disable_hidden=False): server_ip = get_ip_address() logger.info('Starting Discover Server at port %s', port) if password: password = prepare_text(password) config = dict() config[KEYS.GRPC_SERVER_PORT] = GRPCConfig.PORT config[KEYS.GRPC_SERVER_IP] = get_ip_address() _answer = json.dumps(config) # Setup Protocol DiscoverServerProtocol.magic = magic DiscoverServerProtocol.server_ip = server_ip DiscoverServerProtocol.password = password DiscoverServerProtocol.disable_hidden = disable_hidden DiscoverServerProtocol.answer = _answer transport = None try: # Start running the server listen = loop.create_datagram_endpoint(DiscoverServerProtocol, local_addr=(listen_ip, port), allow_broadcast=True) transport, protocol = loop.run_until_complete(listen) loop.run_forever() except: pass finally: logger.info('Shutting down Discovery Server') if transport is not None: transport.close() loop.close()
def _lms_get_one_player(self, uid: str, players): ip = get_ip_address() first, my_ip = None, None for player in players: if uid and self._lms_player_match(player, uid): return player if my_ip is None and player.ip_address.split(':', 1)[0] == ip: my_ip = player if first is None: first = player my_ip = my_ip or first if not my_ip: raise RuntimeError('No players found') return my_ip
def display_ip(): try: ip = get_ip_address('wlan0') print(ip) for addr in ip.split('.'): img = Image.new('RGB', (60, 30), color='black') d = ImageDraw.Draw(img) d.text((10, 10), addr, fill=(255, 255, 255)) img.save('/tmp/ip.png') ip_image = imageio.imread('/tmp/ip.png') update_screen(ip_image) time.sleep(1) except: print('error displaying ip')
def _get_ip_info(type): client = AGENT_CLIENT(AGENT_ADDR, AGENT_PORT) resp = json.loads(client.get_network_general()) if resp["responseCode"] != 200: return "" if type == "address": return utils.get_ip_address( str(resp["network_general"]["niccard_name"])) elif type == "gateway": return str(resp["network_general"]["defaultgw"]) elif type == "dns": ret = "" for dns in resp["network_general"]["dns"]: ret += str(dns) + "\n" return ret else: return ""
def main(): print("Getting IP address") ip_address = get_ip_address('wlan0') print("IP address " + ip_address) ser = serial.Serial('/dev/ttyUSB0', 9600) print("Serial connected ") renderer_client = RendererClient(ip_address) print("Connecting MQTT client") mqtt_client = MqttClient(on_connect, on_message, (renderer_client, ser)) print("Proceeed to drawing part") while (True): sensorData = ser.readline() while (sensorData): renderer_client.setSensorData(sensorData) mqtt_client.publish("SENSOR", renderer_client.getState()) sensorData = ser.readline()
def execute_agent(): _, _, grpc_server_ip, grpc_server_port = discover( magic=DiscoveryConfig.MAGIC, port=DiscoveryConfig.PORT, password=DiscoveryConfig.PASSWORD, timeout=5) cpu_infos, python_info = system_info_gatherer() grpc_server = '%s:%s' % (grpc_server_ip, grpc_server_port) logger.info('Connecting to GRPC server at %s', grpc_server) channel = grpc.insecure_channel(grpc_server) # create a stub (client) stub = master_pb2_grpc.MasterStub(channel) agent_info.stub = stub # create a valid request message network_info = models_pb2.NetworkInfo(ip_address=get_ip_address()) system_info = models_pb2.SystemInfo(hostname=get_machine_name(), cpu_infos=cpu_infos, python_info=python_info, network_info=network_info) register_agent_request = master_pb2.RegisterAgentRequest( system_info=system_info) # make the call response = stub.RegisterAgent(register_agent_request) agent_info.unique_id = response.client_id logger.warning(response) loop = asyncio.get_event_loop() task = loop.create_task(send_heartbeat(agent_info)) try: loop.run_until_complete(task) except asyncio.CancelledError: pass
def run(self, sessionbus=False): dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) if not sessionbus: bus = dbus.SystemBus() else: bus = dbus.SessionBus() bus_name = dbus.service.BusName(DBUS_BUS_NAME, bus) dbus.service.Object.__init__(self, bus_name, DBUS_OBJECT_PATH) self._loop = GObject.MainLoop() # Prepare changes listener self.webservice = Soup.Server() try: address = Gio.InetSocketAddress.new_from_string( get_ip_address(self.webservice_host), self.webservice_port) self.webservice.listen(address, 0) if self.webservice_port == 0: listeners = self.webservice.get_listeners() inetsocket = listeners[0].get_local_address() self.webservice_port = inetsocket.get_port() except Exception, e: logging.error('Error starting webservice: %s' % e) sys.exit(1)
def safe_get_ip_address(): try: return get_ip_address() except: return "127.0.0.1"
import bottle, utils # read configuration file and setup various globals, including logging formats from config import settings import gtk, pygtk # Set up logging log = logging.getLogger() # validate framebuffer settings settings = utils.check_resolution(settings) # setup static file root staticroot = utils.path_for('static') # check if we have a valid IP address ip_address = utils.get_ip_address(settings.interface) # Shared data used by other modules version = '0.13.08.02.1' # Flag for controlled thread termination running = True # Screen state sent from server screen = {} # Local URI Prefix local_uri = 'http://%s:%s' % (settings.http.bind_address, settings.http.port) window = gtk.Window() screen = window.get_screen() if not settings.staging: settings.screen.width = int(screen.get_width()) settings.screen.height = int(screen.get_height())
HOST = "10.0.0.1" USER = "******" PASSWORD = "" DB = "ssh_botnet" # Network interface IFACE = "eth0" # Connect to database db = MySQLdb.connect(host=HOST, user=USER, passwd=PASSWORD, db=DB) # Create cursor object (this allows you to execute all the queries you need) cur = db.cursor() # Get IP address of eth0 interface ip = utils.get_ip_address(IFACE) # SQL insert replace = "REPLACE INTO bot_ip (ip, status) VALUES('" + ip + "', 1);" cur.execute(replace) """ # Check result before commiting select = "SELECT * FROM bot_ip" cur.execute(select) for row in cur.fetchall(): print row """ # Commit db.commit()
try: LOG.info('Gevent approaching ... server ip:%s port:%s', self._ip, self._port) app = API() server = Server(app, host=self._ip, port=self._port) server.start() server.wait() except Exception as e: LOG.error('Exception: %s' % e) LOG.error('%s' % traceback.format_exc()) sys.exit(1) def register_options(self): config.register_interface_driver_opts_helper(cfg.CONF) config.register_use_namespaces_opts_helper(cfg.CONF) config.register_agent_state_opts_helper(cfg.CONF) cfg.CONF.register_opts(dhcp_config.DHCP_AGENT_OPTS) cfg.CONF.register_opts(dhcp_config.DHCP_OPTS) cfg.CONF.register_opts(dhcp_config.DNSMASQ_OPTS) cfg.CONF.register_opts(interface.OPTS) cfg.CONF.register_cli_opts(common_cli_opts) if __name__ == '__main__': local_ctrl_ip = get_ip_address("nspbr0") main = DeamonMain(local_ctrl_ip, "20010", '/var/dhcpagent/pid') if cfg.CONF.daemon: main.start() else: main.run()
'''设置logging记录日志''' FORMAT = '%(asctime)-15s %(clientip)-15s %(levelname)-8s %(module)-20s %(funcName)-15s %(message)s' # noqa DATE_FORMAT = '%Y-%m-%d %H:%M:%S' formatter = Formatter(fmt=FORMAT, datefmt=DATE_FORMAT) handler = StreamHandler() sockethandler = logging.handlers.SocketHandler(SERVER_HOST, logging.handlers.DEFAULT_TCP_LOGGING_PORT) # noqa handler.setFormatter(formatter) for_logger = getLogger('Tencent') for_logger.setLevel(DEBUG) for_logger.addHandler(handler) for_logger.addHandler(sockethandler) return for_logger # 添加自定义的客户端ip字段 d = {'clientip': get_ip_address()} logger = logger() debug = partial(logger.debug, extra=d) info = partial(logger.info, extra=d) warn = partial(logger.warn, extra=d) # error类型的日志记录堆栈 error = partial(logger.error, exc_info=1, extra=d) def handle_log(socket, address): '''搜集各client日志到服务端''' chunk = socket.recv(4) if len(chunk) < 4: return slen = struct.unpack('>L', chunk)[0]
return flask.render_template('todo.html', pos=pos, todos=utils.calc_todo(pos, connection)) @app.route('/BETA') def beta_home(): return flask.render_template('beta-home.html', pos=[{ "id": 1, "company": "hello world", "item": "mug", "clay_type": "white", "glaze_color": "green", "amount": 1000, "buffer": .1, "description": "test", "miscellaneous": "misc", "percent_complete": 50, "delivery_date": 20180101 }]) if __name__ == '__main__': connection = psycopg2.connect(host='localhost', user='******', password='******', database='jonathanmartin') app.secret_key = 'black_oak' app.run(host=utils.get_ip_address(), debug=True)
def send_power_cycle_email(station_parameters): # Get the hostname hostname = socket.gethostname() # Get the MAC address mac_address = utils.get_mac_address('-') # Get the IP address ip_address = utils.get_ip_address() if ip_address is None: ip_address = 'n/a' # Get the current universal coordinated time (UTC) iso8601_time_string = clock.iso8601_time_string_using_computer_clock() # Form the message message = 'From: {0}\n'.format(station_parameters['emailSender']) if type(station_parameters['emailReceivers']) is list: r = str(station_parameters['emailReceivers'])[1:-1] r = r.replace("'", "").replace('"', '') message += 'To: {0}\n'.format(r) else: message += 'To: {0}\n'.format(station_parameters['emailReceivers']) message += 'Subject: *** IMPORTANT *** Camera Power Cycle Occurred ' message += '({0}) '.format(mac_address) message += '[{0}]\n'.format(station_parameters['stationName']) message += '\n' message += 'Hostname: {0}\n'.format(hostname) message += 'Station name: {0}\n'.format(station_parameters['stationName']) message += 'MAC: {0}\n'.format(mac_address) message += 'IP address: {0}\n'.format(ip_address) message += '\n' message += 'Time: {0}\n'.format(iso8601_time_string) message += '\n' message += 'A power cycle was just completed on the camera attached to ' message += 'this station. You should check the operations log to make ' message += 'sure that normal operations have resumed.\n' message += '\n' message += 'If normal operations have not resumed, you may want to ' message += 'consider:\n' message += '\n' message += ' 1) Terminating and restarting the collection script\n' message += ' 2) Power cycling the camera again manually\n' message += ' 3) Rebooting the controlling computer\n' # Send the message smtp = smtplib.SMTP() try: smtp.connect(station_parameters['smtpServer']) except socket.gaierror: msg = '*** ERROR *** SMTP server address is invalid or could not be ' msg += 'resolved' msg += '\n' sys.stderr.write(msg) sys.stderr.flush() sys.exit() except: msg = '*** WARNING *** Unable to connect to SMTP server to send' msg += 'power cycle message' msg += '\n' msg += '... aborting attempt' msg += '\n' msg += '\n' sys.stdout.write(msg) sys.stdout.flush() return try: smtp.sendmail(station_parameters['emailSender'], station_parameters['emailReceivers'], message) except smtplib.SMTPException: msg = '*** WARNING *** Unable to send power cycle message' msg += '\n' msg += '... aborting attempt' msg += '\n' msg += '\n' sys.stdout.write(msg) sys.stdout.flush() return
def setUp(self): """ Init assert conts """ # host meta info self.hostname = utils.get_host_name() self.hostip = utils.get_ip_address("br-ex") # nagios base dir self.base_dir = "/usr/local/nagios/" # bins checks # nagios core bin check self.core_bin = ["bin/nagios"] # nagios nrpe bin check self.nrpe_bin = ["bin/nrpe"] # nagios ndo2db bin check self.ndo2db_bin = ["bin/ndo2db", "bin/ndomod.o"] # nagios plugins check self.plugin_bin =[ "libexec/check_cpu.sh", "libexec/check_disk", "libexec/check_diskstat.sh", "libexec/check_mem.sh", "libexec/check_net.py", "libexec/check_procs" ] # cfgs check # nagios core cfgs check self.core_cfg = [ "etc/cgi.cfg", "etc/nagios.cfg", "etc/resource.cfg", "etc/objects/hosts/"+self.hostname+".cfg" ] # nagios ndo2db cfgs check self.ndo2db_cfg = [ "etc/ndo2db.cfg", "etc/ndomod.cfg" ] # nagios nrpe cfgs check self.nrpe_cfg = [ "etc/nrpe.cfg" ] # setting checks # nagios core set check # nagios ndo2db set check self.kernMsgmnb = '16384000' self.kernMsgmni = '16384000' self.db_name = 'db_name=nagios' self.db_user = '******' self.db_pass = '******' self.broker = 'broker_module' # nagios nrpe set check # nagios plugin set check # service checks # nagios initd checks self.initd = [ "/etc/init.d/nagios", "/etc/init.d/ndo2db", "/etc/xinetd.d/nrpe" ] # service process check self.service_ps = { "core": '/usr/local/nagios/bin/nagios', "ndo2db": '/usr/local/nagios/bin/ndo2db', "nrpe": '/usr/sbin/xinetd' } # service ports check self.service_port = { "nrpe": 5666 }
FORMAT = '%(asctime)-15s %(clientip)-15s %(levelname)-8s %(module)-20s %(funcName)-15s %(message)s' # noqa DATE_FORMAT = '%Y-%m-%d %H:%M:%S' formatter = Formatter(fmt=FORMAT, datefmt=DATE_FORMAT) handler = StreamHandler() sockethandler = logging.handlers.SocketHandler( SERVER_HOST, logging.handlers.DEFAULT_TCP_LOGGING_PORT) # noqa handler.setFormatter(formatter) for_logger = getLogger('Tencent') for_logger.setLevel(DEBUG) for_logger.addHandler(handler) for_logger.addHandler(sockethandler) return for_logger # 添加自定义的客户端ip字段 d = {'clientip': get_ip_address()} logger = logger() debug = partial(logger.debug, extra=d) info = partial(logger.info, extra=d) warn = partial(logger.warn, extra=d) # error类型的日志记录堆栈 error = partial(logger.error, exc_info=1, extra=d) def handle_log(socket, address): '''搜集各client日志到服务端''' chunk = socket.recv(4) if len(chunk) < 4: return slen = struct.unpack('>L', chunk)[0]
def __init__(self): TCPServer.__init__(self, (utils.get_ip_address(), utils.CAR_PORT), CarRequestHandler) self._kill = False self._queue = Queue()
sys.path.insert(0, 'lib') # prefer bundled libraries to local installs import bottle, utils # read configuration file and setup various globals, including logging formats from config import settings # Set up logging log = logging.getLogger() # validate framebuffer settings settings = utils.check_resolution(settings) # setup static file root staticroot = utils.path_for('static') # check if we have a valid IP address ip_address = utils.get_ip_address(settings.interface) # Shared data used by other modules version = '0.13.08.02.1' # Flag for controlled thread termination running = True # Screen state sent from server screen = {} # Local URI Prefix local_uri = 'http://%s:%s' % (settings.http.bind_address, settings.http.port) # Defaults sent to templates template_vars = { 'version': version, 'ip_address': ip_address, 'width': settings.screen.width, 'height': settings.screen.height,
def _restore_appliance(app_filename): tmp_folder = tempfile.mkdtemp(prefix="backup.", dir="/tmp") (ret, output) = utils.app_command("tar xfvz %s -C %s" % (app_filename, tmp_folder)) current_path = os.getcwd() os.chdir(tmp_folder) app_filename = os.path.basename(app_filename) device_type = utils.get_device_type() # copy networking files to some place, _post_restore will use them cur_dir = os.getcwd() if os.path.exists(cur_dir + "/interfaces"): shutil.rmtree(temp_dir_networking, ignore_errors=True) os.makedirs(temp_dir_networking) utils.app_command_quiet("cp interfaces " + temp_dir_networking) utils.app_command_quiet("cp resolv.conf " + temp_dir_networking) utils.app_command_quiet("cp hostname.backup " + temp_dir_networking) if device_type == 2 or device_type == 6 or device_type == 13: utils.app_command_quiet("cp pbr-* " + temp_dir_networking) # restore hostname if os.path.exists(cur_dir + "/hostname.backup"): utils.app_command_quiet("cp hostname.backup " + temp_dir_networking) _restore_hostname(temp_dir_networking + "/hostname.backup") #restore pcap config file if device_type == 2: has_file = False for bro_cfg in bro_backup_filelist: if os.path.isfile(os.path.basename(bro_cfg)): has_file = True shutil.copy(os.path.basename(bro_cfg), bro_cfg) common_bro_rep_str = 'redef snaplen = 32768;\\nconst store_disk_length = 4096 \\&redef;\\nconst ftp_capture_max_file_size =100000000 \\&redef;\\nconst smb_capture_max_file_size =100000000 \\&redef;\\nconst http_capture_max_file_size =100000000 \\&redef;\\nconst smtp_capture_max_file_size =100000000 \\&redef;\\nconst imap_capture_max_file_size =100000000 \\&redef;\\nconst pop3_capture_max_file_size =100000000 \\&redef;' os.system( "sed -i ':a;N;$!ba;s/\\(.*\\)redef snaplen = 32768;\\(.*\\)/\\1%s\\2/' /usr/local/bro/share/bro/policy/protocols/spe-common/common.bro" % (common_bro_rep_str)) if has_file == True: device_mode = utils.get_device_work_mode() current_mode = utils.get_monitor_mode() restore_mode = utils.get_monitor_mode() if device_mode == 1 and not current_mode == restore_mode: utils.logger("Restore monitor mode to %s" % restore_mode) utils.app_command( "/opt/skyguard/www/app/device_work_mode.py switch force") if device_type == 2 or device_type == 4 or device_type == 6 or device_type == 8: #Restore iptables, ebtables first - in case device work doesn't change which will not refresh these tables for bk_file in app_backup_filelist: if (device_type == 4 or device_type == 8) and "ebtables" in bk_file: continue if os.path.isfile(os.path.basename(bk_file)): shutil.copy(os.path.basename(bk_file), bk_file) device_mode = utils.get_device_work_mode() # Flush ebtables if device_type == 2 or device_type == 6 and device_mode != 4: # device mode is not proxy mode utils.app_command_quiet("/sbin/ebtables -t broute -F") utils.app_command_quiet( "/bin/sh /opt/skyguard/www/app/ebtables-rules-%d" % int(device_mode)) utils.app_command_quiet( "/sbin/iptables-restore < /opt/skyguard/www/app/iptables-rules-%d" % int(device_mode)) app_restore = app_filename.replace(".tar.gz", ".conf") with open(app_restore) as config_data: config = json.load(config_data) monitor_mode = utils.get_monitor_mode() agent_client = AGENT_CLIENT(AGENT_ADDR, AGENT_PORT) # restore device work mode if config.has_key("device_work_mode"): device_work_mode = config["device_work_mode"] device_type = utils.get_device_type() if device_type == 2 or device_type == 6: res = agent_client.set_information( {"device_work_mode": device_work_mode}) res_json = json.loads(res) if res_json["responseCode"] != 200: utils.app_logger("Restore device_work_mode encounter error", "error") return False if device_work_mode == 1: utils.app_command( "sudo /opt/skyguard/www/app/device_work_mode.py switch %s" % monitor_mode) # restore protocol settings device_work_mode = utils.get_device_work_mode() if config.has_key("protocol"): utils.app_conf_write({"protocol": config["protocol"]}) if device_work_mode == 1 and monitor_mode == "pcap" and config[ "protocol"].has_key("netObject"): try: res = agent_client.set_protocol_settings( "netObject", config["protocol"]["netObject"]) except httplib.HTTPException: agent_client = AGENT_CLIENT(AGENT_ADDR, AGENT_PORT) res = agent_client.set_protocol_settings( "netObject", config["protocol"]["netObject"]) #restore pcap file in swg if device_type == 6: for bro_cfg in bro_backup_filelist_swg: if os.path.isfile(os.path.basename(bro_cfg)): shutil.copy(os.path.basename(bro_cfg), bro_cfg) # if bro run ,restart ret = os.system( "ps -ef |grep /usr/local/bro/bin/bro |grep -v grep > /dev/null 2>&1" ) if ret == 0: os.system("/usr/local/bro/bin/broctl deploy > /dev/null 2>&1") import time time.sleep(1) # restore global bypass if config.has_key("globalbypass"): res = utils.app_conf_write({"globalbypass": config["globalbypass"]}) #restore exception list if config.has_key("exception"): res = utils.app_conf_write({"exception": config["exception"]}) # restore backup settings if config.has_key("backup_settings"): backup_settings = config["backup_settings"] try: res = agent_client.set_backup_settings(backup_settings) except httplib.HTTPException: agent_client = AGENT_CLIENT(AGENT_ADDR, AGENT_PORT) res = agent_client.set_backup_settings(backup_settings) res_json = json.loads(res) if res_json["responseCode"] != 200: utils.app_logger("Restore backup_settings encounter error", "error") return False # restore snmp settings if config.has_key("snmp_settings"): snmp_settings = config["snmp_settings"] try: res = agent_client.set_information( {"snmp_settings": snmp_settings}) except httplib.HTTPException: agent_client = AGENT_CLIENT(AGENT_ADDR, AGENT_PORT) res = agent_client.set_information( {"snmp_settings": snmp_settings}) res_json = json.loads(res) if res_json["responseCode"] != 200: utils.app_logger("Restore snmp_settings encounter error", "error") return False # restore device time info if config.has_key("device_time_info"): device_time_info = config["device_time_info"] if device_time_info.has_key("ntp") and device_time_info["ntp"]: new_time_info = json.loads( agent_client.get_device_time())["device_time_info"] new_time_info["ntp"] = device_time_info["ntp"] new_time_info["ntpserver"] = device_time_info["ntpserver"] config = {} config["device_time_info"] = new_time_info try: res = agent_client.set_config(config) except httplib.HTTPException: agent_client = AGENT_CLIENT(AGENT_ADDR, AGENT_PORT) res = agent_client.set_config(config) res_json = json.loads(res) if res_json["responseCode"] != 200: utils.app_logger("Restore device_time_info encounter error", "error") return False # restore proxy settings if config.has_key("proxy_settings"): proxy_settings = config["proxy_settings"] try: res = agent_client.set_proxy_settings(proxy_settings) except httplib.HTTPException: agent_client = AGENT_CLIENT(AGENT_ADDR, AGENT_PORT) res = agent_client.set_proxy_settings(proxy_settings) res_json = json.loads(res) if res_json["responseCode"] != 200: utils.app_logger("Restore proxy_settings encounter error", "error") return False # restore block pages settings if config.has_key("block_page_settings"): utils.app_logger("begim to restore block_page_settings") block_page_settings = config["block_page_settings"] bps = BlockPageSettings() # first restore the backup block pages bps.delete_backup_zip() shutil.copy(os.path.basename(blockpage_backup_customized_zip), blockpage_backup_customized_zip) shutil.copy(os.path.basename(blockpage_backup_uploaded_zip), blockpage_backup_uploaded_zip) bps.restore_blockpage_dir() # then update the setting ret = bps.set_settings(block_page_settings) utils.app_command_quiet( "chown -R www-data:www-data /opt/skyguard/download/") if ret["responseCode"] == 0 or ret["responseCode"] == 1: utils.app_logger("Succeed to restore block_page_settings") else: utils.app_logger("Restore block_page_settings encounter error", "error") return False # restore device base info if config.has_key("device_base_info"): agent_client = AGENT_CLIENT(AGENT_ADDR, AGENT_PORT) agent_client.set_information( {"device_base_info": config["device_base_info"]}) #(ret, output) = utils.app_command("sudo /opt/skyguard/www/app/device_base_info.py set %s" % "'"+json.dumps(config["device_base_info"])+"'") #if ret != 0: # utils.app_logger(str(output)) # restore forensics storage if config.has_key("forensics_storage"): forensics_storage.set_forensics_capacity( config["forensics_storage"]["capacity"]) #restore forensic file if device_type != 1: if os.path.exists("forensics"): filelist = os.listdir("forensics") if filelist != []: if not os.path.exists(forensics_dir): os.makedirs(forensics_dir) FileUtil.copyFilesToDir("forensics", forensics_dir) #for f in filelist: # shutil.copy("forensics/"+f,forensics_dir) #restore collect log dir if os.path.exists("collect_log"): filelist = os.listdir("collect_log") if filelist != []: if not os.path.exists(collect_log_dir): os.makedirs(collect_log_dir) FileUtil.copyFilesToDir("collect_log/", collect_log_dir) # Restore hybrid.conf if exist if os.path.isfile("hybrid.conf"): shutil.copy("hybrid.conf", "/opt/skyguard/www/app/hybrid.conf") # Resotre hybrid settings if (device_type == 4 or device_type == 8) and os.path.isfile("gre_info.conf"): shutil.copy("gre_info.conf", "/opt/skyguard/www/app/gre_info.conf") ''' with open("hybrid_settings.conf", "r") as hybrid_conf: hybrid_settings = json.load(hybrid_conf) agent_client = AGENT_CLIENT(AGENT_ADDR, AGENT_PORT) agent_client.set_hybrid_config(hybrid_settings) ''' #restore synctime task setting if config.has_key("synctime_schedule_task"): redis_client = RedisClient() if redis_client.exist_key("synctime_schedule_task"): redis_client.delete_key("synctime_schedule_task") redis_client.hmset("synctime_schedule_task", config["synctime_schedule_task"]) #restore backup task setting if config.has_key("backup_schedule_task"): redis_client = RedisClient() if redis_client.exist_key("backup_schedule_task"): redis_client.delete_key("backup_schedule_task") redis_client.hmset("backup_schedule_task", config["backup_schedule_task"]) set_backup_schedule(config["backup_schedule_task"]) #restore ts setting #if config.has_key("ts_account_info"): #redis_client = RedisClient() #if redis_client.exist_key("ts"): # redis_client.delete_key("ts") #redis_client.set("ts",config["ts_account_info"]) # restore network setting bk_interface_file = temp_dir_networking + "/interfaces" if os.path.exists(bk_interface_file): import time time.sleep(5) utils.app_logger("Restore network setting...") #utils.app_command_quiet("/etc/init.d/networking stop") for nic in [ "eth0", "eth1", "eth2", "eth3", "bond0", "bond1", "bond2", "bond3", "br0" ]: _remove_old_pbr(nic) utils.app_logger("Restore /etc/network/interfaces...") shutil.copy(bk_interface_file, "/etc/network/interfaces") bk_resolv_file = temp_dir_networking + "/resolv.conf" utils.app_logger("Restore /etc/resolv.conf...") shutil.copy(bk_resolv_file, "/etc/resolv.conf") if device_type == 2 or device_type == 6 or device_type == 13: utils.app_logger("Restore /opt/skyguard/www/app/pbr-*...") for f in listdir(temp_dir_networking): if f.find("pbr-") != -1: shutil.copy(join(temp_dir_networking, f), "/opt/skyguard/www/app/") shutil.rmtree(temp_dir_networking, ignore_errors=True) #utils.app_command_quiet("/etc/init.d/networking start") utils.app_logger("Finish to restore network setting.") # notify mta to update mta ip if device_type == 2 or device_type == 6: if utils.get_bonding_status("bond1") == True: mta_ip = utils.get_ip_address("bond1") else: mta_ip = utils.get_ip_address("eth1") mtaclient = MTA_CLIENT(MTA_ADDR, MTA_PORT) mtaclient.set_mta_nics("eth1", mta_ip) os.chdir(current_path) shutil.rmtree(tmp_folder) print "Finished restore" return True
def fix_config(self): # Add default frec if "def_frec" not in self.conf["node"]: self.conf["node"]["def_frec"] = 0.05 # Add ethernet network if "ethernet" not in self.conf["node"]: self.conf["node"]["ethernet"] = utils.get_interface() # Add IP address if "ip" not in self.conf["node"]: self.conf["node"]["ip"] = utils.get_ip_address( self.conf["node"]["ethernet"]) # Add default name if "name" not in self.conf["node"]: self.conf["node"]["name"] = "default_name" # Add port_ns if "port_ns" not in self.conf["node"]: self.conf["node"]["port_ns"] = 9090 # Add port_node if "port_node" not in self.conf["node"]: self.conf["node"]["port_node"] = 4040 # Add start_port if "start_port" not in self.conf["node"]: self.conf["node"]["start_port"] = 5050 # Add def_worker if "def_worker" not in self.conf["node"]: self.conf["node"]["def_worker"] = True # Add password if "password" not in self.conf["node"]: self.conf["node"]["password"] = self.conf["node"]["name"] # Add bigbrother password if "bigbrother-password" not in self.conf["node"]: self.conf["node"]["bigbrother-password"] = "******" # Services and components config for k, v in self.components.items() + self.services.items(): if "worker_run" not in v: v["worker_run"] = True if "mode" not in v: v["mode"] = "public" if "frec" not in v: v["frec"] = self.conf["node"]["def_frec"] v["docstring"] = {} v["exposed"] = {} for k, v in self.services.items(): v["mode"] = "local" # Services configuration newservices = {} for n in self.services: if _clases.get(self.services[n]["cls"], None) is not None: if len(_clases[self.services[n]["cls"]]) > 1: print("Warning: there are many modules {} for class {}". format(_clases[self.services[n]["cls"]], self.services[n]["cls"])) self.services[n]["module"] = _clases[self.services[n] ["cls"]][0] if "." not in n: newservices[self.node["name"] + "." + n] = self.services[n] else: newservices[n] = self.services[n] else: print( colored( "ERROR: Class {} not found or error in Modules".format( self.services[n]["cls"]), "red")) for k_error, error in _modules_errors.iteritems(): print("Module {}: {}".format(k_error, error)) exit() if ("-->") in self.services[n]: sp = [ self.node["name"] + "." + x for x in self.services[n]["-->"] if x.find(".") < 0 ] cp = [x for x in self.services[n]["-->"] if x.find(".") >= 0] self.services[n]["-->"] = sp + cp # esto se puede simplificar self.services = newservices for n in self.services: self.services[n]["_locals"] = [] self.services[n]["_resolved_remote_deps"] = [] if "-->" in self.services[n]: self.services[n]["_locals"], self.services[n][ "_resolved_remote_deps"] = self.local_remote( self.services, n) newrobot = {} # Components configuration for n in self.components: if _clases.get(self.components[n]["cls"], None) is not None: if len(_clases[self.components[n]["cls"]]) > 1: print("Warning: there are many modules {} for class {}". format(_clases[self.components[n]["cls"]], self.components[n]["cls"])) self.components[n]["module"] = _clases[self.components[n] ["cls"]][0] else: print( colored( "ERROR: Class {} not found or error in Modules".format( self.components[n]["cls"]), "red")) for k_error, error in _modules_errors.iteritems(): print("Module {}: {}".format(k_error, error)) exit() self.components[n]["_services"] = list(self.services) if ("-->") in self.components[n]: sp = [ self.node["name"] + "." + x for x in self.components[n]["-->"] if x.find(".") < 0 ] cp = [x for x in self.components[n]["-->"] if x.find(".") >= 0] self.components[n][ "-->"] = sp + cp # esto se puede simplificar if n.find(".") == -1: newrobot[self.node["name"] + "." + n] = self.components[n] else: newrobot[n] = self.components[n] self.components = newrobot for n in self.components: self.components[n]["_locals"] = [] self.components[n]["_unr_remote_deps"] = [] self.components[n]["_resolved_remote_deps"] = [] if "-->" in self.components[n]: self.components[n]["_locals"], self.components[n][ "_resolved_remote_deps"] = self.local_remote( self.components, n)
file.save(img_uri) new_image = Images(img_uri) db.session.add(new_image) try: db.session.commit() except Exception as e: abort(500, e) # PREDICTION #### return jsonify() api.add_resource(UserList, '/api/users') api.add_resource(UserLogin, '/api/auth/login') api.add_resource(UserRegister, '/api/auth/register') api.add_resource(UserRefresh, '/api/auth/refresh') api.add_resource(FamilyRegister, '/api/family/register') api.add_resource(FindMyFamily, '/api/family/find') if __name__ == '__main__': ip_address = get_ip_address() # app.run(host=ip_address, port=BaseConfig.APP_PORT) app.run(host=BaseConfig.APP_HOST, port=BaseConfig.APP_PORT, debug=True, threaded=True)
if __name__ == '__main__': parser = argparse.ArgumentParser( description="Launch the motion dection server") parser.add_argument("--networkDevice", default="wlan0", help="name of Wi-Fi card") parser.add_argument("--port", type=int, default=80, help="port of the server") parser.add_argument("--videoDevice", type=int, default=0, help="index of video device to use") args = parser.parse_args() print('PUBLIC SERVER ACCESSIBLE FROM :', utils.get_ip_address(args.networkDevice) + ':' + str(args.port)) # launch the video feed videoFeed = VideoFeed.create(args.videoDevice) videoFeed.start() # create and run the server server = create_motion_detector_server(__name__, args.port, videoFeed) server.run() # close the video feed when the server is stopped videoFeed.close()
def ip(interface): return utils.get_ip_address(str(interface))
# -*- coding:utf-8 -*- import traceback from functools import wraps from scrapy.exceptions import IgnoreRequest from utils import get_ip_address IP = get_ip_address() def stats_wrapper(func): @wraps(func) def wrapper_method(*args, **kwds): try: return func(*args, **kwds) except Exception: traceback.print_exc() return wrapper_method def parse_method_wrapper(func): @wraps(func) def wrapper_method(*args, **kwds): try: return func(*args, **kwds) except Exception: self = args[0]
sys.path.insert(0,'lib') # prefer bundled libraries to local installs import bottle, utils # read configuration file and setup various globals config = utils.get_config(os.path.join(utils.path_for('data'),'config.json')) # Set up logging logging.config.dictConfig(dict(config.logging)) log = logging.getLogger() # validate framebuffer settings config = utils.validate_resolution(config) # setup static file root staticroot = utils.path_for('static') # check if we have a valid IP address ip_address = utils.get_ip_address(config.interface) # Shared data used by other modules version = '0.13.02.27.6' # Flag for controlled thread termination running = True # Screen state sent from server screen = {} # Local URI Prefix local_uri = 'http://%s:%s' % (config.http.bind_address, config.http.port) # Defaults sent to templates template_vars = { 'version' : version, 'ip_address': ip_address, 'width' : config.screen.width, 'height' : config.screen.height,