def run(self): while 1: gws = netifaces.gateways() cgw = gws['default'][netifaces.AF_INET][0] if not homenet.gateway: homenet.gateway = cgw else: if homenet.gateway != cgw: utils.reconfigure_network(homenet.gateway, cgw) homenet.gateway = cgw try: with lock: utils.save_pkl_object(homenet, "homenet.pkl") except Exception as e: log.debug('FG-ERROR ' + str(e.__doc__) + " - " + str(e)) utils.reboot_appliance() else: pass time.sleep(10)
def main(): if not os.geteuid() == 0: log.debug('FG-FATAL: Script must be run as root') exit('Script must be run as root') global homenet global threads # Check if process is not runing already get_lock('falcongate_main') log.debug('FG-INFO: main.py started') # Starting threads for key in threads.keys(): threads[key].daemon = True threads[key].start() log.debug('FG-INFO: Started thread ' + key) time.sleep(15) # Store process ID and other parameters for the main thread homenet.pid = os.getpid() homenet.executable = sys.executable homenet.args = sys.argv[:] try: # Retrieving network configuration from local eth0 interface and storing in global variables addrs = netifaces.ifaddresses(homenet.interface) ipinfo = addrs[socket.AF_INET][0] # Get MAC for eth0 homenet.mac = addrs[netifaces.AF_LINK][0]['addr'] # IP address for eth0 homenet.ip = ipinfo['addr'] # Netmask for eth0 homenet.netmask = ipinfo['netmask'] cidr = netaddr.IPNetwork('%s/%s' % (homenet.ip, homenet.netmask)) # Network CIDR for eth0 network = cidr.network homenet.net_cidr = netaddr.IPNetwork('%s/%s' % (network, homenet.netmask)) # Get default gateway for eth0 gws = netifaces.gateways() cgw = gws['default'][netifaces.AF_INET][0] if not homenet.gateway: homenet.gateway = cgw else: if homenet.gateway != cgw: utils.reconfigure_network(homenet.gateway, cgw) homenet.gateway = cgw try: with lock: utils.save_pkl_object(homenet, "homenet.pkl") except Exception as e: log.debug(e.__doc__ + " - " + e.message) utils.reboot_appliance() else: pass except Exception as e: pass log.debug('FG-DEBUG: Starting main loop') while True: try: flag = False while not flag: try: with lock: utils.save_pkl_object(homenet, "homenet.pkl") flag = True except Exception as e: log.debug('FG-ERROR: ' + e.__doc__ + " - " + e.message) time.sleep(2) time.sleep(30) except KeyboardInterrupt: log.debug('FG-INFO: Process terminated by keyboard interrupt') print 'Have a nice day!' sys.exit(0)