Class: Netconf::SSH

Inherits:
Object
  • Object
show all
Defined in:
lib/util/client/netconf/netconf_client.rb

Overview

SSH class that does little more than wrap the net/ssh class. It provides an interface for receiving packets that is parser based.

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ SSH

Returns a new instance of SSH.



25
26
27
28
29
# File 'lib/util/client/netconf/netconf_client.rb', line 25

def initialize(args)
  @args = args.clone
  @channel = nil
  @connection = nil
end

Instance Method Details

#closeObject



50
51
52
53
54
55
56
# File 'lib/util/client/netconf/netconf_client.rb', line 50

def close
  debug 'Netconf::SSH closing SSH session'
  @channel.close unless @channel.nil?
  @connection.close unless @connection.nil?
  @channel = nil
  @connection = nil
end

#open(subsystem) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/util/client/netconf/netconf_client.rb', line 31

def open(subsystem)
  ssh_args = {}
  ssh_args[:password] ||= @args[:password]
  ssh_args[:port] = @args[:port]
  ssh_args[:number_of_password_prompts] = 0
  # Enable if you're having trouble with SSH, change :info to :debug if you
  # are really having trouble
  # ssh_args[:verbose] = :info if Cisco::Logger.level == Logger::DEBUG
  debug "Netconf::SSH::open with args: #{@args} and ssh_args: #{ssh_args}"

  @connection = Net::SSH.start(@args[:target],
                               @args[:username],
                               ssh_args)
  @channel = @connection.open_channel do |ch|
    ch.subsystem(subsystem)
    debug "Netconf::SSH connecting to subsystem #{subsystem}"
  end
end

#receive(parser) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/util/client/netconf/netconf_client.rb', line 63

def receive(parser)
  debug 'Netconf::SSH receiving data'
  continue = true

  @channel.on_data do |_ch, data|
    result = parser.call(data)
    # If parser returns :stop, we take that as a hint to stop
    # expecting data for this message.
    #
    # If parser returns :continue (or anything other than :stop),
    # then we presume the parser is collecting data segments
    # and looking for enough to parse a full "thing"
    continue = false if result == :stop
  end

  @channel.on_extended_data do |_ch, _type, _data|
    continue = false
  end

  # Loop is executed until on_data or on_extended_data
  # sets continue to false
  @connection.loop { continue }
end

#send(data) ⇒ Object



58
59
60
61
# File 'lib/util/client/netconf/netconf_client.rb', line 58

def send(data)
  debug "Netconf::SSH sending data #{data}"
  @channel.send_data(data)
end