Exscript.emulators.vdevice module

Defines the behavior of a device, as needed by Exscript.servers.

class Exscript.emulators.vdevice.VirtualDevice(hostname, echo=True, login_type=3, strict=True, banner=None)[source]

Bases: future.types.newobject.newobject

An object that emulates a remote device.

LOGIN_TYPE_BOTH = 3
LOGIN_TYPE_NONE = 4
LOGIN_TYPE_PASSWORDONLY = 1
LOGIN_TYPE_USERONLY = 2
PROMPT_STAGE_CUSTOM = 3
PROMPT_STAGE_PASSWORD = 2
PROMPT_STAGE_USERNAME = 1
__init__(hostname, echo=True, login_type=3, strict=True, banner=None)[source]
Parameters:
  • hostname (str) – The hostname, used for the prompt.
  • echo – bool
  • echo – whether to echo the command in a response.
  • login_type – int
  • login_type – integer constant, one of LOGIN_TYPE_PASSWORDONLY, LOGIN_TYPE_USERONLY, LOGIN_TYPE_BOTH, LOGIN_TYPE_NONE.
  • strict – bool
  • strict – Whether to raise when a given command has no handler.
  • banner – str
  • banner – A string to show as soon as the connection is opened.
add_command(command, handler, prompt=True)[source]

Registers a command.

The command may be either a string (which is then automatically compiled into a regular expression), or a pre-compiled regular expression object.

If the given response handler is a string, it is sent as the response to any command that matches the given regular expression. If the given response handler is a function, it is called with the command passed as an argument.

Parameters:
  • command (str|regex) – A string or a compiled regular expression.
  • handler (function|str) – A string, or a response handler.
  • prompt (bool) – Whether to show a prompt after completing the command.
add_commands_from_file(filename, autoprompt=True)[source]

Wrapper around add_command_handler that reads the handlers from the file with the given name. The file is a Python script containing a list named ‘commands’ of tuples that map command names to handlers.

Parameters:
  • filename (str) – The name of the file containing the tuples.
  • autoprompt (bool) – Whether to append a prompt to each response.
do(command)[source]

“Executes” the given command on the virtual device, and returns the response.

Parameters:command (str) – The command to be executed.
Return type:str
Returns:The response of the virtual device.
get_prompt()[source]

Returns the prompt of the device.

Return type:str
Returns:The current command line prompt.
init()[source]

Init or reset the virtual device.

Return type:str
Returns:The initial response of the virtual device.
set_prompt(prompt)[source]

Change the prompt of the device.

Parameters:prompt (str) – The new command line prompt.