Class: Specinfra::Command::Openbsd::Base::Interface

Inherits:
Base::Interface show all
Defined in:
lib/specinfra/command/openbsd/base/interface.rb

Class Method Summary collapse

Methods inherited from Base

create, escape

Class Method Details

.check_exists(name) ⇒ Object


3
4
5
# File 'lib/specinfra/command/openbsd/base/interface.rb', line 3

def check_exists(name)
  "ifconfig #{name}"
end

.check_has_ipv4_address(interface, ip_address) ⇒ Object


11
12
13
14
15
16
17
18
19
20
# File 'lib/specinfra/command/openbsd/base/interface.rb', line 11

def check_has_ipv4_address(interface, ip_address)
  ip_address = ip_address.dup
  if ip_address =~ /\/\d+$/    # remove the prefix - better would be to calculate the netmask

    ip_address.gsub!(/\/\d+$/, "")
  end
  ip_address << " "
  ip_address.gsub!(".", "\\.")
  "ifconfig #{interface} inet | grep 'inet #{ip_address}'"
end

.check_has_ipv6_address(interface, ip_address) ⇒ Object


22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/specinfra/command/openbsd/base/interface.rb', line 22

def check_has_ipv6_address(interface, ip_address)
  ip_address = ip_address.dup
  (ip_address, prefixlen) = ip_address.split(/\//)
  ip_address.downcase!
  if ip_address =~ /^fe80::/i    # link local needs the scope (interface) appended

    ip_address << "%#{interface}"
  end
  unless prefixlen.to_s.empty?    # append prefixlen

    ip_address << " prefixlen #{prefixlen}"
  else
    ip_address << " "
  end
  "ifconfig #{interface} inet6 | grep 'inet6 #{ip_address}'"
end

.get_ipv4_address(interface) ⇒ Object


39
40
41
# File 'lib/specinfra/command/openbsd/base/interface.rb', line 39

def get_ipv4_address(interface)
  "ifconfig #{interface} inet | grep inet | awk '{print $2}'"
end

.get_ipv6_address(interface) ⇒ Object


43
44
45
46
# File 'lib/specinfra/command/openbsd/base/interface.rb', line 43

def get_ipv6_address(interface)
  # Awk refuses to print '/' even with using escapes or hex so workaround with sed employed here.
  "ifconfig #{interface} inet6 | grep inet6 | awk '{print $2$3$4}' | sed 's/prefixlen/\//'; exit"
end

48
49
50
51
52
53
54
# File 'lib/specinfra/command/openbsd/base/interface.rb', line 48

def get_link_state(interface)
  # Checks if interfaces is administratively up by parsing the options.
  # L1 check via status. Virtual interfaces like tapX missing the status will report up.
  # Emulates operstate in linux with exception of the unknown status.
  %Q{ifconfig #{interface} 2>&1 | awk -v s=down -F '[:<>,]' } +
  %Q{'NR == 1 && $3 == "UP" { s="up" }; /status:/ && $2 != " active" { s="down" }; END{ print s }'}
end

.get_speed_of(name) ⇒ Object


7
8
9
# File 'lib/specinfra/command/openbsd/base/interface.rb', line 7

def get_speed_of(name)
  "ifconfig #{name} | grep 'media\:' | perl -pe 's|.*media\:.*\\((.*?)\\)|\\1|'"
end