Module: TrainPlugins::Juniper::SSHSession
- Included in:
- Connection
- Defined in:
- lib/train-juniper/connection/ssh_session.rb
Overview
Handles SSH session management and configuration
Constant Summary collapse
- SSH_OPTION_MAPPING =
SSH option mapping configuration
{ port: :port, password: :password, timeout: :timeout, keepalive: :keepalive, keepalive_interval: :keepalive_interval, keys: ->(opts) { Array(opts[:key_files]) if opts[:key_files] }, keys_only: ->(opts) { opts[:keys_only] if opts[:key_files] } }.freeze
- SSH_DEFAULTS =
Note:
verify_host_key is set to :never for network device compatibility
Default SSH options for Juniper connections Rationale: Network devices often regenerate SSH keys after firmware updates and operate in controlled environments where MITM attacks are mitigated by network segmentation. This matches standard network automation practices.
{ verify_host_key: :never }.freeze
Instance Method Summary collapse
-
#build_ssh_options ⇒ Object
private
Build SSH connection options from @options.
-
#connect ⇒ Object
Establish SSH connection to Juniper device.
-
#connected? ⇒ Boolean
Check if SSH connection is active.
-
#mock? ⇒ Boolean
Check if running in mock mode.
-
#test_and_configure_session ⇒ Object
Test connection and configure JunOS session.
Instance Method Details
#build_ssh_options ⇒ Object (private)
Build SSH connection options from @options
99 100 101 102 103 104 105 106 |
# File 'lib/train-juniper/connection/ssh_session.rb', line 99 def SSH_DEFAULTS.merge( SSH_OPTION_MAPPING.each_with_object({}) do |(ssh_key, option_key), opts| value = option_key.is_a?(Proc) ? option_key.call(@options) : @options[option_key] opts[ssh_key] = value unless value.nil? end ) end |
#connect ⇒ Object
Establish SSH connection to Juniper device
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/train-juniper/connection/ssh_session.rb', line 28 def connect return if connected? # :nocov: Real SSH connections cannot be tested without actual devices begin # Use direct SSH connection (network device pattern) # Defensive loading - only require if not fully loaded require 'net/ssh' unless defined?(Net::SSH) && Net::SSH.respond_to?(:start) @logger.debug('Establishing SSH connection to Juniper device') = # Add bastion host support if configured if @options[:bastion_host] log_bastion_connection(@options[:bastion_host]) configure_bastion_proxy() end log_connection_attempt(@options[:host], @options[:port]) () # Direct SSH connection @ssh_session = Net::SSH.start(@options[:host], @options[:user], ) log_connection_success(@options[:host]) # Configure JunOS session for automation test_and_configure_session rescue StandardError => e handle_connection_error(e) end # :nocov: end |
#connected? ⇒ Boolean
Check if SSH connection is active
64 65 66 67 68 69 70 |
# File 'lib/train-juniper/connection/ssh_session.rb', line 64 def connected? return true if @options[:mock] !@ssh_session.nil? rescue StandardError false end |
#mock? ⇒ Boolean
Check if running in mock mode
74 75 76 |
# File 'lib/train-juniper/connection/ssh_session.rb', line 74 def mock? @options[:mock] == true end |
#test_and_configure_session ⇒ Object
Test connection and configure JunOS session
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/train-juniper/connection/ssh_session.rb', line 79 def test_and_configure_session @logger.debug('Testing SSH connection and configuring JunOS session') # Test connection first @ssh_session.exec!('echo "connection test"') @logger.debug('SSH connection test successful') # Optimize CLI for automation @ssh_session.exec!('set cli screen-length 0') @ssh_session.exec!('set cli screen-width 0') @ssh_session.exec!('set cli complete-on-space off') if @options[:disable_complete_on_space] @logger.debug('JunOS session configured successfully') rescue StandardError => e @logger.warn("Failed to configure JunOS session: #{e.}") end |