Class: Netconf::SSH

Inherits:
Transport show all
Defined in:
lib/net/netconf/ssh.rb

Constant Summary collapse

NETCONF_PORT =
830
NETCONF_SUBSYSTEM =
'netconf'.freeze

Instance Attribute Summary

Attributes inherited from Transport

#capabilities, #rpc, #session_id, #state, #timeout, #waitio

Instance Method Summary collapse

Methods inherited from Transport

#close, #closed?, #has_capability?, #open, #open?, #rpc_exec, #send_and_receive, #trans_receive_hello, #trans_send_hello

Constructor Details

#initialize(args_h, &block) ⇒ SSH

Returns a new instance of SSH


8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/net/netconf/ssh.rb', line 8

def initialize(args_h, &block)
  @args = args_h.clone
  @args[:os_type] = args_h[:os_type] || Netconf::DEFAULT_OS_TYPE

  # extend this instance with the capabilities of the specific os_type
  begin
    extend Netconf.const_get(@args[:os_type])::TransSSH
  rescue NameError
    # no extensions available ...
  end

  @trans = {}
  super(&block)
end

Instance Method Details

#scpObject

accessor to create an Net::SCP object so the caller can perform secure-copy operations (see Net::SCP) for details


86
87
88
# File 'lib/net/netconf/ssh.rb', line 86

def scp
  @scp ||= Net::SCP.start(@args[:target], @args[:username], password: @args[:password])
end

#trans_closeObject


43
44
45
46
# File 'lib/net/netconf/ssh.rb', line 43

def trans_close
  @trans[:chan].close if @trans[:chan]
  @trans[:conn].close if @trans[:conn]
end

#trans_openObject


23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/net/netconf/ssh.rb', line 23

def trans_open
  # open a connection to the NETCONF subsystem
  start_args = {}
  start_args[:password] ||= @args[:password]
  start_args[:passphrase] = @args[:passphrase] || nil
  start_args[:port] = @args[:port] || NETCONF_PORT
  start_args.merge!(@args[:ssh_args]) if @args[:ssh_args]

  begin
    @trans[:conn] = Net::SSH.start(@args[:target], @args[:username], start_args)
    @trans[:chan] = @trans[:conn].open_channel { |ch| ch.subsystem(NETCONF_SUBSYSTEM) }
  rescue Errno::ECONNREFUSED => e
    if respond_to? 'trans_on_connect_refused'
      return trans_on_connect_refused(start_args)
    end
    return nil
  end
  @trans[:chan]
end

#trans_receiveObject


48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/net/netconf/ssh.rb', line 48

def trans_receive
  @trans[:rx_buf] = ''
  @trans[:more] = true

  # collect the response data as it comes back ...
  # the "on" functions must be set before calling
  # the #loop method

  @trans[:chan].on_data do |_ch, data|
    if data.include?(RPC::MSG_END)
      data.slice!(RPC::MSG_END)
      @trans[:rx_buf] << data unless data.empty?
      @trans[:more] = false
    else
      @trans[:rx_buf] << data
    end
  end

  # ... if there are errors ...
  @trans[:chan].on_extended_data do |_ch, _type, data|
    @trans[:rx_err] = data
    @trans[:more] = false
  end

  # the #loop method is what actually performs
  # ssh event processing ...

  @trans[:conn].loop { @trans[:more] }

  @trans[:rx_buf]
end

#trans_send(cmd_str) ⇒ Object


80
81
82
# File 'lib/net/netconf/ssh.rb', line 80

def trans_send(cmd_str)
  @trans[:chan].send_data(cmd_str)
end