Skip to content

Navigation Menu

Sign in
Sign up

network_interfaces command on interfaces with multiple addresses #1536

rwp0 started this conversation in General
Discussion options

network_interfaces command of Rex::Commands::Gather module fails to reflect the interface with multiple IP addresses only retrieving the first one.

I run Rex on FreeBSD so this eventually calls the get_network_configuration method of the Rex::Hardware::Network::FreeBSD class to obtain the device's network configuration information relying on the ifconfig Unix command:

sub get_network_configuration {
 my $devices = get_network_devices();
 my $device_info = {};
 for my $dev ( @{$devices} ) {
 my $ifconfig = i_run("ifconfig $dev");
 $device_info->{$dev} = {
 ip => [ ( $ifconfig =~ m/inet (\d+\.\d+\.\d+\.\d+)/ ) ]->[0], # <--- only first element (IP address) of array reference for each device
 netmask => $ifconfig =~ m/(?:netmask 0x|netmask )([a-f0-9]+)/
 ? sprintf( "%d.%d.%d.%d", unpack "C4", pack "H*", 1ドル )
 : undef,
 broadcast => [ ( $ifconfig =~ m/broadcast (\d+\.\d+\.\d+\.\d+)/ ) ]->[0],
 mac => [
 ( $ifconfig =~ m/(ether|address:|lladdr) (..?:..?:..?:..?:..?:..?)/ )
 ]->[1],
 is_bridge => 0,
 };
 }
 return $device_info;
}

This is very unfortunate for my use case, and many real world setups with VLAN's and IP aliases.

Should I instead do run "ifconfig"; and parse the output using CPAN modules (which avoids Rex very much)?

You must be logged in to vote

Replies: 1 comment

Comment options

Rex::Hardware::Network::Linux already has support for parsing the output of either ifconfig -a (_parse_ifconfig) or ip addr show (_parse_ip), and the latter already knows how to recognize multiple IP addresses of the same network interface.

Based on that, at least the following things come to my mind as coding opportunities:

  • It might be possible (or even best) to move _parse_ifconfig and _parse_ip out of the Rex::Hardware::Network::Linux namespace to the shared Rex::Hardware::Network one to share this capability across more systems (assuming the ifconfig and ip outputs are all the same, or at least sufficiently similar from the various OSes...).
  • It might be possible to teach _parse_ifconfig to recognize multiple addresses of the same network interface, and return data in the same structure as _parse_ip already does.
  • Rex::Hardware::Network::Darwin and Rex::Hardware::Network::NetBSD delegates all their job to to Rex::Hardware::Network::OpenBSD. Which in turn delegates some of its job to Rex::Hardware::Network::FreeBSD, and the rest of the code is highly duplicated. This seems like a good opportunity to remove (some of) the duplication.

As far as a workaround is concerned, I think there's currently no easy way to force using the Rex::Hardware::Network::Linux modules for discovery on other OSes. If the managed endpoint has the ip command available, and its output is in a compatible format, it might be possible to call Rex::Hardware::Network::Linux::get_network_configuration() directly and get some of the data though.

Getting the raw command output from the managed endpoint with run(), and then parsing that with another CPAN modules would also be feasible. If you know about a good module for that, we might even be able to pull that in as a dependency for Rex instead of doing our own parsing logic.

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants

AltStyle によって変換されたページ (->オリジナル) /