Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions Changelog
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
Version 1.3.0
- import into asomers/ruby-ifconfig on Github
- bug fixes, mostly in the tests
- added support for fibs, media type, and lagg devices on FreeBSD

Version 1.2.4
- import into langhorst/ruby-ifconfig on Github
- bug fixes, mostly in the tests

Version 1.2.3
- added addrs_with_mask method in NetworkAdapter (Alex Peuchert)

Expand Down
11 changes: 6 additions & 5 deletions README
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
This is a Ruby wrapper around the ifconfig command. The goal is to make
getting any information that ifconfig provides easy to access.

It was developed on Linux 2.6 with ifconfig from net-tools 1.60. It also has
support for SunOS, Darwin and Open, Free, Net, and DragonflyBSD. These have
not been thoroughly tested. Non Linux platforms call netstat for interface
packet information
It was developed on Linux 2.6 with ifconfig from net-tools 1.60. The FreeBSD
support has been tested on FreBSD 9.2 amd64. The package also has support for
SunOS, Darwin, OSX and Open, Net, and DragonflyBSD. These have not been
thoroughly tested. Non Linux platforms call netstat for interface packet
information.

Please send me ifconfig / netstat output for platforms that don't work.

Expand All @@ -29,7 +30,7 @@ Prerequisites

You must have a working ifconfig binary in your path.
The output format of you particular version of ifconfig must be supported.
Currently this has only been tested with net-tool 1.6 on Linux
Currently this has only been tested with FreeBSD 9.2 and net-tool 1.6 on Linux.

Example
------
Expand Down
29 changes: 22 additions & 7 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,33 @@ require 'rake/testtask'

$VERBOSE = true

desc "Run all unit tests"
desc "Default Task"
task :default => [ :test ]

desc "Run all unit tests together"
desc "Run all unit tests."
task :test => ["test:bsd", "test:linux", "test:sunos"]

# The different os-specific files cannot be required in the same process, since
# the redefine the same symbols. So we can't test them in a single test/unit
# process. Instead, we'll split them up into the minimum possible number of
# processes, each with its own rake task.
Rake::TestTask.new do |t|
t.test_files = FileList['test/unit/tc_*.rb']
t.name = "test:bsd"
t.test_files = %w(darwin dragonflybsd freebsd netbsd openbsd osx).collect do |os|
"test/unit/tc_#{os}.rb"
end
end

desc "Run the unit tests in test/"
task :test_units do
Dir.glob('test/unit/*').each do |t|
puts `/usr/bin/env ruby #{t}`
Rake::TestTask.new do |t|
t.name = "test:linux"
t.test_files = %w(linux).collect do |os|
"test/unit/tc_#{os}.rb"
end
end

Rake::TestTask.new do |t|
t.name = "test:sunos"
t.test_files = %w(sunos).collect do |os|
"test/unit/tc_#{os}.rb"
end
end
1 change: 1 addition & 0 deletions ifconfig_examples/dragonflybsd.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
rl0: flags=8843<UP,BROADCAST,RUNNING,SIMPLEX,MULTICAST> mtu 1500
options=1b<RXCSUM,TXCSUM,VLAN_MTU,VLAN_HWTAGGING>
inet6 fe80::202:44ff:fe8f:bb15%rl0 prefixlen 64 scopeid 0x2
inet 192.168.1.24 netmask 0xffffff00 broadcast 192.168.1.255
ether 00:02:44:8f:bb:15
Expand Down
67 changes: 51 additions & 16 deletions ifconfig_examples/freebsd.txt
Original file line number Diff line number Diff line change
@@ -1,17 +1,52 @@
xl0: flags=8843<UP,BROADCAST,RUNNING,SIMPLEX,MULTICAST> mtu 1500
options=3<rxcsum,txcsum>
inet 192.168.0.2 netmask 0xffffff00 broadcast 192.168.0.255
inet6 fe80::201:2ff:fec6:4bea%xl0 prefixlen 64 scopeid 0x1
ether 00:01:02:c6:4b:ea
media: Ethernet autoselect (100baseTX <full-duplex>)
igb0: flags=8843<UP,BROADCAST,RUNNING,SIMPLEX,MULTICAST> metric 0 mtu 1500
options=401bb<RXCSUM,TXCSUM,VLAN_MTU,VLAN_HWTAGGING,JUMBO_MTU,VLAN_HWCSUM,TSO4,VLAN_HWTSO>
ether 00:11:22:33:44:55
inet6 fe80::225:90ff:fe95:c98c%igb0 prefixlen 64 scopeid 0x1
inet 10.1.3.176 netmask 0xfffff000 broadcast 10.1.15.255
nd6 options=29<PERFORMNUD,IFDISABLED,AUTO_LINKLOCAL>
media: Ethernet autoselect (1000baseT <full-duplex>)
status: active
igb1: flags=8843<UP,BROADCAST,RUNNING,SIMPLEX,MULTICAST> metric 0 mtu 1500
options=401bb<RXCSUM,TXCSUM,VLAN_MTU,VLAN_HWTAGGING,JUMBO_MTU,VLAN_HWCSUM,TSO4,VLAN_HWTSO>
ether 00:11:22:33:44:66
nd6 options=29<PERFORMNUD,IFDISABLED,AUTO_LINKLOCAL>
media: Ethernet autoselect (1000baseT <full-duplex>)
status: active
fib: 1
igb2: flags=8843<UP,BROADCAST,RUNNING,SIMPLEX,MULTICAST> metric 0 mtu 1500
options=401bb<RXCSUM,TXCSUM,VLAN_MTU,VLAN_HWTAGGING,JUMBO_MTU,VLAN_HWCSUM,TSO4,VLAN_HWTSO>
ether 00:11:22:33:44:66
nd6 options=29<PERFORMNUD,IFDISABLED,AUTO_LINKLOCAL>
media: Ethernet autoselect (1000baseT <full-duplex>)
status: active
igb3: flags=8843<UP,BROADCAST,RUNNING,SIMPLEX,MULTICAST> metric 0 mtu 1500
options=401bb<RXCSUM,TXCSUM,VLAN_MTU,VLAN_HWTAGGING,JUMBO_MTU,VLAN_HWCSUM,TSO4,VLAN_HWTSO>
ether 00:11:22:33:44:66
nd6 options=29<PERFORMNUD,IFDISABLED,AUTO_LINKLOCAL>
media: Ethernet autoselect (1000baseT <full-duplex>)
status: active
cxgbe0: flags=8843<UP,BROADCAST,RUNNING,SIMPLEX,MULTICAST> metric 0 mtu 1500
options=6c07bb<RXCSUM,TXCSUM,VLAN_MTU,VLAN_HWTAGGING,JUMBO_MTU,VLAN_HWCSUM,TSO4,TSO6,LRO,VLAN_HWTSO,LINKSTATE,RXCSUM_IPV6,TXCSUM_IPV6>
ether 22:33:44:55:66:77
inet 10.1.0.11 netmask 0xfffff000 broadcast 10.1.15.255
nd6 options=29<PERFORMNUD,IFDISABLED,AUTO_LINKLOCAL>
media: Ethernet 10Gbase-Twinax <full-duplex>
status: active
rl0: flags=8943<UP,BROADCAST,RUNNING,PROMISC,SIMPLEX,MULTICAST> mtu 1500
inet 10.0.0.10 netmask 0xffffff00 broadcast 10.0.0.255
inet6 fe80::200:21ff:fe03:8e1%rl0 prefixlen 64 scopeid 0x1
ether 00:00:21:03:08:e1
media: Ethernet autoselect (100baseTX )
status: active
lo0: flags=8049<UP,LOOPBACK,RUNNING,MULTICAST> mtu 16384
inet6 ::1 prefixlen 128
inet6 fe80::1%lo0 prefixlen 64 scopeid 0x2
inet 127.0.0.1 netmask 0xff000000
lo0: flags=8049<UP,LOOPBACK,RUNNING,MULTICAST> metric 0 mtu 16384
options=600003<RXCSUM,TXCSUM,RXCSUM_IPV6,TXCSUM_IPV6>
inet6 ::1 prefixlen 128
inet6 fe80::1%lo0 prefixlen 64 scopeid 0x7
inet 127.0.0.1 netmask 0xff000000
nd6 options=21<PERFORMNUD,AUTO_LINKLOCAL>
lagg0: flags=8843<UP,BROADCAST,RUNNING,SIMPLEX,MULTICAST> metric 0 mtu 1500
options=401bb<RXCSUM,TXCSUM,VLAN_MTU,VLAN_HWTAGGING,JUMBO_MTU,VLAN_HWCSUM,TSO4,VLAN_HWTSO>
ether 00:11:22:33:44:66
inet 10.1.19.176 netmask 0xfffff000 broadcast 10.1.31.255
nd6 options=29<PERFORMNUD,IFDISABLED,AUTO_LINKLOCAL>
media: Ethernet autoselect
status: active
fib: 1
laggproto lacp lagghash l2,l3,l4
laggport: igb3 flags=1c<ACTIVE,COLLECTING,DISTRIBUTING>
laggport: igb2 flags=1c<ACTIVE,COLLECTING,DISTRIBUTING>
laggport: igb1 flags=1c<ACTIVE,COLLECTING,DISTRIBUTING>
39 changes: 15 additions & 24 deletions ifconfig_examples/freebsd_netstat.txt
Original file line number Diff line number Diff line change
@@ -1,24 +1,15 @@
Name Mtu Network Address Ipkts Ierrs Ibytes Opkts Oerrs Obytes Coll Time Drop
xl0 1500 <Link#1> 00:01:02:c6:4b:ea 7878752 2 404325088 5502452 0 1276760877 0 0 0
33:33:46:5d:24:5a
33:33:00:00:00:01
33:33:ff:c6:4b:ea
01:00:5e:00:00:01
xl0 1500 192.168.0 beleriand 7865045 - 287935758 5496244 - 1199565347 - - -
ALL-SYSTEMS.MCAST.NET
xl0 1500 fe80:1::201 fe80:1::201:2ff:f 0 - 0 0 - 0 - - -
ff02:1::2:465d:245a(refs: 1)
ff02:1::1 (refs: 1)
ff02:1::1:ffc6:4bea(refs: 1)
lo0 16384 <Link#2> 574 0 118916 574 0 118916 0 0 0
lo0 16384 localhost ::1 0 - 0 0 - 0 - - -
ff01::1 (refs: 1)
ff02:2::1 (refs: 1)
ff02:2::1:ff00:1 (refs: 2)
lo0 16384 fe80:2::1 fe80:2::1 0 - 0 0 - 0 - - -
ff01::1 (refs: 1)
ff02:2::1 (refs: 1)
ff02:2::1:ff00:1 (refs: 2)
lo0 16384 your-net localhost 574 - 118916 574 - 118916 - - -
ALL-SYSTEMS.MCAST.NET

Name Mtu Network Address Ipkts Ierrs Idrop Opkts Oerrs Coll
igb0 1500 <Link#1> 00:11:22:33:44:55 5748766 0 0 1591506 0 0
igb0 1500 fe80::225:90f fe80::225:90ff:fe 0 - - 2 - -
igb0 1500 10.1.0.0/20 10.1.3.176 1683211 - - 1480094 - -
igb1 1500 <Link#2> 00:11:22:33:44:66 6369236 0 0 36130 0 0
igb2 1500 <Link#3> 00:11:22:33:44:66 57591 0 0 35355 0 0
igb3 1500 <Link#4> 00:11:22:33:44:66 53758 0 0 37262 0 0
cxgbe0 1500 <Link#4> 22:33:44:55:66:77 1234567 0 0 2345678 0 0
cxgbe0 1500 10.1.0.0/20 10.1.0.11 1234568 - - 2345679 - -
lo0 16384 <Link#7> 7486204 0 0 7486201 0 0
lo0 16384 ::1/128 ::1 6628 - - 6628 - -
lo0 16384 fe80::1%lo0/6 fe80::1 0 - - 0 - -
lo0 16384 127.0.0.0/8 127.0.0.1 7479584 - - 7479582 - -
lagg0 1500 <Link#8> 00:11:22:33:44:66 6294267 0 0 11768 2 0
lagg0 1500 10.1.16.0/20 10.1.19.176 334619 - - 9436 - -
2 changes: 2 additions & 0 deletions ifconfig_examples/netbsd.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
cs0: flags=8863<UP,BROADCAST,NOTRAILERS,RUNNING,SIMPLEX,MULTICAST> mtu 1500
capabilities=2bf80<TSO4,IP4CSUM_Rx,IP4CSUM_Tx,TCP4CSUM_Rx,TCP4CSUM_Tx,UDP4CSUM_Rx,UDP4CSUM_Tx,TCP6CSUM_Tx,UDP6CSUM_Tx>
enabled=3000<UDP4CSUM_Rx,UDP4CSUM_Tx>
address: 08:00:2b:81:62:ca
media: Ethernet 10baseT
status: active
Expand Down
8 changes: 8 additions & 0 deletions ifconfig_examples/netbsd_netstat.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Name Mtu Network Address Ipkts Ierrs Opkts Oerrs Colls
cs0 1500 <Link> 08:00:2b:81:62:ca 42320 0 131 0 0
cs0 1500 fe80::/64 fe80::a00:2bff:fe 42320 0 131 0 0
cs0 1500 192.168.0/24 192.168.6.156 42320 0 131 0 0
lo0 33648 <Link> 0 0 0 0 0
lo0 33648 127/8 127.0.0.1 0 0 0 0 0
lo0 33648 ::1/128 ::1 0 0 0 0 0
lo0 33648 fe80::/64 fe80::1 0 0 0 0 0
6 changes: 4 additions & 2 deletions ifconfig_examples/sunos.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
lo0: flags=1000849<UP,LOOPBACK,RUNNING,MULTICAST,IPv4> mtu 8232 index 1
inet 127.0.0.1 netmask ff000000
inet6 ::1/128
bge0: flags=1000843<UP,BROADCAST,RUNNING,MULTICAST,IPv4> mtu 1500 index 2
bge0: flags=1000843<UP,BROADCAST,RUNNING,MULTICAST,ROUTER,IPv4> mtu 1500 index 2
inet 10.32.4.138 netmask ffffff00 broadcast 10.32.4.255
ether 0:3:ba:42:9d:ef
inet6 fe80::a00:20ff:fe72:9724/10
le1: flags=1000843<UP,BROADCAST,RUNNING,MULTICAST,IPv4> mtu 1500 index 3
bge0:1: flags=1000843<UP,BROADCAST,RUNNING,MULTICAST,ROUTER,IPv4> mtu 1500 index 2
inet 10.32.4.139 netmask ffffff00 broadcast 10.32.4.255
le1: flags=1000843<UP,BROADCAST,RUNNING,MULTICAST,ROUTER,IPv4> mtu 1500 index 3
inet 172.16.254.99 netmask ffff0000 broadcast 172.16.255.255
ether 8:0:20:1d:71:eb
8 changes: 8 additions & 0 deletions ifconfig_examples/sunos_netstat.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Name Mtu Net/Dest Address Ipkts Ierrs Opkts Oerrs Collis Queue
lo0 8232 127.0.0.0 127.0.0.1 162 0 162 0 0 0
bge0 1500 10.32.4.0 10.32.4.137 3460 0 114 0 0 0
le1 1500 172.16.0.0 172.16.254.99 1000 0 500 0 0 0

Name Mtu Net/Dest Address Ipkts Ierrs Opkts Oerrs Collis
lo0 8252 ::1 ::1 162 0 162 0 0
bge0 1500 fe80:: fe80::a00:20ff:fe72:9724 2000 0 1100 0 0
24 changes: 19 additions & 5 deletions lib/ifconfig/bsd/ifconfig.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ class Ifconfig
# ifconfig = user provided ifconifg output
# netstat = same, but for netstat -in
#
# XXX On FreeBSD, "netstat -inb" will show byte _and_ packet statistics, but
# on NetBSD and OpenBSD, it will show byte statistics only. We use "-in"
# instead of "-inb" for cross-platform compatibility.
#
@@ifcfg_cmd = "/usr/bin/env ifconfig -a"
@@netstat_cmd = "/usr/bin/env netstat -in"
def initialize(ifconfig=nil,netstat=nil,verbose=nil)
Expand All @@ -27,6 +31,12 @@ def initialize(ifconfig=nil,netstat=nil,verbose=nil)
when /^lo\d\:/im
@ifaces[iface_name] = LoopbackInterface.new(iface_name,iface)
parse_activity(iface_name)
when /^lagg[0-9]:/
# This clause is only matched on FreeBSD. Other OSes have similar
# drivers, for example agr(4) on NetBSD, but ruby-ifconfig does not
# yet support them.
@ifaces[iface_name] = LinkAggregation.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)
Expand All @@ -49,17 +59,21 @@ def initialize(ifconfig=nil,netstat=nil,verbose=nil)
#
def parse_activity(iface)
mtu = rxpackets = rxerrors = txpackets = txerrors = 0
fields = {}
@netstat.split("\n").each { |line|
line.strip!
if line =~ /^#{iface}/
if line =~/^Name/
headers = line.split(" ")
headers.each_index {|i| fields[headers[i]] = i}
elsif line =~ /^#{iface}/
next if line.split[2] =~ /\<Link\#\d\>/
puts "matched line for "+iface if @verbose
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
rxpackets += toks[fields["Ipkts"]].to_i
rxerrors += toks[fields["Ierrs"]].to_i
txpackets += toks[fields["Opkts"]].to_i
txerrors += toks[fields["Oerrs"]].to_i
@ifaces[iface].mtu = mtu.to_i
@ifaces[iface].rx = { 'packets' => rxpackets,
'errors' => rxerrors }
Expand Down
62 changes: 57 additions & 5 deletions lib/ifconfig/bsd/interface_types.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,39 @@ class NetworkAdapter
# iterate line by line and dispatch to helper functions
# for lines that match a pattern
#
def parse_ifconfig
def parse_ifconfig(netstattxt=nil)
@ifconfig.split("\n").each { |line|
case line
when /^\s+#{@protos}/
add_network(line)
when /flags\=/i
when /\s*flags\=/i
parse_flags(line)
when /^\s*fib:/
parse_fib(line)
when /^\s*media:/
parse_media(line)
when /^\s*options=/
# FreeBSD and DragonFlyBSD use "options="
parse_capabilities(line)
when /^\s*enabled=/
# NetBSD uses "enabled="
parse_capabilities(line)
end
}
end

# parses the "fib: 1" line
def parse_fib(line)
@fib = line.split()[1].to_i
end

# parses the "options=1b<RXCSUM,TXCSUM,VLAN_MTU,VLAN_HWTAGGING>" line
#
def parse_capabilities(line)
caps = line.match(/\<(\S+)\>/i)[1]
@capabilities = caps.strip.split(',')
end

# parses the "UP LOOPBACK RUNNING MTU:3924 Metric:1" line
#
def parse_flags(line)
Expand All @@ -35,8 +57,8 @@ def parse_flags(line)
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)
addr,mask = line.match(/\s+((\d{1,3}\.){3,}\d{1,3})\s+netmask\s+(\S+)/i)[1..2]
bcast = line.match(/\s+broadcast\s([{1,3}d?\.]{4,})/i)
bcast = bcast[1] unless bcast.nil?
@networks['inet'] = Ipv4Network.new(addr, mask, bcast)
when /^\s+inet6\s/
Expand All @@ -55,15 +77,45 @@ def add_network(line)


class EthernetAdapter
# Parses the "media: Ethernet autoselect (1000baseT <full-duplex>)" line
def parse_media(line)
match = line.match(/Ethernet (autoselect )?(\()?(\S+?)[ )]/)
if match
@media = match[3]
end
end

def set_mac
begin
match=@ifconfig.match(/\s+ether\s+([a-f\d]{1,2}(?:\:[a-f\d]{1,2}){5})/im)
return match[1] unless match.nil?
# Openbsd
# OpenBSD and NetBSD
match = @ifconfig.match(/\s+address\:\s+([a-f\d]{1,2}(?:\:[a-f\d]{1,2}){5})/im)
return match[1] unless match.nil?
rescue NoMethodError
puts "Couldn't Parse MAC Address for: "+@name
end
end
end


class LinkAggregation
def parse_ifconfig(netstattxt=nil)
if @lagg_children.nil?
@lagg_children = []
end
super(netstattxt)
@ifconfig.split("\n").each { |line|
proto_match = line.match( /^\s*laggproto\s*(\S+)\s*/ )
if proto_match
@laggproto = proto_match[1]
next
end
port_match = line.match( /^\s*laggport:\s*(\S+)\s*/ )
if port_match
@lagg_children << port_match[1]
next
end
}
end
end
Loading