Class: NetworkAdapter

Inherits:
Object
  • Object
show all
Defined in:
lib/ifconfig/sunos/interface_types.rb,
lib/ifconfig/bsd/interface_types.rb,
lib/ifconfig/linux/interface_types.rb,
lib/ifconfig/common/interface_types.rb

Overview

$Id: interface_types.rb,v 1.1.1.1 2005/07/02 19:10:58 hobe Exp $

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, ifacetxt) ⇒ NetworkAdapter

Returns a new instance of NetworkAdapter.



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/ifconfig/common/interface_types.rb', line 4

def initialize(name, ifacetxt)
  @name = name
  @ifconfig = ifacetxt
  @status = false
  @protos = ['inet','inet6','IPX/Ethernet II',
             'IPX/Ethernet SNAP',
             'IPX/Ethernet 802.2',
             'IPX/Ethernet 802.3',
             'EtherTalk Phase 2'].join("|")
  @networks = {}
  @flags = []
  @mtu = nil
  @metric = nil
  @rx = @tx = {}
  parse_ifconfig
end

Instance Attribute Details

#flagsObject (readonly)

Returns the value of attribute flags.



20
21
22
# File 'lib/ifconfig/common/interface_types.rb', line 20

def flags
  @flags
end

#mtuObject

Returns the value of attribute mtu.



20
21
22
# File 'lib/ifconfig/common/interface_types.rb', line 20

def mtu
  @mtu
end

#nameObject (readonly)

Returns the value of attribute name.



20
21
22
# File 'lib/ifconfig/common/interface_types.rb', line 20

def name
  @name
end

#rxObject

Returns the value of attribute rx.



21
22
23
# File 'lib/ifconfig/common/interface_types.rb', line 21

def rx
  @rx
end

#statusObject (readonly)

Returns the value of attribute status.



20
21
22
# File 'lib/ifconfig/common/interface_types.rb', line 20

def status
  @status
end

#txObject

Returns the value of attribute tx.



21
22
23
# File 'lib/ifconfig/common/interface_types.rb', line 21

def tx
  @tx
end

Instance Method Details

#add_network(line) ⇒ Object

Parses networks on an interface based on the first token in the line. eg: inet or inet6



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/ifconfig/bsd/interface_types.rb', line 35

def add_network(line)
  case line
    when /^\s+inet\s/
      addr,mask = line.match(/\s+([\d\d?\d?\.]{4,})\s+netmask\s+(\S+)/i)[1..2]
      bcast = line.match(/\s+broadcast\s([\d\d?\d?\.]{4,})/i)
      bcast = bcast[1] unless bcast.nil?
      @networks['inet'] = Ipv4Network.new(addr, mask, bcast)
    when /^\s+inet6\s/
      # there can be multiple inet6 entries
      begin
        addr,scope = line.match(/inet6\s+(\S+)%/i)[1]
      rescue NoMethodError
        addr,scope = line.match(/inet6\s+(\S+)/i)[1]
      end
      @networks["inet6:#{addr}"] = Ipv6Network.new(addr)
    else
      puts "unknown network type: #{line}"
  end
end

#addr_typesObject



71
72
73
74
75
76
77
# File 'lib/ifconfig/common/interface_types.rb', line 71

def addr_types
  types = []
  @networks.each_value { |network|
    types.push(network.nettype) unless types.include?(network.nettype)
  }
  return types
end

#addresses(type = nil) ⇒ Object

Return all addresses bound to this interface or optionally only the specified type of network address



44
45
46
47
48
49
50
# File 'lib/ifconfig/common/interface_types.rb', line 44

def addresses(type=nil)
  a = []
  @networks.each_value { |network|
    a << (network.addr) if network.nettype == type or type.nil?
  }
  return a
end

#addrs_with_typeObject

returns array of arrays

[address , type

]



63
64
65
66
67
68
69
# File 'lib/ifconfig/common/interface_types.rb', line 63

def addrs_with_type
  addrs = []
  @networks.each_value { |network|
    addrs.push([network.addr,network.nettype])
  }
  return addrs
end

#array_to_hash_elem(array) ⇒ Object

take array and turn each two entries into hash key and value, also converts each value to an integer

1,2,3,4

> { 1 => 2, 3 => 4}

Internal utility function used to populate rx and tx hashes



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/ifconfig/common/interface_types.rb', line 29

def array_to_hash_elem(array)
  h = {}
  if array.length.modulo(2) != 0
    puts "Array mus have even number of elements to turn into a hash"
    return nil
  end
  while array.length > 0
    h[array.shift] = array.shift.to_i
  end
  return h
end

#has_addr?(addr) ⇒ Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/ifconfig/common/interface_types.rb', line 79

def has_addr?(addr)
  return self.addresses.include?(addr)
end

#ifacetypeObject



52
53
54
# File 'lib/ifconfig/common/interface_types.rb', line 52

def ifacetype
  return self.class
end

#parse_activityObject

Parse activity on interface



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/ifconfig/linux/interface_types.rb', line 9

def parse_activity(line)
  atr = %w(packets,errors,dropped,overruns)
  case line.strip!

  when /^RX packets/
    @rx = array_to_hash_elem(
      line.match(/^RX\s+(\w+)\:(\d+)\s+(\w+)\:(\d+)\s+(\w+)\:(\d+)\s+(\w+)\:(\d)/i)[1..8])
    match = line.match(/\s+frame\:(\d+)/i)
    @rx['frame'] = match[1] unless match.nil?

  when /^TX packets/
    @tx = array_to_hash_elem(
      line.match(/^TX\s+(\w+)\:(\d+)\s+(\w+)\:(\d+)\s+(\w+)\:(\d+)\s+(\w+)\:(\d+)/i)[1..8])
    match = line.match(/\s+carrier\:(\d+)/i)
    @tx['carrier'] = match[1] unless match.nil?

  when /^RX bytes/
    match = line.match(/RX\s+(\w+)\:(\d+).*TX\s+(\w+)\:(\d+)/i)[1..4]
    @rx.merge!(array_to_hash_elem(match[0..1]))
    @tx.merge!(array_to_hash_elem(match[2..3]))
  end
end

#parse_flags(line) ⇒ Object

parses the “UP LOOPBACK RUNNING MTU:3924 Metric:1” line



26
27
28
29
30
# File 'lib/ifconfig/bsd/interface_types.rb', line 26

def parse_flags(line)
  flags = line.match(/\<(\S+)\>/i)[1]
  @flags = flags.strip.split(',')
  @status = true if @flags.include?('UP')
end

#parse_ifconfigObject

iterate line by line and dispatch to helper functions for lines that match a pattern



13
14
15
16
17
18
19
20
21
22
# File 'lib/ifconfig/bsd/interface_types.rb', line 13

def parse_ifconfig
  @ifconfig.split("\n").each { |line|
    case line
      when /^\s+#{@protos}/
        add_network(line)
      when /flags\=/i
        parse_flags(line)
    end
  }
end

#to_sObject



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/ifconfig/common/interface_types.rb', line 83

def to_s
  s = @name+":"+self.ifacetype.to_s+"\n"
  @networks.keys.sort.each { |network|
    s += @networks[network].to_s+"\n"
  }
  if self.rx['bytes'] && self.tx['bytes']
    s += " RX bytes: #{self.rx['bytes']}, TX bytes: #{self.tx['bytes']}\n"
  elsif self.rx['packets'] && self.tx['packets']
    s += " RX packets: #{self.rx['packets']}, TX packets: #{self.tx['packets']}\n"
  end

  s += " MTU: #{@mtu}\n"
  s += " Metric: #{@metric}\n"
  s += " Flags: #{@flags.join(',')}\n"
  s += " Status: UP" if self.status
  return s
end

#up?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/ifconfig/common/interface_types.rb', line 56

def up?
  return status
end