Exscript.emulators package

Emulating a device for testing your scripts.

class Exscript.emulators.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.
class Exscript.emulators.IOSEmulator(hostname, echo=True, login_type=3, strict=True, banner=None)[source]

Bases: Exscript.emulators.vdevice.VirtualDevice

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

Bases: future.types.newobject.newobject

A set of commands to be used by the Dummy adapter.

__init__(strict=True)[source]

Constructor.

add(command, response)[source]

Register a command/response pair.

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.
  • response (function|str) – A reponse, or a response handler.
add_from_file(filename, handler_decorator=None)[source]

Wrapper around add() 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.
  • handler_decorator (function) – A function that is used to decorate each of the handlers in the file.
eval(command)[source]

Evaluate the given string against all registered commands and return the defined response.

Parameters:command (str) – The command that is evaluated.
Return type:str or None
Returns:The response, if one was defined.