summaryrefslogtreecommitdiff
path: root/lib/oxidized/model/awplus.rb
blob: 1d8fbcdc0ebb072fd95c859ea941fc45d1a13790 (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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
class AWPlus < Oxidized::Model

  #Allied Telesis Alliedware Plus Model#
  #https://www.alliedtelesis.com/products/software/AlliedWare-Plus
  
  prompt /^(\r?[\w.@:\/-]+[#>]\s?)$/
  comment  '! '

  #Avoids needing "term length 0" to display full config file. 
    expect /--More--/ do |data, re|
      send ' '
      data.sub re, ''
    end

  #Removes gibberish pager output e.g. VT100 escape codes
  cmd :all do |cfg|
    cfg.gsub! /\e\[K/, ''         # example how to handle pager - cleareol EL0
    cfg.gsub! /\e\[7m\e\[m/, ''   # example how to handle pager - Reverse SGR7
    cfg.gsub! /\r/, ''  # Filters rogue ^M - see issue #415
    cfg.each_line.to_a[1..-2].join
  end

  #Remove passwords from config file.
  #Add vars "remove_secret: true" to global oxidized config file to enable. 

  cmd :secret do |cfg|
    cfg.gsub! /^(snmp-server community).*/, '\\1 <configuration removed>'
    cfg.gsub! /^(username \S+ privilege \d+) (\S+).*/, '\\1 <secret hidden>'
    cfg.gsub! /^(username \S+ password \d) (\S+)/, '\\1 <secret hidden>'
    cfg.gsub! /^(username \S+ secret \d) (\S+)/, '\\1 <secret hidden>'
    cfg.gsub! /^(enable (password|secret) \d) (\S+)/, '\\1 <secret hidden>'
    cfg.gsub! /^(\s+(?:password|secret)) (?:\d )?\S+/, '\\1 <secret hidden>'
    cfg.gsub! /^(tacacs-server key \d) (\S+)/, '\\1 <secret hidden>'
    cfg
  end

  #Adds "Show system" output to start of config. 

  cmd 'Show System' do |cfg|
    comment cfg.insert(0,"--------------------------------------------------------------------------------! \n")
    #Unhash below to write a comment in the config file.
    cfg.insert(0,"Starting: Show system cmd \n")
    cfg << "\n \nEnding: show system cmd"
    comment cfg << "\n--------------------------------------------------------------------------------! \n \n"
    #Removes the following lines from "show system" in output file. This ensures oxidized diffs are meaningful. 
    comment cfg.each_line.reject { |line| 
                                    line.match /^$\n/ or #Remove blank lines in "sh sys"
                                    line.match /System Status\s*.*/ or
                                    line.match /RAM\s*:.*/ or 
                                    line.match /Uptime\s*:.*/ or 
                                    line.match /Flash\s*:.*/ or 
                                    line.match /Current software\s*:.*/ or 
                                    line.match /Software version\s*:.*/ or 
                                    line.match /Build date\s*:.*/ }.join
  end
     
  #Actually get the devices running config#
  cmd 'show running-config' do |cfg|
    cfg
  end
  
  #Config required for telnet to detect username prompt
  cfg :telnet do
    username /login:\s/
    end

  #Main login config
    cfg :telnet, :ssh do
    post_login do
      if vars :enable
        send "enable\n"
        expect /^Password:\s/
        cmd vars(:enable) + "\r\n"
      else
        cmd 'enable' # Required for Priv-Exec users without enable PW to be put into "enable mode". 
      end
#      cmd 'terminal length 0' #set so the entire config is output without intervention.
    end
    pre_logout do
#      cmd 'terminal no length' #Sets term length back to default on exit. 
      send  "exit\r\n"
    end
  end  

end