Sha256: 4801cf72ceb36d06c197ef6394346b50a09f810765855a57bd255032e9d9247b

Contents?: true

Size: 1.86 KB

Versions: 17

Compression:

Stored size: 1.86 KB

Contents

#coding=utf-8

require "aio/core"
require "pathname"

class Aio::Module::OutputStyle::Cmds < Aio::Module::OutputStyle

	def initialize
		super({
			:author				=> "Elin",
			:description	=> "这个模块将生成一个文件夹,其中每个文件为各种设备的巡检命令, 此模块不需要Input Module",
			:file_suffix	=> "txt"
		})
	end

	def generate
		# 获得 cmds = {cmd_name => cmd_info}
		cmds = module_manager.get_modules_by_type("cmd")
		
		# 取得pf = {device => [cmd, ...]}
		pf = {}
		cmds.each_pair do |cmd, info|
			device = info[:module_layer_2]
			pf[device] = [] unless pf.has_key?(device)
			pf[device] << info[:klass].cmd_full
		end

		# 各种设备的修改方法
		terminal = {
			"cisco"		=> ["ter leng 0", "ter leng 24"],
			"h3c"			=> ["screen-length 0", "screen-length 24"],
			"maipu"		=> ["more off", "more on"],
			"juniper"	=> [" | no-more"],
		}

		text = {}
		# 生成Terminal格式
		pf.each_pair do |device, arr|
			name = device + "_terminal"
			text[name] = []
			
			# 当是cisco, h3c, maipu 的时候使用一种方式
			if device == "cisco" or device == "h3c" or device == "maipu"
				text[name] << terminal[device][0]
				arr.each do |cmd|
					text[name] << cmd
				end
				text[name] << terminal[device][1]

			# 当是 juniper 的时候使用cmd后面跟参数
			elsif device == "juniper"
				arr.each do |cmd|
					text[name] << cmd + terminal[device][0]
				end
			end

			# 最后将数组改为文本
			text[name] = text[name].join("\r\n!\r\n")
		end

		# 生成空格的格式
		pf.each_pair do |device, arr|
			name = device + "_blank"
			text[name] = arr.join("\r\n#{' '*10}\r\n!\r\n")
			text[name] << "\r\n#{' '*10}\r\n!\r\n"
		end

		pn = Pathname.new(output_file)
		Dir.mkdir(output_file) unless pn.directory?

		text.each_pair do |name, content|
			file = File.new(File.join(output_file, name), "w+")
			file.write(content)
		end
	end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
aio_elin-1.1.8 lib/modules/output/style/cmds.rb
aio_elin-1.1.7 lib/modules/output/style/cmds.rb
aio_elin-1.1.4 lib/modules/output/style/cmds.rb
aio_elin-1.1.3 lib/modules/output/style/cmds.rb
aio_elin-1.1.2 lib/modules/output/style/cmds.rb
aio_elin-1.1.1 lib/modules/output/style/cmds.rb
aio_elin-1.1.0 lib/modules/output/style/cmds.rb
aio_elin-1.0.9 lib/modules/output/style/cmds.rb
aio_elin-1.0.8 lib/modules/output/style/cmds.rb
aio_elin-1.0.7 lib/modules/output/style/cmds.rb
aio_elin-1.0.6 lib/modules/output/style/cmds.rb
aio_elin-1.0.5 lib/modules/output/style/cmds.rb
aio_elin-1.0.4 lib/modules/output/style/cmds.rb
aio_elin-1.0.3 lib/modules/output/style/cmds.rb
aio_elin-1.0.2 lib/modules/output/style/cmds.rb
aio_elin-1.0.1 lib/modules/output/style/cmds.rb
aio_elin-1.0.0 lib/modules/output/style/cmds.rb