summaryrefslogtreecommitdiff
path: root/lib/oxidized/model/tplink.rb
blob: 9286ea4b3cb5558c47c2b5474f4b0c2b342fdd42 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
class TPLink < Oxidized::Model

  # tp-link prompt
  prompt /^\r?([\w.@()-]+[#>]\s?)$/
  comment  '! '

  # handle paging
  # workaround for sometimes missing whitespaces with "\s?"
  expect /Press\s?any\s?key\s?to\s?continue\s?\(Q\s?to\s?quit\)/ do |data, re|
   send ' '
   data.sub re, ''
  end

  # send carriage return because \n with the command is not enough
  # checks if line ends with prompt >,# or \r,\nm otherwise send \r
  expect /[^>#\r\n]$/ do |data, re|
   send "\r"
   data.sub re, ''
  end

  cmd :all do |cfg|
    # normalize linefeeds
    cfg.gsub! /(\r|\r\n|\n\r)/,"\n"
    # remove empty lines
    cfg.each_line.reject { |line| line.match /^[\r\n\s\u0000#]+$/ }.join
  end

  cmd :secret do |cfg|
    cfg.gsub! /^(snmp-server community).*/, '\\1 <configuration removed>'
    cfg.gsub! /secret (\d+) (\S+).*/, '<secret hidden>'
    cfg
  end

  cmd 'show system-info'  do |cfg|
    comment cfg.each_line.to_a[3..-3].join
  end

  cmd 'show running-config' do |cfg|
    lines = cfg.each_line.to_a[1..-1]
    # cut config after "end"
    lines[0..lines.index("end\n")].join
  end

  cfg :telnet, :ssh do
    username /^User ?[nN]ame:/
    password /^\r?Password:/
  end

  cfg :telnet, :ssh do
    if vars :enable
      post_login do
        send "enable\r"
        send vars(:enable) + "\r"
      end
    end

    pre_logout do
      send "exit\r"
      send "logout\r"
    end

  end

end