Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 6 additions & 0 deletions docs/DeviceSimulation.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
16 changes: 14 additions & 2 deletions extra/device2yaml.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ''
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down