Class: Bandshell::Interface

Inherits:
Object
  • Object
show all
Defined in:
lib/bandshell/netconfig.rb

Overview

Some useful interface operations.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Interface

Wrap an interface name (eth0, wlan0 etc) with some useful operations.



37
38
39
# File 'lib/bandshell/netconfig.rb', line 37

def initialize(name)
  @name = name
end

Instance Attribute Details

#nameObject (readonly)

Get the name of the interface as a string.



42
43
44
# File 'lib/bandshell/netconfig.rb', line 42

def name
  @name
end

Instance Method Details

#downObject



65
66
67
# File 'lib/bandshell/netconfig.rb', line 65

def down
  system("/sbin/ifconfig #{@name} down")
end

#ifdownObject



73
74
75
# File 'lib/bandshell/netconfig.rb', line 73

def ifdown
  system("/sbin/ifdown #{@name}")
end

#ifupObject



69
70
71
# File 'lib/bandshell/netconfig.rb', line 69

def ifup
  system("/sbin/ifup #{@name}")
end

#ipObject

Get the (first) IPv4 address assigned to the interface. Return “0.0.0.0” if we don’t have any v4 addresses.



46
47
48
49
50
51
52
# File 'lib/bandshell/netconfig.rb', line 46

def ip
  if ifconfig =~ /inet addr:([0-9.]+)/
    $1
  else
    "0.0.0.0"
  end
end

#macObject

Get the physical (mac, ethernet) address of the interface.



55
56
57
58
59
# File 'lib/bandshell/netconfig.rb', line 55

def mac
  File.open("/sys/class/net/#{@name}/address") do |f|
    f.read.chomp
  end
end

#medium_present?Boolean

Returns:

  • (Boolean)


85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/bandshell/netconfig.rb', line 85

def medium_present?
  brought_up = false
  result = false

  if not up?
    brought_up = true
    up
    sleep 10
  end

  if ifconfig =~ /RUNNING/
    result = true
  end

  if brought_up
    down
  end

  result
end

#upObject



61
62
63
# File 'lib/bandshell/netconfig.rb', line 61

def up
  system("/sbin/ifconfig #{@name} up")
end

#up?Boolean

Returns:

  • (Boolean)


77
78
79
80
81
82
83
# File 'lib/bandshell/netconfig.rb', line 77

def up?
  if ifconfig =~ /UP/
    true
  else
    false
  end
end