Models define how we extract information from a specific type of device, they should be generic to a (line of) vendor model(s).
All models should consist of re-usable code.
The model configuration are Python modules, we try to follow the PEP 8 guidelines where possible.
Captures text in the device output to a local variable. One can either use named Python Regular Expression groups or provide the variable names as arguments:
>>> capture(r'version: (?P<version>\S+)')
Is equivalent to:
>>> capture(r'version: (\S+)', 'version')
Executes the command on the device returning the output:
>>> output = command('show version')
Connect to the device:
>>> with connect(device) as remote:
... log('Connected to %s' % (remote,))
...
Repository logger for the device, takes a device as parameter:
>>> with dumper(device) as output:
... record(output, 'Starting dump...')
...
Fires a callback as soon as the expected string is seen:
>>> expect(r'login:', device.config.username)
>>> def callback(*args, **kwargs):
... return 'testing'
...
>>> expect(r'password:', callback)
Replace text in the device output with the given replace string, this defaults to <removed>> if not provided.
Generate a header, which can be used as header in the recorded output:
>>> record(output, header('Dumping our border router'))
Record data to the device output, where output is a dumper() instance.
Compiles a pattern into a regular expression object:
>>> ignore(pattern(r'rx packets: .*'))
If no arguments are provided, we wait until the device returns a prompt:
>>> prompt()
Otherwise, we can specify what pattern is used to recognise the device’s prompt:
>>> prompt(pattern(r'^[>#$]$'))
Keep in mind that the pattern is matched against stripped lines (no leading or trailing spaces/tabs are provided).