def spawn_server(port_low, port_high, interface, protocol_type, data): #p = psutil.Process(os.getpid()) try: os.nice(-1) except Exception as e: print "Unable to lower niceness(priority), RUN AS SUDO, running with normal priority" for i in range(port_low, port_high): try: print "Spawning " show_commands, prompt_change_commands, usr, passwd, cmd_delay, default_prompt = parse_commands( data) users = {usr: passwd} local_commands = [] for cmd in show_commands: command = getShowCommand(cmd, data, show_commands[cmd], cmd_delay) local_commands.append(command) for cmd in prompt_change_commands: if ("password" in prompt_change_commands[cmd]): command = getPasswordPromptCommand( cmd, prompt_change_commands[cmd], cmd_delay) else: command = getPromptChangingCommand( cmd, prompt_change_commands[cmd], cmd_delay) local_commands.append(command) factory = None if (protocol_type == "ssh"): factory = MockSSH.getSSHFactory(local_commands, default_prompt, ".", **users) elif (protocol_type == "telnet"): factory = MockSSHExtensions.getTelnetFactory( local_commands, default_prompt, **users) reactor.listenTCP(i, factory, interface=interface) except Exception as e: print >> sys.stderr, traceback.format_exc() print "Unable to open port at %s, due to: %s" % (i, e) reactor.run()
def run(self, interface="127.0.0.1", port=None): """Start the SSH server listening. You must add a username (addUser) before starting the server. (Errors will be thrown! It's a library limitation.) :param str interface: The interface to listen to. Defaults to ``127.0.0.1``. :param port: The port to listen to. If not provided (or None), an open port will be chosen. :type port: int or None :returns: int -- The port the SSH server is listening to. """ if port == None: port = get_open_port() self.running = True # Re-implementation of MockSSH.startThreadedServer, # that doesn't start Reactor inside a Thread if it's already running. sshFactory = MockSSH.getSSHFactory(self.commands, prompt=self.prompt(), keypath=".", **self.users) self.listener = reactor.listenTCP(port, sshFactory, interface='') if not reactor.running: Thread(target=reactor.run, args=(False, )).start() self.reactor_running = True return port
def ssh_hook_to_reactor(reactor): sshFactory = MockSSH.getSSHFactory(commands, "hostname>", tempfile.mkdtemp(), **users) reactor.listenTCP(interface='127.0.0.1', port=SshClientTest.port, factory=sshFactory)
def ssh_hook_to_reactor(reactor): sshFactory = MockSSH.getSSHFactory(commands, "hostname>", tempfile.mkdtemp(), **users) reactor.listenTCP(interface="127.0.0.1", port=SshClientTest.port, factory=sshFactory)