class Bandshell::WiredConnection

Layer 2 connection via wired media. We will look for wired interfaces that have media connected, or use an interface chosen by the user via the args. There’s nothing extra to be contributed to the interfaces file besides the name of the interface to be used.

Attributes

interface_name[RW]

If interface_name is something other than nil or the empty string, we will override the automatic detection and use that interface.

Public Class Methods

description() click to toggle source
# File lib/bandshell/netconfig.rb, line 216
def self.description
  "Wired connection"
end
interfaces() click to toggle source

Try to find all wired interfaces on the system.

# File lib/bandshell/netconfig.rb, line 210
def self.interfaces
  # This is somewhat Linux specific and may miss some oddballs.
  devices = Dir.glob('/sys/class/net/eth*')
  devices.map { |d| Interface.new(File.basename(d)) }
end
new(args={}) click to toggle source
# File lib/bandshell/netconfig.rb, line 161
def initialize(args={})
  if args['interface_name']
    @interface_name = args['interface_name']
  end
end

Public Instance Methods

args() click to toggle source
# File lib/bandshell/netconfig.rb, line 189
def args
  {
    'interface_name' => @interface_name
  }
end
config_interface_name() click to toggle source
# File lib/bandshell/netconfig.rb, line 171
def config_interface_name
  if @interface_name && @interface_name.length > 0
    # the user has specified an interface to use
    @interface_name
  else
    # scan for the first wired interface that has media
    scan_interfaces
  end
end
interfaces_lines() click to toggle source
# File lib/bandshell/netconfig.rb, line 195
def interfaces_lines
  []
end
safe_assign() click to toggle source
# File lib/bandshell/netconfig.rb, line 185
def safe_assign
  [ :interface_name ]
end
validate() click to toggle source
# File lib/bandshell/netconfig.rb, line 199
def validate
  if @interface_name != ''
    if self.class.interfaces.find {
          |iface| iface.name == @interface_name
        }.nil?
      fail "The interface doesn't exist on the system"
    end
  end
end
write_configs() click to toggle source
# File lib/bandshell/netconfig.rb, line 167
def write_configs
  # We don't need any.
end

Private Instance Methods

scan_interfaces() click to toggle source

Find the first wired interface with medium present. If none is found default to eth0.

# File lib/bandshell/netconfig.rb, line 223
def scan_interfaces
  first_with_medium = self.class.interfaces.find {
    |iface| iface.medium_present?
  }

  if first_with_medium
    first_with_medium.name
  else
    # if we get here no interface was found with a cable attached
    # default to eth0 and hope for the best
    STDERR.puts "warning: no suitable interface found, using eth0"
    'eth0'
  end
end