Class: Aio::Module::OutputStyle::Citic

Inherits:
Aio::Module::OutputStyle show all
Includes:
Device, Ui::Verbose
Defined in:
lib/modules/output/style/citic.rb

Instance Attribute Summary

Attributes inherited from Aio::Module::OutputStyle

#device_manager, #module_manager, #output_file, #output_info

Instance Method Summary collapse

Methods included from Ui::Verbose

#clear_line, #print_error, #print_good, #progress_bar

Methods inherited from Aio::Module::OutputStyle

#author, #description, #each_devices_with_useful, #file_suffix, #license, #platform, #set_defaults, #type

Constructor Details

#initializeCitic

Returns a new instance of Citic.



10
11
12
13
14
15
16
17
18
# File 'lib/modules/output/style/citic.rb', line 10

def initialize
  super({
    :author		=> "Elin",
    :description	=> "此模块用于中信银行接口查询",
    :file_suffix	=> "txt",
  })

  @report = {}
end

Instance Method Details

#check_access(device_klass, iface, config) ⇒ Object

检查access接口配置是否齐全



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/modules/output/style/citic.rb', line 74

def check_access(device_klass, iface, config)
  ck = false

  # 当是思科
=begin

switchport access vlan
switchport mode access
switchport port-security
switchport port-security mac-address

或者

switchport access vlan
switchport mode access
authentication port-control auto
authentication violation protect
dot1x pae authenticator

或者

switchport access vlan
switchport mode access
dot1x mac-auth-bypass
dot1x pae authenticator
dot1x port-control auto

=end
  if device_klass.kind_of? Cisco
    point = 0b000000000
    # switchport port-security mac-address 只能出现一次
    mac_first = true

    config.each do |c|
      case c
      when /^switchport access vlan/ # 1
        point += 0b000000001
      when /^switchport mode access$/ # 2
        point += 0b000000010
      when /^switchport port-security$/ # 3
        point += 0b000000100
      when /^switchport port-security mac-address/ # 4
        point += 0b000001000 if mac_first
        mac_first = false
      when /^authentication port-control auto$/ # 5
        point += 0b000010000
      when /^authentication violation protect$/ # 6
        point += 0b000100000
      when /^dot1x pae authenticator$/ # 7
        point += 0b001000000
      when /^dot1x mac-auth-bypass$/ # 8
        point += 0b010000000
      when /^dot1x port-control auto$/ # 9
        point += 0b100000000
      end
    end

    if point == 0b000001111 or point == 0b001110011 or point == 0b111000011 or point == 0b001010011
      ck = true
    end

  # 当是H3C的时候
=begin

port link-mode bridge
port access vlan
stp edged-port
mac-address max-mac-count 0
undo mac-address max-mac-count enable-forwarding
mac-address static

=end
  elsif device_klass.kind_of? H3C
    point = 0b000000
    config.each do |c|
      case c
      when /^port link-mode bridge$/
        point += 0b000001
      when /^port access vlan$/
        point += 0b000010
      when /^stp edged-port$/
        point += 0b000100
      when /^mac-address max-mac-count 0$/
        point += 0b001000
      when /^undo mac-address max-mac-count enable-forwarding$/
        point += 0b010000
      when /^mac-address static$/
        point += 0b100000
      end
    end

    if point == 0b0011111 or point == 0b1100011
      ck = true
    end
  end

  report_error(device_klass.name, iface, 'access 接口配置不完全') unless ck
  return ck
end

#check_trunk(device_klass, iface, config) ⇒ Object

检查trunk接口配置是否齐全



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/modules/output/style/citic.rb', line 175

def check_trunk(device_klass, iface, config)
  ck = false

  if device_klass.kind_of? Cisco
    ck = true if config.include?('switchport mode trunk')

  elsif device_klass.kind_of? H3C
    ck = true if config.include?('port link-type trunk')

  else
    return false
  end

  report_error(device_klass.name, iface, 'trunk 接口配置不完全') unless ck
  return ck
end

#generateObject



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
# File 'lib/modules/output/style/citic.rb', line 20

def generate
  file = File.new(output_file, "w+")

  device_manager.devices.each_key do |device_name|
    device_klass = device_manager[device_name]

    # 当不是需要的设备类型时
    unless device_klass.kind_of?(Cisco) or device_klass.kind_of?(H3C)
      print_error "#{device_name} 不在检查设备类型内"
      next
    end

    # 当没有信息的时候,跳过
    unless device_klass.configuration?
      print_error "#{device_name} 没有配置信息"
      next
    end

    device_klass.configuration_interfaces.each_pair do |iface, cont|
      config = cont.split("\n")

      # 当接口是关闭的,那么跳过
      if config.include?('shutdown')
        next

      # 当接口是三层接口,那么跳过
      elsif config.to_s.match(/ip address/)
        next

      # 当接口是access
      elsif config.to_s.match(/port access/)
        check_access(device_klass, iface, config)

      # 当接口是trunk
      elsif config.to_s.match(/port mode|link-type trunk/)
        check_trunk(device_klass, iface, config)

      # 如有既不是access 也不是 trunk 则报错
      else
        report_error(device_name, iface, '未做配置')
      end

    end
  end

  file.write(puts_report)
end

#init_table(header) ⇒ Object



204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/modules/output/style/citic.rb', line 204

def init_table(header)
  col = ['接口', '错误配置信息']

  table = Aio::Base::Toolkit::Table.new(
    'Header'	=> header,
    'Columns' => col,
    'Indent'	=> 4,
    'HeaderIndent' => 2
  )
  
  table.sort_index = -1
  table
end

#puts_reportObject



192
193
194
195
196
197
198
199
200
201
202
# File 'lib/modules/output/style/citic.rb', line 192

def puts_report
  arr = []

  @report.each_pair do |device_name, e|
    tb = init_table(device_name)
    e.each { |row| tb << row }
    arr << tb
  end

  arr.join("\n")
end

#report_error(device_name, iface, msg) ⇒ Object



68
69
70
71
# File 'lib/modules/output/style/citic.rb', line 68

def report_error(device_name, iface, msg)
  @report[device_name] ||= []
  @report[device_name] << [iface, msg]
end