Class: Puppet::Util::NetworkDevice::Transport_ios::Sshios

Inherits:
Base_ios
  • Object
show all
Defined in:
lib/puppet/util/network_device/transport_ios/sshios.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSshios

Returns a new instance of Sshios.



9
10
11
12
13
14
# File 'lib/puppet/util/network_device/transport_ios/sshios.rb', line 9

def initialize
  super
  unless Puppet.features.ssh?
    raise 'Connecting with ssh to a network device requires the \'net/ssh\' ruby library'
  end
end

Instance Attribute Details

#bufObject

Returns the value of attribute buf.



7
8
9
# File 'lib/puppet/util/network_device/transport_ios/sshios.rb', line 7

def buf
  @buf
end

#channelObject

Returns the value of attribute channel.



7
8
9
# File 'lib/puppet/util/network_device/transport_ios/sshios.rb', line 7

def channel
  @channel
end

#sshObject

Returns the value of attribute ssh.



7
8
9
# File 'lib/puppet/util/network_device/transport_ios/sshios.rb', line 7

def ssh
  @ssh
end

Instance Method Details

#closeObject



53
54
55
56
57
# File 'lib/puppet/util/network_device/transport_ios/sshios.rb', line 53

def close
  @channel.close if @channel
  @channel = nil
  @ssh.close if @ssh
end

#connect(&block) ⇒ Object



20
21
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
49
50
51
# File 'lib/puppet/util/network_device/transport_ios/sshios.rb', line 20

def connect(&block)
  begin
    Puppet.debug "Trying to connect to #{host} as #{user}"
    @ssh = Net::SSH.start(host, user, :port => port, :password => password, :timeout => timeout)
  rescue TimeoutError
    raise TimeoutError, "SSH timed out while trying to connect to #{host}"
  rescue Net::SSH::AuthenticationFailed
    raise Puppet::Error, "SSH auth failed while trying to connect to #{host} as #{user}"
  rescue Net::SSH::Exception => e
    raise Puppet::Error, "SSH connection failure to #{host}"
  end

  @buf      = ''
  @eof      = false
  @channel  = nil
  @ssh.open_channel do |channel|
    channel.request_pty {|ch, success| raise "Failed to open PTY" unless success}

    channel.send_channel_request('shell') do |ch, success|
      raise 'Failed to open SSH SHELL Channel' unless success

      ch.on_data {|ch, data| @buf << data}
      ch.on_extended_data {|ch, type, data| @buf << data if type == 1}
      ch.on_close {@eof = true}

      @channel = ch
      expect(default_prompt, &block)
      return
    end
  end
  @ssh.loop
end

#eof?Boolean

Returns:

  • (Boolean)


96
97
98
# File 'lib/puppet/util/network_device/transport_ios/sshios.rb', line 96

def eof?
  !!@eof
end

#expect(prompt) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/puppet/util/network_device/transport_ios/sshios.rb', line 59

def expect(prompt)
  line    = ''
  socket  = @ssh.transport.socket

  while not eof?
    break if line =~ prompt and @buf == ''
    break if socket.closed?

    IO::select([socket], [socket], nil, nil)

    process_ssh

    if @buf != ''
      line << @buf.gsub(/\r\n/no, "\n")
      @buf = ''
      yield line if block_given?
    elsif eof?
      break if line =~ prompt
      if line == ''
        line = nil
        yield nil if block_given?
      end
      break
    end
  end
  line.split(/\n/).each do |l|
    Puppet.debug "SSH_IOS received: #{l}" if Puppet[:debug]
    Puppet.fail "Executed invalid Command! For a detailed output add --debug to the next Puppet run!" if line.match(/^% Invalid input detected at '\^' marker\.$/n)
  end
  line
end

#handles_login?Boolean

Returns:

  • (Boolean)


16
17
18
# File 'lib/puppet/util/network_device/transport_ios/sshios.rb', line 16

def handles_login?
  true
end

#process_sshObject



100
101
102
103
104
105
106
107
108
# File 'lib/puppet/util/network_device/transport_ios/sshios.rb', line 100

def process_ssh
  while @buf == '' and not eof?
    begin
      @channel.connection.process(0.1)
    rescue IOError
      @eof = true
    end
  end
end

#send(line, noop = false) ⇒ Object



91
92
93
94
# File 'lib/puppet/util/network_device/transport_ios/sshios.rb', line 91

def send(line, noop = false)
  Puppet.debug "SSH_IOS send: #{line}" if Puppet[:debug]
  @channel.send_data(line + "\n") unless noop
end