aaalex-ruby-ifconfig 1.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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
data/README ADDED
@@ -0,0 +1,37 @@
1
+
2
+ This is a Ruby wrapper around the ifconfig command. The goal is to make
3
+ getting any information that ifconfig provides easy to access.
4
+
5
+ It was developed on Linux 2.6 with ifconfig from net-tools 1.60. It also has
6
+ support for SunOS, Darwin and Open, Free, Net, and DragonflyBSD. These have
7
+ not been thoroughly tested. Non Linux platforms call netstat for interface
8
+ packet information
9
+
10
+ Please send me ifconfig / netstat output for platforms that don't work.
11
+
12
+ see test/*.rb for usage examples.
13
+
14
+ Thanks to Peter Johnson for the *BSD examples
15
+ Thanks to Jeremy Tregunna for Darwin and DragonflyBSD support
16
+
17
+ INSTALLATION
18
+ ------------
19
+
20
+ Please see the INSTALL file for details of how to install Ruby-Ifconfig
21
+
22
+ TESTING
23
+ -------
24
+ > rake
25
+ to run all the unit tests.
26
+
27
+ LICENCE
28
+ -------
29
+
30
+ This software is offered under the GNU GENERAL PUBLIC LICENSE, a copy of which
31
+ is included.
32
+
33
+ Section 2, paragraph b and section 5 of the GNU General Public License does not apply when the Software (Ruby/Ifconfig) is included in the program Corgi.
34
+
35
+ --
36
+ Daniel Hobe
37
+ daniel@nightrunner.com
data/Rakefile ADDED
@@ -0,0 +1,14 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+
4
+ $VERBOSE = true
5
+
6
+ desc "Run all unit tests"
7
+ task :default => [ :test_units ]
8
+
9
+ desc "Run the unit tests in test/"
10
+ task :test_units do
11
+ Dir.glob('test/unit/*').each do |t|
12
+ puts `/usr/bin/env ruby #{t}`
13
+ end
14
+ end
data/TODO ADDED
@@ -0,0 +1,8 @@
1
+ $Id: TODO,v 1.1.1.1 2005/07/02 19:10:57 hobe Exp $
2
+
3
+ Clean up code to reduce duplicate code as much as possible.
4
+
5
+ Test on more platforms
6
+
7
+ Add concept of 'sub platform'. For example BSD->NetBSD1.4 might be different
8
+ from BSD->NetBSD1.5
@@ -0,0 +1,17 @@
1
+ lo0: flags=8049<UP,LOOPBACK,RUNNING,MULTICAST> mtu 16384
2
+ inet6 ::1 prefixlen 128
3
+ inet6 fe80::1 prefixlen 64 scopeid 0x1
4
+ inet 127.0.0.1 netmask 0xff000000
5
+ gif0: flags=8010<POINTOPOINT,MULTICAST> mtu 1280
6
+ stf0: flags=0<> mtu 1280
7
+ en0: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
8
+ inet6 fe80::203:93ff:fe0a:1676 prefixlen 64 scopeid 0x4
9
+ inet 192.168.0.14 netmask 0xffffff00 broadcast 192.168.0.255
10
+ inet6 2001:5c0:8116::203:93ff:fe0a:1676 prefixlen 64 autoconf
11
+ ether 00:03:93:0a:16:76
12
+ media: autoselect (100baseTX <full-duplex>) status: active
13
+ supported media: none autoselect 10baseT/UTP <half-duplex> 10baseT/UTP <full-duplex> 10baseT/UTP <full-duplex,hw-loopback> 100baseTX <half-duplex> 100baseTX <full-duplex> 100baseTX <full-duplex,hw-loopback>
14
+ fw0: flags=8822<BROADCAST,SMART,SIMPLEX,MULTICAST> mtu 2030
15
+ lladdr 00:03:93:ff:fe:0a:16:76
16
+ media: autoselect <full-duplex> status: inactive
17
+ supported media: autoselect <full-duplex>
@@ -0,0 +1,10 @@
1
+ rl0: flags=8843<UP,BROADCAST,RUNNING,SIMPLEX,MULTICAST> mtu 1500
2
+ inet6 fe80::202:44ff:fe8f:bb15%rl0 prefixlen 64 scopeid 0x2
3
+ inet 192.168.1.24 netmask 0xffffff00 broadcast 192.168.1.255
4
+ ether 00:02:44:8f:bb:15
5
+ media: Ethernet autoselect (100baseTX <full-duplex>)
6
+ status: active
7
+ lo0: flags=8049<UP,LOOPBACK,RUNNING,MULTICAST> mtu 16384
8
+ inet6 ::1 prefixlen 128
9
+ inet6 fe80::1%lo0 prefixlen 64 scopeid 0x4
10
+ inet 127.0.0.1 netmask 0xff000000
@@ -0,0 +1,14 @@
1
+ ppp0: flags=8010<POINTOPOINT,MULTICAST> mtu 1500
2
+ sl0: flags=c010<POINTOPOINT,LINK2,MULTICAST> mtu 552
3
+ faith0: flags=8002<BROADCAST,MULTICAST> mtu 1500
4
+ Name Mtu Network Address Ipkts Ierrs Opkts Oerrs Coll
5
+ rl0 1500 <Link#1> 00:02:44:8f:bb:15 606 0 549 0 0
6
+ rl0 1500 fe80:2::202 fe80:2::202:44ff: 0 - 0 - -
7
+ rl0 1500 192.168.1 192.168.1.24 605 - 544 - -
8
+ lo0 16384 <Link#2> 0 0 0 0 0
9
+ lo0 16384 ::1/128 ::1 0 - 0 - -
10
+ lo0 16384 fe80:4::1/6 fe80:4::1 0 - 0 - -
11
+ lo0 16384 127 127.0.0.1 0 - 0 - -
12
+ ppp0* 1500 <Link#3> 0 0 0 0 0
13
+ sl0* 552 <Link#4> 0 0 0 0 0
14
+ faith0* 1500 <Link#5> 0 0 0 0 0
@@ -0,0 +1,17 @@
1
+ xl0: flags=8843<UP,BROADCAST,RUNNING,SIMPLEX,MULTICAST> mtu 1500
2
+ options=3<rxcsum,txcsum>
3
+ inet 192.168.0.2 netmask 0xffffff00 broadcast 192.168.0.255
4
+ inet6 fe80::201:2ff:fec6:4bea%xl0 prefixlen 64 scopeid 0x1
5
+ ether 00:01:02:c6:4b:ea
6
+ media: Ethernet autoselect (100baseTX <full-duplex>)
7
+ status: active
8
+ rl0: flags=8943<UP,BROADCAST,RUNNING,PROMISC,SIMPLEX,MULTICAST> mtu 1500
9
+ inet 10.0.0.10 netmask 0xffffff00 broadcast 10.0.0.255
10
+ inet6 fe80::200:21ff:fe03:8e1%rl0 prefixlen 64 scopeid 0x1
11
+ ether 00:00:21:03:08:e1
12
+ media: Ethernet autoselect (100baseTX )
13
+ status: active
14
+ lo0: flags=8049<UP,LOOPBACK,RUNNING,MULTICAST> mtu 16384
15
+ inet6 ::1 prefixlen 128
16
+ inet6 fe80::1%lo0 prefixlen 64 scopeid 0x2
17
+ inet 127.0.0.1 netmask 0xff000000
@@ -0,0 +1,24 @@
1
+ Name Mtu Network Address Ipkts Ierrs Ibytes Opkts Oerrs Obytes Coll Time Drop
2
+ xl0 1500 <Link#1> 00:01:02:c6:4b:ea 7878752 2 404325088 5502452 0 1276760877 0 0 0
3
+ 33:33:46:5d:24:5a
4
+ 33:33:00:00:00:01
5
+ 33:33:ff:c6:4b:ea
6
+ 01:00:5e:00:00:01
7
+ xl0 1500 192.168.0 beleriand 7865045 - 287935758 5496244 - 1199565347 - - -
8
+ ALL-SYSTEMS.MCAST.NET
9
+ xl0 1500 fe80:1::201 fe80:1::201:2ff:f 0 - 0 0 - 0 - - -
10
+ ff02:1::2:465d:245a(refs: 1)
11
+ ff02:1::1 (refs: 1)
12
+ ff02:1::1:ffc6:4bea(refs: 1)
13
+ lo0 16384 <Link#2> 574 0 118916 574 0 118916 0 0 0
14
+ lo0 16384 localhost ::1 0 - 0 0 - 0 - - -
15
+ ff01::1 (refs: 1)
16
+ ff02:2::1 (refs: 1)
17
+ ff02:2::1:ff00:1 (refs: 2)
18
+ lo0 16384 fe80:2::1 fe80:2::1 0 - 0 0 - 0 - - -
19
+ ff01::1 (refs: 1)
20
+ ff02:2::1 (refs: 1)
21
+ ff02:2::1:ff00:1 (refs: 2)
22
+ lo0 16384 your-net localhost 574 - 118916 574 - 118916 - - -
23
+ ALL-SYSTEMS.MCAST.NET
24
+
@@ -0,0 +1,60 @@
1
+ eth0 Link encap:Ethernet HWaddr 00:50:DA:C1:C3:45
2
+ inet addr:216.87.223.240 Bcast:216.87.223.255 Mask:255.255.255.0
3
+ inet6 addr: fe80::250:daff:fec1:c345/10 Scope:Link
4
+ inet6 addr: 3ffe:b00:c18:1fff::2bd/0 Scope:Global
5
+ IPX/Ethernet II addr:CA008137:00204808C79B
6
+ IPX/Ethernet SNAP addr:00204808C79B
7
+ IPX/Ethernet 802.2 addr:00204808C79B
8
+ IPX/Ethernet 802.3 addr:00204808C79B
9
+ EtherTalk Phase 2 addr:7349/195
10
+ UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
11
+ RX packets:90170 errors:0 dropped:0 overruns:0 frame:0
12
+ TX packets:2854 errors:0 dropped:0 overruns:0 carrier:0
13
+ collisions:0 txqueuelen:100
14
+ RX bytes:1037052545 (989.0 MiB) TX bytes:32859376 (31.3 MiB)
15
+ Interrupt:10 Base address:0xe400
16
+
17
+ eth0:0 Link encap:Ethernet HWaddr 00:50:DA:C1:C3:45
18
+ inet addr:172.16.0.93 Bcast:172.16.0.255 Mask:255.255.255.0
19
+ UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
20
+ Interrupt:10 Base address:0xe400
21
+
22
+ ppp0 Link encap:Point-to-Point Protocol
23
+ inet addr:213.8.120.98 P-t-P:213.8.120.1 Mask:255.255.255.255
24
+ UP POINTOPOINT RUNNING NOARP MULTICAST MTU:1452 Metric:1
25
+ RX packets:49753 errors:0 dropped:0 overruns:0 frame:0
26
+ TX packets:26973 errors:0 dropped:0 overruns:0 carrier:0
27
+ collisions:0 txqueuelen:10
28
+
29
+ sl0 Link encap:Serial Line IP
30
+ inet addr:192.168.1.1 P-t-P:192.168.1.2 Mask:255.255.255.0
31
+ UP POINTOPOINT RUNNING MTU:1500 Metric:1
32
+ RX packets:0 errors:0 dropped:0 overruns:0
33
+ TX packets:40 errors:0 dropped:0 overruns:0
34
+
35
+ lo Link encap:Local Loopback
36
+ inet addr:127.0.0.1 Mask:255.0.0.0
37
+ inet6 addr: ::1/128 Scope:Host
38
+ EtherTalk Phase 2 addr:0/0
39
+ UP LOOPBACK RUNNING MTU:3924 Metric:1
40
+ RX packets:43 errors:0 dropped:0 overruns:0 frame:0
41
+ TX packets:43 errors:0 dropped:0 overruns:0 carrier:0
42
+ collisions:0 txqueuelen:0
43
+ RX bytes:14564572 (13.8 MiB) TX bytes:14564572 (13.8 MiB)
44
+
45
+ sit0 Link encap:IPv6-in-IPv4
46
+ inet6 addr: ::127.0.0.1/96 Scope:Unknown
47
+ inet6 addr: ::216.87.223.240/96 Scope:Compat
48
+ inet6 addr: ::172.16.0.93/96 Scope:Compat
49
+ UP RUNNING NOARP MTU:1480 Metric:1
50
+ RX packets:0 errors:0 dropped:0 overruns:0 frame:0
51
+ TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
52
+ collisions:0 txqueuelen:0
53
+
54
+ sit1 Link encap:IPv6-in-IPv4
55
+ inet6 addr: fe80::d857:dff0/10 Scope:Link
56
+ inet6 addr: fe80::ac10:5d/10 Scope:Link
57
+ UP POINTOPOINT RUNNING NOARP MTU:1480 Metric:1
58
+ RX packets:0 errors:0 dropped:0 overruns:0 frame:0
59
+ TX packets:25 errors:0 dropped:0 overruns:0 carrier:0
60
+ collisions:0 txqueuelen:0
@@ -0,0 +1,20 @@
1
+ eth0 Link encap:Ethernet HWaddr 00:50:DA:C1:C3:45
2
+ inet addr:216.87.223.240 Bcast:216.87.223.255 Mask:255.255.255.0
3
+ inet6 addr: fe80::250:daff:fec1:c345/10 Scope:Link
4
+ inet6 addr: 3ffe:b00:c18:1fff::2bd/0 Scope:Global
5
+ IPX/Ethernet II addr:CA008137:00204808C79B
6
+ IPX/Ethernet SNAP addr:00204808C79B
7
+ IPX/Ethernet 802.2 addr:00204808C79B
8
+ IPX/Ethernet 802.3 addr:00204808C79B
9
+ EtherTalk Phase 2 addr:7349/195
10
+ UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
11
+ RX packets:90170 errors:0 dropped:0 overruns:0 frame:0
12
+ TX packets:2854 errors:0 dropped:0 overruns:0 carrier:0
13
+ collisions:0 txqueuelen:100
14
+ RX bytes:1037052545 (989.0 MiB) TX bytes:32859376 (31.3 MiB)
15
+ Interrupt:10 Base address:0xe400
16
+
17
+ eth0:0 Link encap:Ethernet HWaddr 00:50:DA:C1:C3:45
18
+ inet addr:172.16.0.93 Bcast:172.16.0.255 Mask:255.255.255.0
19
+ UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
20
+ Interrupt:10 Base address:0xe400
@@ -0,0 +1,10 @@
1
+ cs0: flags=8863<UP,BROADCAST,NOTRAILERS,RUNNING,SIMPLEX,MULTICAST> mtu 1500
2
+ address: 08:00:2b:81:62:ca
3
+ media: Ethernet 10baseT
4
+ status: active
5
+ inet 192.168.0.3 netmask 0xffffff00 broadcast 192.168.0.255
6
+ inet6 fe80::a00:2bff:fe81:62ca%cs0 prefixlen 64 scopeid 0x1
7
+ lo0: flags=8009<UP,LOOPBACK,MULTICAST> mtu 33220
8
+ inet 127.0.0.1 netmask 0xff000000
9
+ inet6 fe80::1%lo0 prefixlen 64 scopeid 0x2
10
+ inet6 ::1 prefixlen 128
@@ -0,0 +1,36 @@
1
+ lo0: flags=8049<UP,LOOPBACK,RUNNING,MULTICAST> mtu 33224
2
+ inet 127.0.0.1 netmask 0xff000000
3
+ inet6 ::1 prefixlen 128
4
+ inet6 fe80::1%lo0 prefixlen 64 scopeid 0x6
5
+ lo1: flags=8008<LOOPBACK,MULTICAST> mtu 33224
6
+ xl0: flags=8843<UP,BROADCAST,RUNNING,SIMPLEX,MULTICAST> mtu 1500
7
+ address: 00:01:02:c6:4b:3d
8
+ media: Ethernet autoselect (100baseTX full-duplex)
9
+ status: active
10
+ inet 192.168.0.1 netmask 0xffffff00 broadcast 192.168.0.255
11
+ inet6 fe80::201:2ff:fec6:4b3d%xl0 prefixlen 64 scopeid 0x1
12
+ ep0: flags=8863<UP,BROADCAST,NOTRAILERS,RUNNING,SIMPLEX,MULTICAST> mtu 1500
13
+ address: 00:20:af:31:99:43
14
+ media: Ethernet 10baseT
15
+ inet6 fe80::220:afff:fe31:9943%ep0 prefixlen 64 scopeid 0x2
16
+ inet 24.6.50.88 netmask 0xfffff800 broadcast 255.255.255.255
17
+ pflog0: flags=41<UP,RUNNING> mtu 33224
18
+ pfsync0: flags=0<> mtu 1896
19
+ sl0: flags=c010<POINTOPOINT,LINK2,MULTICAST> mtu 296
20
+ sl1: flags=c010<POINTOPOINT,LINK2,MULTICAST> mtu 296
21
+ ppp0: flags=8010<POINTOPOINT,MULTICAST> mtu 1500
22
+ ppp1: flags=8010<POINTOPOINT,MULTICAST> mtu 1500
23
+ tun0: flags=10<POINTOPOINT> mtu 3000
24
+ tun1: flags=10<POINTOPOINT> mtu 3000
25
+ enc0: flags=0<> mtu 1536
26
+ bridge0: flags=0<> mtu 1500
27
+ bridge1: flags=0<> mtu 1500
28
+ vlan0: flags=0<> mtu 1500
29
+ address: 00:00:00:00:00:00
30
+ vlan1: flags=0<> mtu 1500
31
+ address: 00:00:00:00:00:00
32
+ gre0: flags=9010<POINTOPOINT,LINK0,MULTICAST> mtu 1450
33
+ gif0: flags=8010<POINTOPOINT,MULTICAST> mtu 1280
34
+ gif1: flags=8010<POINTOPOINT,MULTICAST> mtu 1280
35
+ gif2: flags=8010<POINTOPOINT,MULTICAST> mtu 1280
36
+ gif3: flags=8010<POINTOPOINT,MULTICAST> mtu 1280
@@ -0,0 +1,10 @@
1
+ lo0: flags=1000849<UP,LOOPBACK,RUNNING,MULTICAST,IPv4> mtu 8232 index 1
2
+ inet 127.0.0.1 netmask ff000000
3
+ inet6 ::1/128
4
+ bge0: flags=1000843<UP,BROADCAST,RUNNING,MULTICAST,IPv4> mtu 1500 index 2
5
+ inet 10.32.4.138 netmask ffffff00 broadcast 10.32.4.255
6
+ ether 0:3:ba:42:9d:ef
7
+ inet6 fe80::a00:20ff:fe72:9724/10
8
+ le1: flags=1000843<UP,BROADCAST,RUNNING,MULTICAST,IPv4> mtu 1500 index 3
9
+ inet 172.16.254.99 netmask ffff0000 broadcast 172.16.255.255
10
+ ether 8:0:20:1d:71:eb
data/lib/ifconfig.rb ADDED
@@ -0,0 +1,71 @@
1
+ # $Revision: 1.1.1.1 $
2
+ # $Id: ifconfig.rb,v 1.1.1.1 2005/07/02 19:10:57 hobe Exp $
3
+
4
+ # = Ruby/Ifconfig - ruby interface to the information presented by ifconfig
5
+ #
6
+ # == Prerequisites
7
+ #
8
+ # You must have a working ifconfig binary in your path.
9
+ # The output format of you particular version of ifconfig must be supported.
10
+ # Currently this has only been tested with net-tool 1.6 on Linux
11
+ #
12
+ # == Example
13
+ # require 'ifconfig'
14
+ #
15
+ # cfg = IfconfigWrapper.new.parse # parses the output of ifconfig
16
+ #
17
+ # cfg.interfaces() # returns list of interfaces
18
+ #
19
+ # cfg[eth0].addresses('inet') # returns ipv4 address of eth0
20
+ #
21
+ # cfg.addrresses('inet') # returns list of all ipv4 addresses
22
+ #
23
+ # cfg[eth0].status # returns true if interface is up
24
+ #
25
+ # cfg.each { block } # run a block on each interface object
26
+
27
+
28
+ #
29
+ # inspired by Net::Ifconfig::Wrapper perl module
30
+ # by Daniel Podolsky, <tpaba@cpan.org>
31
+ #
32
+ # Ruby version by Daniel Hobe <daniel@packetspike.net>
33
+ # Licence:: GPL[http://www.gnu.org/copyleft/gpl.html]
34
+ #
35
+
36
+ require 'ipaddr'
37
+
38
+ class IfconfigWrapper
39
+ NAME = 'Ruby/Ifconfig'
40
+ include Enumerable
41
+
42
+ #
43
+ # Can manually specify the platform (should be output of the 'uname' command)
44
+ # and the ifconfig input
45
+ #
46
+ def initialize(platform=nil,input=nil,netstat=nil)
47
+ platform = self.get_os unless !platform.nil?
48
+ require "ifconfig/"+platform.downcase+"/ifconfig"
49
+ @cfg = Ifconfig.new(input,netstat)
50
+ end
51
+ def parse
52
+ return @cfg
53
+ end
54
+ # Find out what os we are on
55
+ #
56
+ def get_os
57
+ plat = IO.popen('uname'){ |f| f.gets.strip }
58
+ case plat
59
+ when 'Windows'
60
+ return plat
61
+ when 'Linux'
62
+ return plat
63
+ when 'SunOS'
64
+ return plat
65
+ when 'FreeBSD','OpenBSD','NetBSD','DragonFly','Darwin'
66
+ return 'BSD'
67
+ else
68
+ return 'Linux'
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,72 @@
1
+ # $Id: ifconfig.rb,v 1.1.1.1 2005/07/02 19:10:58 hobe Exp $
2
+ #
3
+ require 'ifconfig/common/ifconfig'
4
+ require 'ifconfig/bsd/network_types'
5
+ require 'ifconfig/bsd/interface_types'
6
+
7
+ class Ifconfig
8
+ #
9
+ # ifconfig = user provided ifconifg output
10
+ # netstat = same, but for netstat -in
11
+ #
12
+ @@ifcfg_cmd = "/usr/bin/env ifconfig -a"
13
+ @@netstat_cmd = "/usr/bin/env netstat -in"
14
+ def initialize(ifconfig=nil,netstat=nil,verbose=nil)
15
+ @ifconfig = ifconfig
16
+ @ifconfig ||= IO.popen(@@ifcfg_cmd){ |f| f.readlines.join }
17
+
18
+ @netstat = netstat
19
+ @netstat ||= IO.popen(@@netstat_cmd){ |f| f.readlines.join }
20
+
21
+ @verbose = verbose
22
+
23
+ @ifaces = {}
24
+ split_interfaces(@ifconfig).each do |iface|
25
+ iface_name = get_iface_name(iface)
26
+ case iface
27
+ when /^lo\d\:/im
28
+ @ifaces[iface_name] = LoopbackInterface.new(iface_name,iface)
29
+ parse_activity(iface_name)
30
+ when /\s+media\:\s+Ethernet\s+/im
31
+ @ifaces[iface_name] = EthernetAdapter.new(iface_name,iface)
32
+ parse_activity(iface_name)
33
+ when /\s+supported\smedia\:\s+none\s+autoselect\s+/im
34
+ # This clause is only matched on Darwin. This pattern will only be
35
+ # matched on ethernet devices (won't match on fw0 or any other
36
+ # interface I can see).
37
+ @ifaces[iface_name] = EthernetAdapter.new(iface_name,iface)
38
+ parse_activity(iface_name)
39
+ else
40
+ puts "Unknown Adapter Type: #{iface}" if @verbose
41
+ end
42
+ end
43
+ end
44
+
45
+ # Parse activity on interface
46
+ #
47
+ # Not doing it in each interface class so we can pass in
48
+ # fake input
49
+ #
50
+ def parse_activity(iface)
51
+ mtu = rxpackets = rxerrors = txpackets = txerrors = 0
52
+ @netstat.split("\n").each { |line|
53
+ line.strip!
54
+ if line =~ /^#{iface}/
55
+ next if line.split[2] =~ /\<Link\#\d\>/
56
+ puts "matched line for "+iface
57
+ toks = line.split
58
+ mtu = toks[1]
59
+ rxpackets += toks[4].to_i
60
+ rxerrors += toks[5].to_i
61
+ txpackets += toks[6].to_i
62
+ txerrors += toks[7].to_i
63
+ @ifaces[iface].mtu = mtu.to_i
64
+ @ifaces[iface].rx = { 'packets' => rxpackets,
65
+ 'errors' => rxerrors }
66
+ @ifaces[iface].tx = { 'packets' => txpackets,
67
+ 'errors' => txerrors }
68
+ end
69
+ }
70
+ end
71
+
72
+ end