diff --git a/docs/Creating-Models.md b/docs/Creating-Models.md index 1dd578f56..188063ce8 100644 --- a/docs/Creating-Models.md +++ b/docs/Creating-Models.md @@ -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. diff --git a/lib/oxidized/model/garderos.rb b/lib/oxidized/model/garderos.rb index 10996f015..60c463d11 100644 --- a/lib/oxidized/model/garderos.rb +++ b/lib/oxidized/model/garderos.rb @@ -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 '# '