Class: Netdisco::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/netdisco/cli.rb

Defined Under Namespace

Classes: MissingHost, NoConfig

Instance Method Summary collapse

Constructor Details

#initializeCLI

类对象初始化入口函数

Raises:



35
36
37
38
39
40
41
42
43
44
# File 'lib/netdisco/cli.rb', line 35

def initialize
  raise NoConfig, "edit ~/.config/netdisco/config" if CONFIG.create
  args, @opts = opt_parse
  @host       = DNS.getip args.shift
  raise MissingHost, "no hostname given as argument" unless @host

  CFG.snmp.community = @opts[:community] if @opts[:community]
  CFG.debug          = true if @opts[:debug]
  CFG.ipname         = true if @opts[:ipname]
end

Instance Method Details

#opt_parseObject

CLI 命令行交互解析



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/netdisco/cli.rb', line 47

def opt_parse
  opts = Slop.parse do |o|
    o.on "-h", "--help", "show usage" do
      puts o
      exit
    end
    o.on "-g", "--graphviz", "dot output use 'dot -Tpng -o map.png map.dot'"
    o.on "-l", "--list", "list nodes"
    o.on "-j", "--json", "json output"
    o.on "-y", "--yaml", "yaml output"
    o.on "-a", "--hash", "hash/associative array output"
    o.on "-r", "--resolve", "resolve addresses to names"
    o.on "-p", "--purge", "remove peers not in configured CIDR"
    o.string "-c=", "--community", "SNMP community to use"
    o.bool "-d", "--debug", "turn debugging on"
    o.bool "-i", "--ipname", "use rev(ip) name instead of discovered name"
    o.on "-v", "--version", "print the version" do
      puts Slop::VERSION
      exit
    end
  end
  [opts.arguments, opts]
end

#runObject

网络邻居发现入口函数



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/netdisco/cli.rb', line 13

def run
  Log.debug "RUNNING Netdisco.new.discover"
  output = Netdisco.new.discover @host

  # 邻居关系发现后修正处理逻辑钩子函数
  output.clean! if @opts[:purge]
  output.resolve! if @opts[:resolve]
  # 将计算结果转换格式打印
  if @opts[:graphviz]
    output.to_dot
  elsif @opts[:list]
    output.to_a
  elsif @opts[:json]
    output.to_json
  elsif @opts[:yaml]
    output.to_yaml
  else
    output.to_hash
  end
end