aaalex-ruby-ifconfig 1.2.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (45) hide show
  1. data/COPYING +340 -0
  2. data/Changelog +20 -0
  3. data/INSTALL +239 -0
  4. data/README +37 -0
  5. data/Rakefile +14 -0
  6. data/TODO +8 -0
  7. data/ifconfig_examples/darwin.txt +17 -0
  8. data/ifconfig_examples/dragonflybsd.txt +10 -0
  9. data/ifconfig_examples/dragonflybsd_netstat.txt +14 -0
  10. data/ifconfig_examples/freebsd.txt +17 -0
  11. data/ifconfig_examples/freebsd_netstat.txt +24 -0
  12. data/ifconfig_examples/linux.txt +60 -0
  13. data/ifconfig_examples/linux_ethernet.txt +20 -0
  14. data/ifconfig_examples/netbsd.txt +10 -0
  15. data/ifconfig_examples/openbsd.txt +36 -0
  16. data/ifconfig_examples/sunos.txt +10 -0
  17. data/lib/ifconfig.rb +71 -0
  18. data/lib/ifconfig/bsd/ifconfig.rb +72 -0
  19. data/lib/ifconfig/bsd/interface_types.rb +69 -0
  20. data/lib/ifconfig/bsd/network_types.rb +3 -0
  21. data/lib/ifconfig/common/ifconfig.rb +84 -0
  22. data/lib/ifconfig/common/interface_types.rb +130 -0
  23. data/lib/ifconfig/common/network_types.rb +49 -0
  24. data/lib/ifconfig/linux/ifconfig.rb +41 -0
  25. data/lib/ifconfig/linux/interface_types.rb +112 -0
  26. data/lib/ifconfig/linux/network_types.rb +55 -0
  27. data/lib/ifconfig/sunos/ifconfig.rb +38 -0
  28. data/lib/ifconfig/sunos/interface_types.rb +77 -0
  29. data/lib/ifconfig/sunos/network_types.rb +4 -0
  30. data/test/test_bsd.rb +35 -0
  31. data/test/test_darwin.rb +33 -0
  32. data/test/test_dragonflybsd.rb +35 -0
  33. data/test/test_helper.rb +4 -0
  34. data/test/test_linux.rb +31 -0
  35. data/test/test_netbsd.rb +33 -0
  36. data/test/test_openbsd.rb +33 -0
  37. data/test/test_sunos.rb +35 -0
  38. data/test/unit/tc_darwin.rb +40 -0
  39. data/test/unit/tc_dragonflybsd.rb +39 -0
  40. data/test/unit/tc_freebsd.rb +40 -0
  41. data/test/unit/tc_linux.rb +49 -0
  42. data/test/unit/tc_netbsd.rb +39 -0
  43. data/test/unit/tc_openbsd.rb +39 -0
  44. data/test/unit/tc_sunos.rb +44 -0
  45. metadata +101 -0
@@ -0,0 +1,69 @@
1
+ # $Id: interface_types.rb,v 1.1.1.1 2005/07/02 19:10:58 hobe Exp $
2
+ #
3
+ require 'ifconfig/common/interface_types'
4
+
5
+ class NetworkAdapter
6
+ # becuase on *BSD we get activity from netstat
7
+ # we need to have a way to set this:
8
+ attr_writer :mtu
9
+
10
+ # iterate line by line and dispatch to helper functions
11
+ # for lines that match a pattern
12
+ #
13
+ def parse_ifconfig
14
+ @ifconfig.split("\n").each { |line|
15
+ case line
16
+ when /^\s+#{@protos}/
17
+ add_network(line)
18
+ when /flags\=/i
19
+ parse_flags(line)
20
+ end
21
+ }
22
+ end
23
+
24
+ # parses the "UP LOOPBACK RUNNING MTU:3924 Metric:1" line
25
+ #
26
+ def parse_flags(line)
27
+ flags = line.match(/\<(\S+)\>/i)[1]
28
+ @flags = flags.strip.split(',')
29
+ @status = true if @flags.include?('UP')
30
+ end
31
+
32
+ # Parses networks on an interface based on the first token in the line.
33
+ # eg: inet or inet6
34
+ #
35
+ def add_network(line)
36
+ case line
37
+ when /^\s+inet\s/
38
+ addr,mask = line.match(/\s+([\d\d?\d?\.]{4,})\s+netmask\s+(\S+)/i)[1..2]
39
+ bcast = line.match(/\s+broadcast\s([\d\d?\d?\.]{4,})/i)
40
+ bcast = bcast[1] unless bcast.nil?
41
+ @networks['inet'] = Ipv4Network.new(addr, mask, bcast)
42
+ when /^\s+inet6\s/
43
+ # there can be multiple inet6 entries
44
+ begin
45
+ addr,scope = line.match(/inet6\s+(\S+)%/i)[1]
46
+ rescue NoMethodError
47
+ addr,scope = line.match(/inet6\s+(\S+)/i)[1]
48
+ end
49
+ @networks["inet6:#{addr}"] = Ipv6Network.new(addr)
50
+ else
51
+ puts "unknown network type: #{line}"
52
+ end
53
+ end
54
+ end
55
+
56
+
57
+ class EthernetAdapter
58
+ def set_mac
59
+ begin
60
+ match=@ifconfig.match(/\s+ether\s+([a-f\d]{1,2}(?:\:[a-f\d]{1,2}){5})/im)
61
+ return match[1] unless match.nil?
62
+ # Openbsd
63
+ match = @ifconfig.match(/\s+address\:\s+([a-f\d]{1,2}(?:\:[a-f\d]{1,2}){5})/im)
64
+ return match[1] unless match.nil?
65
+ rescue NoMethodError
66
+ puts "Couldn't Parse MAC Address for: "+@name
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,3 @@
1
+ # $Id: network_types.rb,v 1.1.1.1 2005/07/02 19:10:58 hobe Exp $
2
+ #
3
+ require 'ifconfig/common/network_types'
@@ -0,0 +1,84 @@
1
+ # $Id: ifconfig.rb,v 1.1.1.1 2005/07/02 19:10:58 hobe Exp $
2
+ #
3
+ class Ifconfig
4
+ include Enumerable
5
+
6
+ #
7
+ # Give hash like access to the interfaces
8
+ #
9
+ def [](iface)
10
+ return @ifaces[iface]
11
+ end
12
+
13
+ def each( &block )
14
+ return @ifaces.each_value( &block )
15
+ end
16
+
17
+ # return list of interface names
18
+ #
19
+ def interfaces
20
+ return @ifaces.keys
21
+ end
22
+
23
+ # returns array of interface text blocks
24
+ #
25
+ def split_interfaces(text)
26
+ ifaces = []
27
+ text.split("\n").each { |line|
28
+ ifaces[ifaces.length] = "" if line =~ /^\S/
29
+ ifaces[ifaces.length-1] += line.rstrip+"\n"
30
+ }
31
+ return ifaces
32
+ end
33
+
34
+ # Given an interface block
35
+ # returns the name of an interface (eth0, eth0:1 ppp0, etc.)
36
+ #
37
+ def get_iface_name(text)
38
+ name = Regexp.compile(/^(\S+)/).match(text)[1]
39
+ # strip trailing :, for bsd, sun
40
+ name.sub!(/:$/,'')
41
+ return name
42
+ end
43
+
44
+ def to_s
45
+ s=""
46
+ self.interfaces.sort.each { |k|
47
+ s += @ifaces[k].to_s
48
+ s += "\n-------------------------\n"
49
+ }
50
+ return s
51
+ end
52
+
53
+ # return list of all addresses on all interfaces reported by ifconfig
54
+ #
55
+ def addresses(type=nil)
56
+ addr = []
57
+ @ifaces.each_value { |iface|
58
+ addr += iface.addresses
59
+ }
60
+ return addr
61
+ end
62
+
63
+ # returns array of arrays
64
+ # [ [address , type ] ]
65
+ #
66
+ def addrs_with_type
67
+ addr = []
68
+ @ifaces.each_value { |iface|
69
+ addr += iface.addrs_with_type
70
+ }
71
+ return addr
72
+ end
73
+
74
+ def valid_addr?(addr,type='inet')
75
+ case type
76
+ when /(inet|v4)/
77
+ return addr.ipv4?
78
+ when /(inet6|v6)/
79
+ return addr.ipv6?
80
+ else
81
+ raise "Unknown address type `#{type}' for address `#{addr}'"
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,130 @@
1
+ # $Id: interface_types.rb,v 1.1.1.1 2005/07/02 19:10:58 hobe Exp $
2
+ #
3
+ class NetworkAdapter
4
+ def initialize(name, ifacetxt)
5
+ @name = name
6
+ @ifconfig = ifacetxt
7
+ @status = false
8
+ @protos = ['inet','inet6','IPX/Ethernet II',
9
+ 'IPX/Ethernet SNAP',
10
+ 'IPX/Ethernet 802.2',
11
+ 'IPX/Ethernet 802.3',
12
+ 'EtherTalk Phase 2'].join("|")
13
+ @networks = {}
14
+ @flags = []
15
+ @mtu = nil
16
+ @metric = nil
17
+ @rx = @tx = {}
18
+ parse_ifconfig
19
+ end
20
+ attr_reader :status, :name, :flags, :mtu
21
+ attr_accessor :tx, :rx
22
+
23
+ # take array and turn each two entries into
24
+ # hash key and value, also converts each value to an integer
25
+ #
26
+ # [1,2,3,4] => { 1 => 2, 3 => 4}
27
+ #
28
+ # Internal utility function used to populate rx and tx hashes
29
+ def array_to_hash_elem(array)
30
+ h = {}
31
+ if array.length.modulo(2) != 0
32
+ puts "Array mus have even number of elements to turn into a hash"
33
+ return nil
34
+ end
35
+ while array.length > 0
36
+ h[array.shift] = array.shift.to_i
37
+ end
38
+ return h
39
+ end
40
+
41
+ # Return all addresses bound to this interface or
42
+ # optionally only the specified type of network address
43
+ #
44
+ def addresses(type=nil)
45
+ a = []
46
+ @networks.each_value { |network|
47
+ a << (network.addr) if network.nettype == type or type.nil?
48
+ }
49
+ return a
50
+ end
51
+
52
+ def ifacetype
53
+ return self.class
54
+ end
55
+
56
+ def up?
57
+ return status
58
+ end
59
+
60
+ # returns array of arrays
61
+ # [ [address , type ] ]
62
+ #
63
+ def addrs_with_type
64
+ addrs = []
65
+ @networks.each_value { |network|
66
+ addrs.push([network.addr,network.nettype])
67
+ }
68
+ return addrs
69
+ end
70
+
71
+ def addr_types
72
+ types = []
73
+ @networks.each_value { |network|
74
+ types.push(network.nettype) unless types.include?(network.nettype)
75
+ }
76
+ return types
77
+ end
78
+
79
+ def has_addr?(addr)
80
+ return self.addresses.include?(addr)
81
+ end
82
+
83
+ def to_s
84
+ s = @name+":"+self.ifacetype.to_s+"\n"
85
+ @networks.keys.sort.each { |network|
86
+ s += @networks[network].to_s+"\n"
87
+ }
88
+ if self.rx['bytes'] && self.tx['bytes']
89
+ s += " RX bytes: #{self.rx['bytes']}, TX bytes: #{self.tx['bytes']}\n"
90
+ elsif self.rx['packets'] && self.tx['packets']
91
+ s += " RX packets: #{self.rx['packets']}, TX packets: #{self.tx['packets']}\n"
92
+ end
93
+
94
+ s += " MTU: #{@mtu}\n"
95
+ s += " Metric: #{@metric}\n"
96
+ s += " Flags: #{@flags.join(',')}\n"
97
+ s += " Status: UP" if self.status
98
+ return s
99
+ end
100
+ end
101
+
102
+ # each platform defines it's own set_mac
103
+ # function to get the mac address
104
+ #
105
+ class EthernetAdapter < NetworkAdapter
106
+ def initialize(name,ifconfigtxt)
107
+ super(name,ifconfigtxt)
108
+ @mac = set_mac
109
+ end
110
+
111
+ attr_reader :mac, :interrupt, :rxbytes, :txbytes, :rxpackets,
112
+ :txpackets
113
+
114
+ def to_s
115
+ super + "\n MAC: #{@mac}"
116
+ end
117
+
118
+ end
119
+
120
+ class PPP < NetworkAdapter
121
+ end
122
+
123
+ class LoopbackInterface < NetworkAdapter
124
+ end
125
+
126
+ class IPv6_in_IPv4 < NetworkAdapter
127
+ end
128
+
129
+ class SerialLineIP < NetworkAdapter
130
+ end
@@ -0,0 +1,49 @@
1
+ # $Id: network_types.rb,v 1.1.1.1 2005/07/02 19:10:58 hobe Exp $
2
+ #
3
+
4
+ # base type to hold information about the kinds of networks
5
+ # on to each adapter. ipv4,6 ppp etc
6
+ class BasicNetworkType
7
+ def initialize
8
+ @nettype = nil
9
+ end
10
+ attr_reader :nettype
11
+ end
12
+
13
+ #
14
+ # Ipv4 Network type
15
+ # Optional Broadcast and Point to Point Arguments
16
+ #
17
+ class Ipv4Network < BasicNetworkType
18
+ def initialize(addr,mask,bcast=nil,ptp=nil)
19
+ super()
20
+ @nettype = 'inet'
21
+ @addr = IPAddr.new(addr)
22
+ @bcast = bcast
23
+ @mask = mask
24
+ @ptp = ptp
25
+ end
26
+ attr_reader :addr, :bcast, :mask, :ptp
27
+
28
+ def to_s
29
+ a = [" #{@nettype} Address: #{@addr}","Mask: #{@mask}"]
30
+ a.push "Broadcast: #{@bcast}" unless @bcast.nil?
31
+ a.push "P-t-P: #{@ptp}" unless @ptp.nil?
32
+ return a.join(', ')
33
+ end
34
+
35
+ end
36
+
37
+ class Ipv6Network < BasicNetworkType
38
+ def initialize(addr,scope=nil)
39
+ super()
40
+ @nettype = 'inet6'
41
+ @addr = IPAddr.new(addr)
42
+ @scope = scope
43
+ end
44
+ attr_reader :addr, :scope
45
+
46
+ def to_s
47
+ " #{@nettype} Address: #{@addr}"
48
+ end
49
+ end
@@ -0,0 +1,41 @@
1
+ # $Id: ifconfig.rb,v 1.1.1.1 2005/07/02 19:10:57 hobe Exp $
2
+ #
3
+
4
+ require 'ifconfig/common/ifconfig'
5
+ require 'ifconfig/linux/network_types'
6
+ require 'ifconfig/linux/interface_types'
7
+
8
+ class Ifconfig
9
+ #
10
+ # Can manually specify the platform (should be output of the 'uname' command)
11
+ # and the ifconfig input
12
+ #
13
+ def initialize(input=nil,verbose=nil)
14
+ if input.nil?
15
+ cmd = IO.popen('which ifconfig'){ |f| f.readlines[0] }
16
+ exit unless !cmd.nil?
17
+ @ifconfig = IO.popen("/sbin/ifconfig -a"){ |f| f.readlines.join }
18
+ else
19
+ @ifconfig = input
20
+ end
21
+ @verbose = verbose
22
+ @ifaces = {}
23
+ split_interfaces(@ifconfig).each do |iface|
24
+ iface_name = get_iface_name(iface)
25
+ case iface
26
+ when /encap\:ethernet/im
27
+ @ifaces[iface_name] = EthernetAdapter.new(iface_name,iface)
28
+ when /encap\:Local Loopback/im
29
+ @ifaces[iface_name] = LoopbackInterface.new(iface_name,iface)
30
+ when /encap\:IPv6-in-IPv4/im
31
+ @ifaces[iface_name] = IPv6_in_IPv4.new(iface_name,iface)
32
+ when /encap\:Point-to-Point Protocol/im
33
+ @ifaces[iface_name] = PPP.new(iface_name,iface)
34
+ when /encap\:Serial Line IP/im
35
+ @ifaces[iface_name] = SerialLineIP.new(iface_name,iface)
36
+ else
37
+ puts "Unknown Adapter Type on Linux: #{iface}" if @verbose
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,112 @@
1
+ # $Id: interface_types.rb,v 1.1.1.1 2005/07/02 19:10:57 hobe Exp $
2
+ #
3
+
4
+ require 'ifconfig/common/interface_types'
5
+
6
+ class NetworkAdapter
7
+ # Parse activity on interface
8
+ #
9
+ def parse_activity(line)
10
+ atr = %w(packets,errors,dropped,overruns)
11
+ case line.strip!
12
+
13
+ when /^RX packets/
14
+ @rx = array_to_hash_elem(
15
+ line.match(/^RX\s+(\w+)\:(\d+)\s+(\w+)\:(\d+)\s+(\w+)\:(\d+)\s+(\w+)\:(\d)/i)[1..8])
16
+ match = line.match(/\s+frame\:(\d+)/i)
17
+ @rx['frame'] = match[1] unless match.nil?
18
+
19
+ when /^TX packets/
20
+ @tx = array_to_hash_elem(
21
+ line.match(/^TX\s+(\w+)\:(\d+)\s+(\w+)\:(\d+)\s+(\w+)\:(\d+)\s+(\w+)\:(\d+)/i)[1..8])
22
+ match = line.match(/\s+carrier\:(\d+)/i)
23
+ @tx['carrier'] = match[1] unless match.nil?
24
+
25
+ when /^RX bytes/
26
+ match = line.match(/RX\s+(\w+)\:(\d+).*TX\s+(\w+)\:(\d+)/i)[1..4]
27
+ @rx.merge!(array_to_hash_elem(match[0..1]))
28
+ @tx.merge!(array_to_hash_elem(match[2..3]))
29
+ end
30
+ end
31
+
32
+ # iterate line by line and dispatch to helper functions
33
+ # for lines that match a pattern
34
+ #
35
+ def parse_ifconfig
36
+ @ifconfig.split("\n").each { |line|
37
+ case line
38
+ when /^\s+#{@protos}/
39
+ add_network(line)
40
+ when /MTU\:\d+\s+Metric\:/
41
+ parse_flags(line)
42
+ when /^\s+RX|TX/
43
+ parse_activity(line)
44
+ end
45
+ }
46
+ end
47
+
48
+ # parses the "UP LOOPBACK RUNNING MTU:3924 Metric:1" line
49
+ #
50
+ def parse_flags(line)
51
+ flags = line.strip.split
52
+ @metric = flags.pop.split(':')[1].to_i
53
+ @mtu = flags.pop.split(':')[1].to_i
54
+ @flags = flags
55
+ @status = true if @flags.include?('UP')
56
+ end
57
+
58
+ # Parses networks on an interface based on the first token in the line.
59
+ # eg: inet or inet6
60
+ #
61
+ def add_network(line)
62
+ case line
63
+ when /^\s+inet\s/
64
+ addr = line.match(/\s+addr\:([\d\d?\d?\.]{4,})/i)[1]
65
+ mask = line.match(/\s+mask\:([\d\d?\d?\.]{4,})/i)[1]
66
+ bcast = line.match(/\s+bcast\:([\d\d?\d?\.]{4,})/i)
67
+ bcast = bcast[1] unless bcast.nil?
68
+ ptp = line.match(/\s+P-t-P\:([\d\d?\d?\.]{4,})/i)
69
+ ptp = ptp[1] unless ptp.nil?
70
+ @networks['inet'] = Ipv4Network.new(addr, mask, bcast, ptp)
71
+
72
+ when /^\s+inet6\s/
73
+ # there can be multiple inet6 entries
74
+ addr,scope = line.match(/\s+addr\:\s+(\S+)\s+scope\:\s*(\w+)/i)[1..2]
75
+ @networks["inet6:#{addr}"] = Ipv6Network.new(addr,scope)
76
+
77
+ when /^\s+#{Regexp.escape('IPX/Ethernet II')}\s/
78
+ addr = line.match(/\s+addr\:\s*(\S+)/)[1]
79
+ @networks["IPX/Ethernet II"] = IPX_EthernetII.new(addr)
80
+
81
+ when /^\s+#{Regexp.escape('IPX/Ethernet 802.2')}\s/
82
+ addr = line.match(/\s+addr\:\s*(\S+)/)[1]
83
+ @networks["IPX/Ethernet 802.2"] = IPX_Ethernet802_2.new(addr)
84
+
85
+ when /^\s+#{Regexp.escape('IPX/Ethernet 802.3')}\s/
86
+ addr = line.match(/\s+addr\:\s*(\S+)/)[1]
87
+ @networks["IPX/Ethernet 802.3"] = IPX_Ethernet802_3.new(addr)
88
+
89
+ when /^\s+#{Regexp.escape('EtherTalk Phase 2')}\s/
90
+ addr = line.match(/\s+addr\:\s*(\S+)/)[1]
91
+ @networks["EtherTalk Phase 2"] = EtherTalkPhase2.new(addr)
92
+
93
+ when /^\s+#{Regexp.escape('IPX/Ethernet SNAP')}\s/
94
+ addr = line.match(/\s+addr\:\s*(\S+)/)[1]
95
+ @networks["IPX/Ethernet SNAP"] = IPX_EthernetSNAP.new(addr)
96
+
97
+ else
98
+ puts "unknown network type: #{line}"
99
+ end
100
+ end
101
+ end
102
+
103
+
104
+ class EthernetAdapter
105
+ def set_mac
106
+ begin
107
+ return @ifconfig.match(/\s+hwaddr\s+([a-f\d]{1,2}(?:\:[a-f\d]{1,2}){5})/im)[1]
108
+ rescue NoMethodError
109
+ puts "Couldn't Parse MAC Address for: #{@name}: #{$!}"
110
+ end
111
+ end
112
+ end