Skip to content
Merged
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
26 changes: 26 additions & 0 deletions lib/linux_admin/ip_address.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,34 @@ def address6
address_list.detect { |ip| IPAddr.new(ip).ipv6? }
end

def mac_address(interface)
result = run(cmd("ip"), :params => ["addr", "show", interface])
return nil if result.failure?

parse_output(result.output, %r{link/ether}, 1)
end

def netmask(interface)
result = run(cmd("ifconfig"), :params => [interface])
return nil if result.failure?

parse_output(result.output, /netmask/, 3)
end

def gateway
result = run(cmd("ip"), :params => ["route"])
return nil if result.failure?

parse_output(result.output, /^default/, 2)
end

private

def parse_output(output, regex, col)
the_line = output.split("\n").detect { |l| l =~ regex }
the_line.nil? ? nil : the_line.strip.split(' ')[col]
end

def address_list
result = nil
# Added retry to account for slow DHCP not assigning an IP quickly at boot; specifically:
Expand Down
110 changes: 103 additions & 7 deletions spec/ip_address_spec.rb
Original file line number Diff line number Diff line change
@@ -1,52 +1,148 @@
describe LinuxAdmin::IpAddress do
let(:ip) { described_class.new }

SPAWN_ARGS = [
ADDR_SPAWN_ARGS = [
described_class.new.cmd("hostname"),
:params => ["-I"]
]

MAC_SPAWN_ARGS = [
described_class.new.cmd("ip"),
:params => %w(addr show eth0)
]

MASK_SPAWN_ARGS = [
described_class.new.cmd("ifconfig"),
:params => %w(eth0)
]

GW_SPAWN_ARGS = [
described_class.new.cmd("ip"),
:params => %w(route)
]

IP_ADDR_SHOW_ETH0 = <<-IP_OUT
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:ed:0e:8b brd ff:ff:ff:ff:ff:ff
inet 192.168.1.9/24 brd 192.168.1.255 scope global dynamic eth0
valid_lft 1297sec preferred_lft 1297sec
inet6 fe80::20c:29ff:feed:e8b/64 scope link
valid_lft forever preferred_lft forever

IP_OUT

IFCFG = <<-IP_OUT
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.1.9 netmask 255.255.255.0 broadcast 192.168.1.255
inet6 fe80::20c:29ff:feed:e8b prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:ed:0e:8b txqueuelen 1000 (Ethernet)
RX packets 10171 bytes 8163955 (7.7 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 2871 bytes 321915 (314.3 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

IP_OUT

IP_ROUTE = <<-IP_OUT
default via 192.168.1.1 dev eth0 proto static metric 100
192.168.1.0/24 dev eth0 proto kernel scope link src 192.168.1.9 metric 100
IP_OUT

def result(output, exit_status)
AwesomeSpawn::CommandResult.new("", output, "", exit_status)
end

describe "#address" do
it "returns an address" do
ip_addr = "192.168.1.2"
expect(AwesomeSpawn).to receive(:run).with(*SPAWN_ARGS).and_return(result(ip_addr, 0))
expect(AwesomeSpawn).to receive(:run).with(*ADDR_SPAWN_ARGS).and_return(result(ip_addr, 0))
expect(ip.address).to eq(ip_addr)
end

it "returns nil when no address is found" do
ip_addr = ""
expect(AwesomeSpawn).to receive(:run).at_least(5).times.with(*SPAWN_ARGS).and_return(result(ip_addr, 1))
expect(AwesomeSpawn).to receive(:run).at_least(5).times.with(*ADDR_SPAWN_ARGS).and_return(result(ip_addr, 1))
expect(ip.address).to be_nil
end

it "returns only IPv4 addresses" do
ip_addr = "fd12:3456:789a:1::1 192.168.1.2"
expect(AwesomeSpawn).to receive(:run).with(*SPAWN_ARGS).and_return(result(ip_addr, 0))
expect(AwesomeSpawn).to receive(:run).with(*ADDR_SPAWN_ARGS).and_return(result(ip_addr, 0))
expect(ip.address).to eq("192.168.1.2")
end
end

describe "#address6" do
it "returns an address" do
ip_addr = "fd12:3456:789a:1::1"
expect(AwesomeSpawn).to receive(:run).with(*SPAWN_ARGS).and_return(result(ip_addr, 0))
expect(AwesomeSpawn).to receive(:run).with(*ADDR_SPAWN_ARGS).and_return(result(ip_addr, 0))
expect(ip.address6).to eq(ip_addr)
end

it "returns nil when no address is found" do
ip_addr = ""
expect(AwesomeSpawn).to receive(:run).at_least(5).times.with(*SPAWN_ARGS).and_return(result(ip_addr, 1))
expect(AwesomeSpawn).to receive(:run).at_least(5).times.with(*ADDR_SPAWN_ARGS).and_return(result(ip_addr, 1))
expect(ip.address6).to be_nil
end

it "returns only IPv6 addresses" do
ip_addr = "192.168.1.2 fd12:3456:789a:1::1"
expect(AwesomeSpawn).to receive(:run).with(*SPAWN_ARGS).and_return(result(ip_addr, 0))
expect(AwesomeSpawn).to receive(:run).with(*ADDR_SPAWN_ARGS).and_return(result(ip_addr, 0))
expect(ip.address6).to eq("fd12:3456:789a:1::1")
end
end

describe "#mac_address" do
it "returns the correct MAC address" do
expect(AwesomeSpawn).to receive(:run).with(*MAC_SPAWN_ARGS).and_return(result(IP_ADDR_SHOW_ETH0, 0))
expect(ip.mac_address("eth0")).to eq("00:0c:29:ed:0e:8b")
end

it "returns nil when the command fails" do
expect(AwesomeSpawn).to receive(:run).with(*MAC_SPAWN_ARGS).and_return(result("", 1))
expect(ip.mac_address("eth0")).to be_nil
end

it "returns nil if the link/ether line is not present" do
bad_output = IP_ADDR_SHOW_ETH0.gsub(%r{link/ether}, "")
expect(AwesomeSpawn).to receive(:run).with(*MAC_SPAWN_ARGS).and_return(result(bad_output, 0))
expect(ip.mac_address("eth0")).to be_nil
end
end

describe "#netmask" do
it "returns the correct netmask" do
expect(AwesomeSpawn).to receive(:run).with(*MASK_SPAWN_ARGS).and_return(result(IFCFG, 0))
expect(ip.netmask("eth0")).to eq("255.255.255.0")
end

it "returns nil when the command fails" do
expect(AwesomeSpawn).to receive(:run).with(*MASK_SPAWN_ARGS).and_return(result("", 1))
expect(ip.netmask("eth0")).to be_nil
end

it "returns nil if the netmask line is not present" do
bad_output = IFCFG.gsub(/netmask/, "")
expect(AwesomeSpawn).to receive(:run).with(*MASK_SPAWN_ARGS).and_return(result(bad_output, 0))
expect(ip.netmask("eth0")).to be_nil
end
end

describe "#gateway" do
it "returns the correct gateway address" do
expect(AwesomeSpawn).to receive(:run).with(*GW_SPAWN_ARGS).and_return(result(IP_ROUTE, 0))
expect(ip.gateway).to eq("192.168.1.1")
end

it "returns nil when the command fails" do
expect(AwesomeSpawn).to receive(:run).with(*GW_SPAWN_ARGS).and_return(result("", 1))
expect(ip.gateway).to be_nil
end

it "returns nil if the default line is not present" do
bad_output = IP_ROUTE.gsub(/default/, "")
expect(AwesomeSpawn).to receive(:run).with(*GW_SPAWN_ARGS).and_return(result(bad_output, 0))
expect(ip.gateway).to be_nil
end
end
end