Skip to content

Commit

Permalink
Document an universal ANSI Esc code regexp for models
Browse files Browse the repository at this point in the history
- Updated the garderos model, as we have unit tests for it an it uses
ANSI ESC codes.
- This will help me to improve PR #3332 with an universal approach.
- No documentation to CHANGELOG.md, as documentation / change in
Garderos covered by unit tests
  • Loading branch information
robertcheramy committed Dec 9, 2024
1 parent cad6989 commit e0cf8eb
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
23 changes: 23 additions & 0 deletions docs/Creating-Models.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,29 @@ need to enable privileged mode, either without providing a password (by setting
```
Note: remove `:telnet, ` if your device does not support telnet.

### Common Task: remove ANSI escape codes
> :warning: This common task is still experimental.
> If it does not work for you, please open an issue so that we can adapt the
> code snippet.
Some devices produce ANSI escape codes to enhance the appearance of output.
However, this can make prompt matching difficult and some of these ANSI escape
codes might end up in the resulting configuration.

You can remove most [ANSI escape codes](https://en.wikipedia.org/wiki/ANSI_escape_code#Control_Sequence_Introducer_commands) using the following Ruby
code in your model:
```
# Remove ANSI escape codes
expect /\e\[[0-?]*[ -\/]*[@-~]/ do |data, re|
data.gsub re, ''
end
```
Explanation of the Regular Expression:
- `\e\[` : Control Sequence Introducer (CSI), which starts with "ESC [".
- `[0-?]*`: "parameter" bytes (range 0x30–0x3F, corresponding to ASCII `0–9:;<=>?`)
- `[ -/]*`: "intermediate" bytes (range 0x20–0x2F, corresponding to ASCII ` !"#$%&'()*+,-./`)
- `[@-~]` : the "final" byte (range 0x40–0x7E, corresponding to ASCII ``@A–Z[\]^_`a–z{|}~).[``)

## Extending an existing model with a new command

The example below can be used to extend the `JunOS` model to collect the output of `show interfaces diagnostics optics` and append the output to the configuration file as a comment. This command retrieves DOM information on pluggable optics present in a `JunOS`-powered chassis.
Expand Down
6 changes: 3 additions & 3 deletions lib/oxidized/model/garderos.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ class Garderos < Oxidized::Model
# Routers for harsh environments
# grs = Garderos Router Software

# remove all ANSI escape codes, as GRS uses them :-(
# the prompt does not need to match escape codes, as they have been removed
expect /\e\[\d*m\r?/ do |data, re|
# remove ANSI escape codes
expect /\e\[[0-?]*[ -\/]*[@-~]/ do |data, re|
data.gsub re, ''
end

# the prompt does not need to match escape codes, as they have been removed above
prompt /[\w-]+# /
comment '# '

Expand Down

0 comments on commit e0cf8eb

Please sign in to comment.