Class: Netconf::SSH

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

Constant Summary collapse

NETCONF_PORT =
830
NETCONF_SUBSYSTEM =
'netconf'

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


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

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 = Hash.new   
  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


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

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

#trans_closeObject


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

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

#trans_open(&block) ⇒ Object


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

def trans_open( &block )
  # open a connection to the NETCONF subsystem        
  start_args = Hash.new
  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 self.respond_to? 'trans_on_connect_refused'
      return trans_on_connect_refused( start_args )
    end
    return nil
  end
  @trans[:chan]
end

#trans_receiveObject


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
79
# File 'lib/net/netconf/ssh.rb', line 49

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] }        
  
  return @trans[:rx_buf]      
end

#trans_send(cmd_str) ⇒ Object


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

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