def start_tftp_server(self): self.main_logger.debug("TFTP Server: Starting thread") self.options.ip_address = self.gui_sip_ip_address.get() self.options.tftp_port = self.gui_tftp_port.get() self.options.tftp_root = self.gui_tftp_root.get() self.main_logger.debug("TFTP Server port: %s", self.options.tftp_port) try: self.tftp_server = tftp.TFTPD( ip=self.options.ip_address, port=self.options.tftp_port, mode_debug=self.options.debug, logger=self.main_logger, netboot_directory=self.options.tftp_root) self.tftp_server_thread = threading.Thread( name='tftp', target=self.tftp_server.listen) self.tftp_server_thread.daemon = True self.tftp_server_thread.start() self.tftp_control_button.configure(text="Stop TFTP Server", command=self.stop_tftp_server) except Exception, e: self.main_logger.error("Cannot start the server: %s", e) raise e
#serve all files from one directory os.chdir(args.NETBOOT_DIR) # make a list of running threads for each service running_services = [] # configure/start TFTP server if args.USE_TFTP: # setup TFTP logger tftp_logger = sys_logger.getChild('TFTP') sys_logger.info('Starting TFTP server...') # setup the thread tftp_server = tftp.TFTPD(mode_debug=do_debug('tftp'), logger=tftp_logger) tftpd = threading.Thread(target=tftp_server.listen) tftpd.daemon = True tftpd.start() running_services.append(tftpd) # configure/start DHCP server if args.USE_DHCP: # setup DHCP logger dhcp_logger = sys_logger.getChild('DHCP') if args.DHCP_MODE_PROXY: sys_logger.info('Starting DHCP server in ProxyDHCP mode...') else: sys_logger.info('Starting DHCP server...')
if options.pnp: main_logger.info("PnP: Starting server thread") pnp_server = pnp.SipTracedMcastUDPServer( ('224.0.1.75', 5060), pnp.UDPHandler, sip_logger, main_logger, options) pnp_server_thread = threading.Thread( name='pnp', target=pnp_server.serve_forever) pnp_server_thread.daemon = True pnp_server_thread.start() running_services.append(pnp_server_thread) if options.tftp: main_logger.info("TFTP: Starting server thread") tftp_server = tftp.TFTPD(ip=options.ip_address, port=options.tftp_port, mode_debug=options.debug, logger=main_logger, netboot_directory=options.tftp_root) tftp_server_thread = threading.Thread( name='tftp', target=tftp_server.listen) tftp_server_thread.daemon = True tftp_server_thread.start() running_services.append(tftp_server_thread) if options.http: main_logger.info("HTTP: Starting server thread") http_server = http.HTTPD(ip=options.ip_address, mode_debug=options.debug, logger=main_logger, port=options.http_port, work_directory=options.http_root)
def main(): global SETTINGS, args try: # configure args = parse_cli_arguments() if args.DUMP_CONFIG or args.DUMP_CONFIG_MERGED: if args.DUMP_CONFIG: settings = SETTINGS else: # some arguments don't make sense to print settings = args.__dict__ del settings['DUMP_CONFIG'] del settings['DUMP_CONFIG_MERGED'] del settings['JSON_CONFIG'] print(json.dumps(settings, sort_keys=True, indent=4)) sys.exit() if args.JSON_CONFIG: # load from configuration file if specified try: config_file = io.open(args.JSON_CONFIG, 'r') except IOError: sys.exit('Failed to open {0}'.format(args.JSON_CONFIG)) try: loaded_config = json.load(config_file) config_file.close() except ValueError: sys.exit('{0} does not contain valid JSON'.format(args.JSON_CONFIG)) for setting in loaded_config: if type(loaded_config[setting]) is unicode: loaded_config[setting] = loaded_config[setting].encode('ascii') SETTINGS.update(loaded_config) # update settings with JSON config args = parse_cli_arguments() # re-parse, CLI options take precedence # warn the user that they are starting PyPXE as non-root user if os.getuid() != 0: print(sys.stderr, '\nWARNING: Not root. Servers will probably fail to bind.\n') # ideally this would be in dhcp itself, but the chroot below *probably* # breaks the ability to open the config file. if args.STATIC_CONFIG: try: static_config = io.open(args.STATIC_CONFIG, 'r') except IOError: sys.exit("Failed to open {0}".format(args.STATIC_CONFIG)) try: loaded_statics = json.load(static_config) static_config.close() except ValueError: sys.exit("{0} does not contain valid json".format(args.STATIC_CONFIG)) else: loaded_statics = dict() # setup main logger sys_logger = logging.getLogger('PyPXE') if args.SYSLOG_SERVER: handler = logging.handlers.SysLogHandler(address = (args.SYSLOG_SERVER, int(args.SYSLOG_PORT))) else: handler = logging.StreamHandler() formatter = logging.Formatter('%(asctime)s [%(levelname)s] %(name)s %(message)s') handler.setFormatter(formatter) sys_logger.addHandler(handler) sys_logger.setLevel(logging.INFO) # pass warning to user regarding starting HTTP server without iPXE if args.USE_HTTP and not args.USE_IPXE and not args.USE_DHCP: sys_logger.warning('HTTP selected but iPXE disabled. PXE ROM must support HTTP requests.') # if the argument was pased to enabled ProxyDHCP then enable the DHCP server if args.DHCP_MODE_PROXY: args.USE_DHCP = True # if the network boot file name was not specified in the argument, # set it based on what services were enabled/disabled if args.NETBOOT_FILE == '': if not args.USE_IPXE: args.NETBOOT_FILE = 'pxelinux.0' elif not args.USE_HTTP: args.NETBOOT_FILE = 'boot.ipxe' else: args.NETBOOT_FILE = 'boot.http.ipxe' if args.NBD_WRITE and not args.NBD_COW: sys_logger.warning('NBD Write enabled but copy-on-write is not. Multiple clients may cause corruption') if args.NBD_COW_IN_MEM or args.NBD_COPY_TO_RAM: sys_logger.warning('NBD cowinmem and copytoram can cause high RAM usage') if args.NBD_COW and not args.NBD_WRITE: # cow implies write args.NBD_WRITE = True # make a list of running threads for each service running_services = [] # configure/start TFTP server if args.USE_TFTP: # setup TFTP logger tftp_logger = helpers.get_child_logger(sys_logger, 'TFTP') sys_logger.info('Starting TFTP server...') # setup the thread tftp_server = tftp.TFTPD( mode_debug = do_debug('tftp'), mode_verbose = do_verbose('tftp'), logger = tftp_logger, netboot_directory = args.NETBOOT_DIR, ip = args.TFTP_SERVER_IP) tftpd = threading.Thread(target = tftp_server.listen) tftpd.daemon = True tftpd.start() running_services.append(tftpd) # configure/start DHCP server if args.USE_DHCP: # setup DHCP logger dhcp_logger = helpers.get_child_logger(sys_logger, 'DHCP') if args.DHCP_MODE_PROXY: sys_logger.info('Starting DHCP server in ProxyDHCP mode...') else: sys_logger.info('Starting DHCP server...') # setup the thread dhcp_server = dhcp.DHCPD( ip = args.DHCP_SERVER_IP, port = args.DHCP_SERVER_PORT, offer_from = args.DHCP_OFFER_BEGIN, offer_to = args.DHCP_OFFER_END, subnet_mask = args.DHCP_SUBNET, router = args.DHCP_ROUTER, dns_server = args.DHCP_DNS, broadcast = args.DHCP_BROADCAST, file_server = args.DHCP_FILESERVER, file_name = args.NETBOOT_FILE, use_ipxe = args.USE_IPXE, use_http = args.USE_HTTP, mode_proxy = args.DHCP_MODE_PROXY, mode_debug = do_debug('dhcp'), mode_verbose = do_verbose('dhcp'), whitelist = args.DHCP_WHITELIST, static_config = loaded_statics, logger = dhcp_logger, saveleases = args.LEASES_FILE) dhcpd = threading.Thread(target = dhcp_server.listen) dhcpd.daemon = True dhcpd.start() running_services.append(dhcpd) # configure/start HTTP server if args.USE_HTTP: # setup HTTP logger http_logger = helpers.get_child_logger(sys_logger, 'HTTP') sys_logger.info('Starting HTTP server...') # setup the thread http_server = http.HTTPD( mode_debug = do_debug('http'), mode_verbose = do_debug('http'), logger = http_logger, port = args.HTTP_PORT, netboot_directory = args.NETBOOT_DIR) httpd = threading.Thread(target = http_server.listen) httpd.daemon = True httpd.start() running_services.append(httpd) # configure/start NBD server if args.NBD_BLOCK_DEVICE: # setup NBD logger nbd_logger = helpers.get_child_logger(sys_logger, 'NBD') sys_logger.info('Starting NBD server...') nbd_server = nbd.NBD( block_device = args.NBD_BLOCK_DEVICE, write = args.NBD_WRITE, cow = args.NBD_COW, in_mem = args.NBD_COW_IN_MEM, copy_to_ram = args.NBD_COPY_TO_RAM, ip = args.NBD_SERVER_IP, port = args.NBD_PORT, mode_debug = do_debug('nbd'), mode_verbose = do_verbose('nbd'), logger = nbd_logger, netboot_directory = args.NETBOOT_DIR) nbdd = threading.Thread(target = nbd_server.listen) nbdd.daemon = True nbdd.start() running_services.append(nbdd) sys_logger.info('PyPXE successfully initialized and running!') while all(map(lambda x: x.isAlive(), running_services)): sleep(1) except KeyboardInterrupt: sys.exit('\nShutting down PyPXE...\n')