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,55 @@
1
+ # $Id: network_types.rb,v 1.1.1.1 2005/07/02 19:10:57 hobe Exp $
2
+ #
3
+
4
+ require 'ifconfig/common/network_types'
5
+
6
+ #
7
+ # Base class for IPX and Appletalk classes
8
+ # Shouldn't be used directly
9
+ #
10
+ class MiscEthernet < BasicNetworkType
11
+ def initialize(addr)
12
+ super()
13
+ @addr = addr
14
+ end
15
+ attr_reader :addr
16
+ def to_s
17
+ " #{@nettype} Address: #{@addr}"
18
+ end
19
+ end
20
+
21
+ class IPX_EthernetII < MiscEthernet
22
+ def initialize(addr)
23
+ super(addr)
24
+ @nettype = 'IPX/Ethernet II'
25
+ end
26
+ end
27
+
28
+ class IPX_Ethernet802_2 < MiscEthernet
29
+ def initialize(addr)
30
+ super(addr)
31
+ @nettype = 'IPX/Ethernet 802.2'
32
+ end
33
+ end
34
+
35
+ class IPX_Ethernet802_3 < MiscEthernet
36
+ def initialize(addr)
37
+ super(addr)
38
+ @nettype = 'IPX/Ethernet 802.3'
39
+ end
40
+ end
41
+
42
+ class EtherTalkPhase2 < MiscEthernet
43
+ def initialize(addr)
44
+ super(addr)
45
+ @nettype = 'EtherTalk Phase 2'
46
+ end
47
+ end
48
+
49
+ class IPX_EthernetSNAP < MiscEthernet
50
+ def initialize(addr)
51
+ super(addr)
52
+ @nettype = 'IPX/Ethernet SNAP'
53
+ end
54
+ end
55
+
@@ -0,0 +1,38 @@
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
+
6
+ class Ifconfig
7
+ #
8
+ # Can manually specify the platform (should be output of the 'uname' command)
9
+ # and the ifconfig input
10
+ #
11
+ def initialize(input=nil,verbose=nil)
12
+ if input.nil?
13
+ cmd = IO.popen('which ifconfig'){ |f| f.readlines[0] }
14
+ exit unless !cmd.nil?
15
+ @ifconfig = IO.popen("/sbin/ifconfig -a"){ |f| f.readlines.join }
16
+ else
17
+ @ifconfig = input
18
+ end
19
+ @verbose = verbose
20
+
21
+ require 'ifconfig/sunos/network_types'
22
+ require 'ifconfig/sunos/interface_types'
23
+
24
+ @ifaces = {}
25
+
26
+ split_interfaces(@ifconfig).each do |iface|
27
+ iface_name = get_iface_name(iface)
28
+ case iface
29
+ when /^lo\d\:/im
30
+ @ifaces[iface_name] = LoopbackInterface.new(iface_name,iface)
31
+ when /\s+ether\s+/im
32
+ @ifaces[iface_name] = EthernetAdapter.new(iface_name,iface)
33
+ else
34
+ puts "Unknown Adapter Type: #{iface}" if @verbose
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,77 @@
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
+ # base class for network adapters
7
+ class NetworkAdapter
8
+ # Parse activity on interface
9
+ #
10
+ def parse_activity
11
+ #imaptest1# netstat -in
12
+ #Name Mtu Net/Dest Address Ipkts Ierrs Opkts Oerrs Collis Queue
13
+ #lo0 8232 0.0.0.0 0.0.0.4 267824 0 267824 0 0 0
14
+ #bge0 1500 10.0.0.0 10.32.4.138 10935939 0 7741167 0 0 0
15
+ cmd = "netstat -in | grep #{@name}"
16
+ line = IO.popen(cmd).gets
17
+ return if line.nil?
18
+ name,@mtu,dfgw,addr,@rx['packets'],@rx['errors'],
19
+ @tx['packets'],@tx['errors'],collisions,queue = line.split
20
+ @mtu = @mtu.to_i
21
+ end
22
+
23
+ # iterate line by line and dispatch to helper functions
24
+ # for lines that match a pattern
25
+ #
26
+ def parse_ifconfig
27
+ @ifconfig.split("\n").each { |line|
28
+ case line
29
+ when /^\s+#{@protos}/
30
+ add_network(line)
31
+ when /flags\=/i
32
+ parse_flags(line)
33
+ end
34
+ }
35
+ parse_activity
36
+ end
37
+
38
+ # parses the "UP LOOPBACK RUNNING MTU:3924 Metric:1" line
39
+ #
40
+ def parse_flags(line)
41
+ flags = Regexp.compile(/\<(\S+)\>/i).match(line)[1]
42
+ flags = flags.strip.split(',')
43
+ @flags = flags
44
+ @status = true if @flags.include?('UP')
45
+ end
46
+
47
+ # Parses networks on an interface based on the first token in the line.
48
+ # eg: inet or inet6
49
+ #
50
+ def add_network(line)
51
+ case line
52
+ when /^\s+inet\s/
53
+ addr,mask = Regexp.compile(/\s+([\d\d?\d?\.]{4,})\s+netmask\s+(\S+)/i).match(line)[1..2]
54
+ bcast = Regexp.compile(/\s+broadcast\s([\d\d?\d?\.]{4,})/i).match(line)
55
+ bcast = bcast[1] unless bcast.nil?
56
+ @networks['inet'] = Ipv4Network.new(addr, mask, bcast)
57
+ when /^\s+inet6\s/
58
+ # there can be multiple inet6 entries
59
+ addr,scope =
60
+ Regexp.compile(/inet6\s+(\S+)/i).match(line)[1]
61
+ @networks["inet6:#{addr}"] = Ipv6Network.new(addr)
62
+ else
63
+ puts "unknown network type: #{line}"
64
+ end
65
+ end
66
+ end
67
+
68
+
69
+ class EthernetAdapter
70
+ def set_mac
71
+ begin
72
+ @mac = Regexp.compile(/\s+ether\s+([a-f\d]{1,2}(?:\:[a-f\d]{1,2}){5})/im).match(@ifconfig)[1]
73
+ rescue NoMethodError
74
+ puts "Couldn't Parse MAC Address for: "+@name
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,4 @@
1
+ # $Id: network_types.rb,v 1.1.1.1 2005/07/02 19:10:57 hobe Exp $
2
+ #
3
+
4
+ require 'ifconfig/common/network_types'
data/test/test_bsd.rb ADDED
@@ -0,0 +1,35 @@
1
+ #!/usr/bin/ruby -w
2
+
3
+ $: << File.dirname(__FILE__) + "/../lib"
4
+
5
+ require 'ifconfig'
6
+ require 'pp'
7
+
8
+ ifcfg = IO.readlines('../ifconfig_examples/freebsd.txt').join
9
+ netstat = IO.readlines('../ifconfig_examples/freebsd_netstat.txt').join
10
+
11
+ ifconfig = IfconfigWrapper.new('BSD',ifcfg,netstat).parse
12
+
13
+ puts "Interfaces: (ifconfig.interfaces)"
14
+ pp ifconfig.interfaces
15
+
16
+ puts "\nrl0 mac address: (ifconfig['rl0'].mac)"
17
+ pp ifconfig['rl0'].mac
18
+
19
+ puts "\nIpV4 addresses on rl0: (ifconfig['rl0'].addresses('inet'))"
20
+ pp ifconfig['rl0'].addresses('inet')
21
+
22
+ puts "\nAll addresses reported by ifconfig: (ifconfig.addresses)"
23
+ pp ifconfig.addrs_with_type
24
+
25
+ puts "\nList of address types for rl0: (ifconfig['rl0'].addr_types)"
26
+ pp ifconfig['rl0'].addr_types
27
+
28
+ puts "\niconfig.each { block }"
29
+ ifconfig.each do |iface|
30
+ pp iface.name if iface.up?
31
+ end
32
+
33
+ puts
34
+ s = ifconfig.to_s
35
+ puts s
@@ -0,0 +1,33 @@
1
+ #!/usr/bin/ruby -w
2
+
3
+ $: << File.dirname(__FILE__) + "/../lib"
4
+
5
+ require 'ifconfig'
6
+ require 'pp'
7
+
8
+ sample = IO.readlines('../ifconfig_examples/darwin.txt').join
9
+ ifconfig = IfconfigWrapper.new('BSD',sample).parse
10
+
11
+ puts "Interfaces: (ifconfig.interfaces)"
12
+ pp ifconfig.interfaces
13
+
14
+ puts "\nen0 mac address: (ifconfig['en0'].mac)"
15
+ pp ifconfig['en0'].mac
16
+
17
+ puts "\nIpV4 addresses on en0: (ifconfig['en0'].addresses('inet'))"
18
+ pp ifconfig['en0'].addresses('inet')
19
+
20
+ puts "\nAll addresses reported by ifconfig: (ifconfig.addresses)"
21
+ pp ifconfig.addrs_with_type
22
+
23
+ puts "\nList of address types for en0: (ifconfig['en0'].addr_types)"
24
+ pp ifconfig['en0'].addr_types
25
+
26
+ puts "\niconfig.each { block }"
27
+ ifconfig.each do |iface|
28
+ pp iface.name if iface.up?
29
+ end
30
+
31
+ puts
32
+ s = ifconfig.to_s
33
+ puts s
@@ -0,0 +1,35 @@
1
+ #!/usr/bin/ruby -w
2
+
3
+ $: << File.dirname(__FILE__) + "/../lib"
4
+
5
+ require 'ifconfig'
6
+ require 'pp'
7
+
8
+ ifcfg = IO.readlines('../ifconfig_examples/dragonflybsd.txt').join
9
+ netstat = IO.readlines('../ifconfig_examples/dragonflybsd_netstat.txt').join
10
+
11
+ ifconfig = IfconfigWrapper.new('BSD',ifcfg,netstat).parse
12
+
13
+ puts "Interfaces: (ifconfig.interfaces)"
14
+ pp ifconfig.interfaces
15
+
16
+ puts "\nrl0 mac address: (ifconfig['rl0'].mac)"
17
+ pp ifconfig['rl0'].mac
18
+
19
+ puts "\nIpV4 addresses on rl0: (ifconfig['rl0'].addresses('inet'))"
20
+ pp ifconfig['rl0'].addresses('inet')
21
+
22
+ puts "\nAll addresses reported by ifconfig: (ifconfig.addresses)"
23
+ pp ifconfig.addrs_with_type
24
+
25
+ puts "\nList of address types for rl0: (ifconfig['rl0'].addr_types)"
26
+ pp ifconfig['rl0'].addr_types
27
+
28
+ puts "\niconfig.each { block }"
29
+ ifconfig.each do |iface|
30
+ pp iface.name if iface.up?
31
+ end
32
+
33
+ puts
34
+ s = ifconfig.to_s
35
+ puts s
@@ -0,0 +1,4 @@
1
+ $: << File.dirname(__FILE__)+'/../lib'
2
+ require 'ifconfig'
3
+ require 'test/unit'
4
+ require 'pp'
@@ -0,0 +1,31 @@
1
+ #!/usr/bin/ruby -w
2
+
3
+ $: << File.dirname(__FILE__) + "/../lib"
4
+
5
+
6
+ require 'ifconfig'
7
+ require 'pp'
8
+
9
+ sample = IO.readlines('../ifconfig_examples/linux.txt').join
10
+ ifconfig = IfconfigWrapper.new('Linux',sample).parse
11
+
12
+ puts "Interfaces: (ifconfig.interfaces)"
13
+ pp ifconfig.interfaces
14
+
15
+ puts "\neth0 mac address: (ifconfig['eth0'].mac)"
16
+ pp ifconfig['eth0'].mac
17
+
18
+ puts "\nIpV4 addresses on eth0: (ifconfig['eth0'].addresses('inet'))"
19
+ pp ifconfig['eth0'].addresses('inet')
20
+
21
+ puts "\nAll addresses reported by ifconfig: (ifconfig.addresses)"
22
+ pp ifconfig.addrs_with_type
23
+
24
+ puts "\niconfig.each { block }"
25
+ ifconfig.each do |iface|
26
+ pp iface.name if iface.up?
27
+ end
28
+
29
+ puts
30
+ s = ifconfig.to_s
31
+ puts s
@@ -0,0 +1,33 @@
1
+ #!/usr/bin/ruby -w
2
+
3
+ $: << File.dirname(__FILE__) + "/../lib"
4
+
5
+ require 'ifconfig'
6
+ require 'pp'
7
+
8
+ sample = IO.readlines('../ifconfig_examples/netbsd.txt').join
9
+ ifconfig = IfconfigWrapper.new('BSD',sample).parse
10
+
11
+ puts "Interfaces: (ifconfig.interfaces)"
12
+ pp ifconfig.interfaces
13
+
14
+ puts "\ncs0 mac address: (ifconfig['cs0'].mac)"
15
+ pp ifconfig['cs0'].mac
16
+
17
+ puts "\nIpV4 addresses on cs0: (ifconfig['cs0'].addresses('inet'))"
18
+ pp ifconfig['cs0'].addresses('inet')
19
+
20
+ puts "\nAll addresses reported by ifconfig: (ifconfig.addresses)"
21
+ pp ifconfig.addrs_with_type
22
+
23
+ puts "\nList of address types for cs0: (ifconfig['cs0'].addr_types)"
24
+ pp ifconfig['cs0'].addr_types
25
+
26
+ puts "\niconfig.each { block }"
27
+ ifconfig.each do |iface|
28
+ pp iface.name if iface.up?
29
+ end
30
+
31
+ puts
32
+ s = ifconfig.to_s
33
+ puts s
@@ -0,0 +1,33 @@
1
+ #!/usr/bin/ruby -w
2
+
3
+ $: << File.dirname(__FILE__) + "/../lib"
4
+
5
+ require 'ifconfig'
6
+ require 'pp'
7
+
8
+ sample = IO.readlines('../ifconfig_examples/openbsd.txt').join
9
+ ifconfig = IfconfigWrapper.new('BSD',sample).parse
10
+
11
+ puts "Interfaces: (ifconfig.interfaces)"
12
+ pp ifconfig.interfaces
13
+
14
+ puts "\nxl0 mac address: (ifconfig['xl0'].mac)"
15
+ pp ifconfig['xl0'].mac
16
+
17
+ puts "\nIpV4 addresses on xl0: (ifconfig['xl0'].addresses('inet'))"
18
+ pp ifconfig['xl0'].addresses('inet')
19
+
20
+ puts "\nAll addresses reported by ifconfig: (ifconfig.addresses)"
21
+ pp ifconfig.addrs_with_type
22
+
23
+ puts "\nList of address types for xl0: (ifconfig['xl0'].addr_types)"
24
+ pp ifconfig['xl0'].addr_types
25
+
26
+ puts "\niconfig.each { block }"
27
+ ifconfig.each do |iface|
28
+ pp iface.name if iface.up?
29
+ end
30
+
31
+ puts
32
+ s = ifconfig.to_s
33
+ puts s
@@ -0,0 +1,35 @@
1
+ #!/usr/bin/ruby -w
2
+
3
+ $: << File.dirname(__FILE__) + "/../lib"
4
+
5
+ #
6
+ #imaptest1# netstat -in
7
+ #Name Mtu Net/Dest Address Ipkts Ierrs Opkts Oerrs Collis Queue
8
+ #lo0 8232 0.0.0.0 0.0.0.4 267824 0 267824 0 0 0
9
+ #bge0 1500 10.0.0.0 10.32.4.138 10935939 0 7741167 0 0 0
10
+ require 'ifconfig'
11
+ require 'pp'
12
+
13
+ sample = IO.readlines('../ifconfig_examples/sunos.txt').join
14
+ ifconfig = IfconfigWrapper.new('SunOS',sample).parse
15
+
16
+ puts "Interfaces: (ifconfig.interfaces)"
17
+ pp ifconfig.interfaces
18
+
19
+ puts "\nbge0 mac address: (ifconfig['bge0'].mac)"
20
+ pp ifconfig['bge0'].mac
21
+
22
+ puts "\nIpV4 addresses on bge0: (ifconfig['bge0'].addresses('inet'))"
23
+ pp ifconfig['bge0'].addresses('inet')
24
+
25
+ puts "\nAll addresses reported by ifconfig: (ifconfig.addresses)"
26
+ pp ifconfig.addrs_with_type
27
+
28
+ puts "\niconfig.each { block }"
29
+ ifconfig.each do |iface|
30
+ pp iface.name if iface.up?
31
+ end
32
+
33
+ puts
34
+ s = ifconfig.to_s
35
+ puts s
@@ -0,0 +1,40 @@
1
+ require File.dirname(__FILE__) + '/../test_helper'
2
+
3
+ class TC_DarwinTest < Test::Unit::TestCase
4
+ def setup
5
+ sample = IO.readlines("#{File.dirname(__FILE__)}"+
6
+ "/../../ifconfig_examples/darwin.txt").join
7
+ @cfg = IfconfigWrapper.new('BSD',sample).parse
8
+ end
9
+
10
+ def test_interface_list
11
+ assert(@cfg.interfaces.sort == ["en0", "lo0"].sort,
12
+ "Failed to parse all interfaces")
13
+ end
14
+
15
+ def test_mac_parse
16
+ assert(@cfg['en0'].mac == "00:03:93:0a:16:76",
17
+ "Failed to parse MAC address: "+@cfg['en0'].mac)
18
+ end
19
+
20
+ def test_flags
21
+ assert(@cfg['en0'].flags.include?('BROADCAST') &&
22
+ @cfg['en0'].flags.include?('RUNNING') &&
23
+ @cfg['en0'].flags.include?('MULTICAST') &&
24
+ @cfg['en0'].up?,
25
+ "FLAG Parsing failed: #{@cfg['en0'].flags}")
26
+ end
27
+
28
+ def test_addr_types
29
+ assert(@cfg['en0'].addr_types.include?('inet') &&
30
+ @cfg['en0'].addr_types.include?('inet6'),
31
+ "Failed to parse all address types")
32
+ end
33
+
34
+ def test_attribs
35
+ assert(@cfg['en0'].rx['bytes'].class == Fixnum || NilClass &&
36
+ @cfg['en0'].tx['bytes'].class == Fixnum || NilClass, "Wrong class")
37
+
38
+ end
39
+
40
+ end