diff --git a/CHANGELOG.md b/CHANGELOG.md index 1147701eb..1f78d99ee 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/). ## [Unreleased] ### Added +- device2yaml: add `-n`/`--newline` option to set the command line terminator (e.g. `-n "\r\n"`) for devices that submit a command only on a carriage return; the terminator is recorded as a `command_newline` key in the generated YAML (@Vantomas) ### Changed diff --git a/docs/DeviceSimulation.md b/docs/DeviceSimulation.md index 530b406b9..c531b256b 100644 --- a/docs/DeviceSimulation.md +++ b/docs/DeviceSimulation.md @@ -60,6 +60,7 @@ Usages: -i, --input file Specify an input file for commands to be run -o, --output file Specify an output YAML-file -t, --timeout value Specify the idle timeout beween commands (default: 5 seconds) + -n, --newline value Line terminator appended to each command (default: \n) -e, --exec-mode Run ssh in exec mode (without tty) -u, --unordered The YAML simulation should not enforce an order of the commands -h, --help Print this help @@ -91,6 +92,11 @@ you will have to use the option `-e` to run `device2yaml.rb` in SSH exec mode. the model uses, use the option `-u`. Note that the `unordered` mode may not produce a useful YAML file when combined with user input (see [Interactive Mode](#interactive-mode) below). +- Commands are terminated with `\n` by default. Some CLIs (e.g. TP-Link) execute + a command only on a carriage return; for those, set the line terminator with + `-n`/`--newline` (it understands the `\n`, `\r` and `\t` escapes), e.g. + `-n '\r\n'`. The chosen terminator is recorded in the YAML file as a + `command_newline` key, so it is clear how the simulation was captured. Note that `device2yaml.rb` takes some time to run because of the idle timeout of (default) 5 seconds between each command. You can press the "Escape" key if you diff --git a/extra/device2yaml.rb b/extra/device2yaml.rb index 9f4ea1b1f..35b9b0712 100755 --- a/extra/device2yaml.rb +++ b/extra/device2yaml.rb @@ -147,6 +147,12 @@ def cleanup 'Specify the idle timeout beween commands (default: 5 seconds)') do |timeout| options[:timeout] = timeout end + opts.on('-n', '--newline value', + 'Line terminator appended to each command (default: \n). ' \ + 'Accepts the escapes \n, \r and \t, e.g. -n "\r\n" for ' \ + 'devices that submit a command only on a carriage return.') do |newline| + options[:newline] = newline.gsub('\n', "\n").gsub('\r', "\r").gsub('\t', "\t") + end opts.on('-e', '--exec-mode', 'Run ssh in exec mode (without tty)') { @exec_mode = true } opts.on('-u', '--unordered', 'The YAML simulation should not enforce an order of the commands') do @sequence_prepend_command = '' @@ -194,8 +200,10 @@ def cleanup end puts "Running #{ssh_commands} on #{ssh_user}@#{ssh_host}" -# Add \n to each command -ssh_commands.map! { |s| s + "\n" } +# Append the line terminator to each command (default "\n", overridable with -n +# for devices that submit a command only on a carriage return) +command_newline = options[:newline] || "\n" +ssh_commands.map! { |s| s + command_newline } # Defaut idle timeout: 5 seconds, as tests showed that 2 seconds is too short @idle_timeout = options[:timeout] || 5 @@ -235,6 +243,10 @@ def cleanup # YAML begin of file @output&.puts '---' +# Record the line terminator appended to each command, so it is clear how the +# simulation was captured (some CLIs submit a command only on "\r"). Consumers +# that don't need it (e.g. the model unit tests) simply ignore this key. +@output&.puts "command_newline: #{command_newline.dump}" if @exec_mode # init prompt does not exist and is empty in exec mode