Class: Ifconfig

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

Overview

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

Constant Summary collapse

@@ifcfg_cmd =

ifconfig = user provided ifconifg output netstat = same, but for netstat -in

"/usr/bin/env ifconfig -a"
@@netstat_cmd =
"/usr/bin/env netstat -in"

Instance Method Summary collapse

Constructor Details

#initialize(input = nil, verbose = nil) ⇒ Ifconfig

Can manually specify the platform (should be output of the ‘uname’ command) and the ifconfig input



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/ifconfig/linux/ifconfig.rb', line 13

def initialize(ifconfig=nil,netstat=nil,verbose=nil)
  @ifconfig = ifconfig
  @ifconfig ||= IO.popen(@@ifcfg_cmd){ |f| f.readlines.join }

  @netstat = netstat
  @netstat ||= IO.popen(@@netstat_cmd){ |f| f.readlines.join }

  @verbose = verbose

  @ifaces = {}
  split_interfaces(@ifconfig).each do |iface|
    iface_name = get_iface_name(iface)
    case iface
      when /^lo\d\:/im
        @ifaces[iface_name] = LoopbackInterface.new(iface_name,iface)
        parse_activity(iface_name)
      when /\s+media\:\s+Ethernet\s+/im
        @ifaces[iface_name] = EthernetAdapter.new(iface_name,iface)
        parse_activity(iface_name)
      when /\s+supported\smedia\:\s+none\s+autoselect\s+/im
        # This clause is only matched on Darwin. This pattern will only be
        # matched on ethernet devices (won't match on fw0 or any other
        # interface I can see).
        @ifaces[iface_name] = EthernetAdapter.new(iface_name,iface)
        parse_activity(iface_name)
      else
        puts "Unknown Adapter Type: #{iface}" if @verbose
    end
  end
end

Instance Method Details

#[](iface) ⇒ Object

Give hash like access to the interfaces



9
10
11
# File 'lib/ifconfig/common/ifconfig.rb', line 9

def [](iface)
  return @ifaces[iface]
end

#addresses(type = nil) ⇒ Object

return list of all addresses on all interfaces reported by ifconfig



55
56
57
58
59
60
61
# File 'lib/ifconfig/common/ifconfig.rb', line 55

def addresses(type=nil)
  addr = []
  @ifaces.each_value { |iface|
    addr += iface.addresses
  }
  return addr
end

#addrs_with_typeObject

returns array of arrays

[address , type

]



66
67
68
69
70
71
72
# File 'lib/ifconfig/common/ifconfig.rb', line 66

def addrs_with_type
  addr = []
  @ifaces.each_value { |iface|
    addr += iface.addrs_with_type
  }
  return addr
end

#each(&block) ⇒ Object



13
14
15
# File 'lib/ifconfig/common/ifconfig.rb', line 13

def each( &block )
  return @ifaces.each_value( &block )
end

#get_iface_name(text) ⇒ Object

Given an interface block returns the name of an interface (eth0, eth0:1 ppp0, etc.)



37
38
39
40
41
42
# File 'lib/ifconfig/common/ifconfig.rb', line 37

def get_iface_name(text)
  name = Regexp.compile(/^(\S+)/).match(text)[1]
  # strip trailing :, for bsd, sun
  name.sub!(/:$/,'')
  return name
end

#interfacesObject

return list of interface names



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

def interfaces
  return @ifaces.keys
end

#parse_activity(iface) ⇒ Object

Parse activity on interface

Not doing it in each interface class so we can pass in fake input



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/ifconfig/bsd/ifconfig.rb', line 50

def parse_activity(iface)
  mtu = rxpackets = rxerrors = txpackets = txerrors = 0
  @netstat.split("\n").each { |line|
    line.strip!
    if line =~ /^#{iface}/
      next if line.split[2] =~ /\<Link\#\d\>/
      puts "matched line for "+iface
      toks = line.split
      mtu = toks[1]
      rxpackets += toks[4].to_i
      rxerrors += toks[5].to_i
      txpackets += toks[6].to_i
      txerrors += toks[7].to_i
      @ifaces[iface].mtu = mtu.to_i
      @ifaces[iface].rx = { 'packets' => rxpackets,
                            'errors' => rxerrors }
      @ifaces[iface].tx = { 'packets' => txpackets,
                            'errors' => txerrors }
    end
  }
end

#split_interfaces(text) ⇒ Object

returns array of interface text blocks



25
26
27
28
29
30
31
32
# File 'lib/ifconfig/common/ifconfig.rb', line 25

def split_interfaces(text)
  ifaces = []
  text.split("\n").each { |line|
    ifaces[ifaces.length] = "" if line =~ /^\S/
    ifaces[ifaces.length-1] += line.rstrip+"\n"
  }
  return ifaces
end

#to_sObject



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

def to_s
  s=""
  self.interfaces.sort.each { |k|
    s += @ifaces[k].to_s
    s += "\n-------------------------\n"
  }
  return s
end

#valid_addr?(addr, type = 'inet') ⇒ Boolean

Returns:

  • (Boolean)


74
75
76
77
78
79
80
81
82
83
# File 'lib/ifconfig/common/ifconfig.rb', line 74

def valid_addr?(addr,type='inet')
  case type
    when /(inet|v4)/
      return addr.ipv4?
    when /(inet6|v6)/
      return addr.ipv6?
    else
      raise "Unknown address type `#{type}' for address `#{addr}'"
  end
end