Module: Netutils

Defined in:
lib/netutils.rb,
lib/netutils/version.rb

Constant Summary collapse

ACL_MAX_SEQ =
4294967294
VERSION =
"0.1.1"

Instance Method Summary collapse

Instance Method Details

#interface_name_vlan_id(name) ⇒ Object



64
65
66
67
# File 'lib/netutils.rb', line 64

def interface_name_vlan_id(name)
  return $1 if name =~ /^vlan([0-9]+)$/i
  nil
end

#interface_sanity_check(host, port) ⇒ Object



35
36
37
38
39
40
# File 'lib/netutils.rb', line 35

def interface_sanity_check(host, port)
  # XXX: need more checks to detect backbone links.
  if port !~ /^[gf]/i
    raise "Suppress shutdown #{port}, which may be backbone link."
  end
end

#log(m) ⇒ Object



47
48
49
50
# File 'lib/netutils.rb', line 47

def log(m)
  puts m
  $log += m + "\n"
end

#log_bufferObject



52
53
54
# File 'lib/netutils.rb', line 52

def log_buffer
  return $log
end

#log_without_newline(m) ⇒ Object



42
43
44
45
# File 'lib/netutils.rb', line 42

def log_without_newline(m)
  print m
  $log += m
end

#mail(s, b) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/netutils.rb', line 17

def mail(s, b)
  m = Mail.new do
    delivery_method :smtp, address: MAILSERVER
    from MAILFROM
    to MAILTO
    subject s
    body b
  end
  m.charset = 'ascii'
  m.deliver!
end

#router_locate(ia) ⇒ Object



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
# File 'lib/netutils.rb', line 83

def router_locate(ia)
  root = SWITCHES[0]
  sw = Switch.new(root[0], Switch::Type::ROUTER, root[1])
  sw.

  while true
    rts = sw.route_gets(ia)
    raise "No route found for #{ia}" if ! rts
    bestrt = nil
    rts.each do |rt|
      case rt.proto
      when 'connected'
        return sw, ia
      when 'static', 'rip', 'ospf', 'bgp'
        # just in case for redistributed routes.
        next if rt.nh === nil && ! rt.tunnel?
        bestrt = rt.compare(bestrt)
      end
    end
    raise "No valid route found for #{ia}" if ! bestrt

    if OTHER_NEXTHOPS.include?(bestrt.nh)
      return sw, bestrt.nh
    end

    if bestrt.tunnel?
      nh = tunnel_nexthop_resolve(sw, bestrt)
    else
      nh = bestrt.nh
    end
    sw = Switch.new(nil, Switch::Type::ROUTER, nh)
    sw.
  end
end

#static_neighbor_resolve(name, ifname) ⇒ Object



69
70
71
72
73
74
# File 'lib/netutils.rb', line 69

def static_neighbor_resolve(name, ifname)
  key = "#{name}_#{ifname}"
  n = STATIC_NEIGHBOR[key]
  return nil if n.nil?
  Switch.get(n[:name], Switch::Type::ROUTER, nil, nil, nil, n[:ia])
end

#tunnel_nexthop_resolve(sw, rt) ⇒ Object



76
77
78
79
80
81
# File 'lib/netutils.rb', line 76

def tunnel_nexthop_resolve(sw, rt)
  return rt.nh if rt.nh
  c = CDP.new(nil)
  c.parse(sw.cli.cmd("show cdp neighbors #{rt.interface} detail"))
  c.ias[0]
end

#valid_ip_address?(s) ⇒ Boolean

Returns:

  • (Boolean)


29
30
31
32
33
# File 'lib/netutils.rb', line 29

def valid_ip_address?(s)
  return false if s !~ /^(?:[0-9]+\.){3}[0-9]+$/
  results = s.split('.').collect { |i| i.to_i.between?(0, 255) }
  return results.count(true) === 4
end

#vlans_by_switch_name(swname, vlan) ⇒ Object



56
57
58
59
60
61
62
# File 'lib/netutils.rb', line 56

def vlans_by_switch_name(swname, vlan)
  vlan = vlan.to_i
  vlans = []
  vlans = VLANS[swname].dup if VLANS.has_key?(swname)
  vlans.unshift(vlan) if vlan && ! vlans.include?(vlan)
  return vlans
end