Module: TrainPlugins::Juniper::CommandExecutor
- Included in:
- Connection
- Defined in:
- lib/train-juniper/connection/command_executor.rb
Overview
Handles command execution, sanitization, and output formatting
Constant Summary collapse
- DANGEROUS_COMMAND_PATTERNS =
Command sanitization patterns Note: Pipe (|) is allowed as it’s commonly used in JunOS commands
[ /[;&<>$`]/, # Shell metacharacters (excluding pipe) /\n|\r/, # Newlines that could inject commands /\\(?![nrt])/ # Escape sequences (except valid ones like \n, \r, \t) ].freeze
Instance Method Summary collapse
-
#run_command_via_connection(cmd) ⇒ CommandResult
Execute commands on Juniper device via SSH.
Instance Method Details
#run_command_via_connection(cmd) ⇒ CommandResult
Execute commands on Juniper device via SSH
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/train-juniper/connection/command_executor.rb', line 22 def run_command_via_connection(cmd) # Sanitize command to prevent injection safe_cmd = sanitize_command(cmd) return mock_command_result(safe_cmd) if @options[:mock] begin # :nocov: Real SSH execution cannot be tested without actual devices # Ensure we're connected connect unless connected? log_command(safe_cmd) # Execute command via SSH session output = @ssh_session.exec!(safe_cmd) @logger.debug("Command output: #{output}") # Format JunOS result format_junos_result(output, safe_cmd) rescue StandardError => e log_error(e, "Command execution failed for: #{safe_cmd}") # Handle connection errors gracefully error_result("#{e.} (command: #{safe_cmd})") # :nocov: end end |