import MockSSH import random import string from twisted.cred import checkers # Dirty hack to override default twisted behavior class CredsToStdOut(checkers.InMemoryUsernamePasswordDatabaseDontUse): def requestAvatarId(self, credentials): print("Authentication attempt: %s / %s" % (credentials.username, credentials.password)) return super(CredsToStdOut, self).requestAvatarId(credentials) MockSSH.checkers.InMemoryUsernamePasswordDatabaseDontUse = CredsToStdOut def passwd_change_protocol_prompt(instance): instance.protocol.prompt = "hostname #" instance.protocol.password_input = False def passwd_write_password_to_transport(instance): instance.writeln("") command_passwd = MockSSH.PromptingCommand( name = 'passwd', password = ''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(99)), prompt = "Password: "******"hostname>", interface = '0.0.0.0', port = 2222, **users)
# # command: en # def en_change_protocol_prompt(instance): instance.protocol.prompt = "hostname #" instance.protocol.password_input = False def en_write_password_to_transport(instance): instance.writeln("MockSSH: password is %s" % instance.valid_password) command_en = MockSSH.PromptingCommand( name='en', password='******', prompt="Password: "******"MockSSH: supported usage: conf t") def conf_output_success(instance): instance.writeln("Enter configuration commands, one per line. End " "with CNTL/Z")
# command: wr # def wr_build_config(instance): instance.terminal.session.conn.transport.transport.setTcpNoDelay(True) instance.write("Building configuration...") ## In a perfect world, we would sleep here to pretend ## that the router is building the configuration; however, ## there isn't a good way to make Twisted flush the TCP buffer ## so the client sees a delay between "Building configuration..." ## and "[OK]" #time.sleep(1.2) instance.write(CRLF+"[OK]") command_en = MockSSH.PromptingCommand( 'en', # the enable command ENABLE_PASS, # enable password 'Password: '******'enable', # the enable command ENABLE_PASS, # enable password 'Password: '******'conf', [conf_output_success, conf_change_protocol_prompt], [invalid_input], *["t"])