Class: Bandshell::Interface
- Inherits:
-
Object
- Object
- Bandshell::Interface
- Defined in:
- lib/bandshell/netconfig.rb
Overview
Some useful interface operations.
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Get the name of the interface as a string.
Instance Method Summary collapse
- #down ⇒ Object
- #ifdown ⇒ Object
- #ifup ⇒ Object
-
#initialize(name) ⇒ Interface
constructor
Wrap an interface name (eth0, wlan0 etc) with some useful operations.
-
#ip ⇒ Object
Get the (first) IPv4 address assigned to the interface.
-
#mac ⇒ Object
Get the physical (mac, ethernet) address of the interface.
- #medium_present? ⇒ Boolean
- #up ⇒ Object
- #up? ⇒ Boolean
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
#name ⇒ Object (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
#down ⇒ Object
65 66 67 |
# File 'lib/bandshell/netconfig.rb', line 65 def down system("/sbin/ifconfig #{@name} down") end |
#ifdown ⇒ Object
73 74 75 |
# File 'lib/bandshell/netconfig.rb', line 73 def ifdown system("/sbin/ifdown #{@name}") end |
#ifup ⇒ Object
69 70 71 |
# File 'lib/bandshell/netconfig.rb', line 69 def ifup system("/sbin/ifup #{@name}") end |
#ip ⇒ Object
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 |
#mac ⇒ Object
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
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 |
#up ⇒ Object
61 62 63 |
# File 'lib/bandshell/netconfig.rb', line 61 def up system("/sbin/ifconfig #{@name} up") end |
#up? ⇒ 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 |