#!/usr/bin/perl use strict; use warnings; use IO::Handle; my $conf = {}; # Read the 'ip addr' my $device = ""; my $mac_address = ""; my $file_handle = IO::Handle->new(); my $shell_call = "ip addr"; open ($file_handle, "$shell_call 2>&1 |") or die "Failed to call: [$shell_call], error: $!\n"; while (<$file_handle>) { chomp; my $line = $_; if ($line =~ /^\d+: (\S+):/) { $device = $1; next if $device eq "lo"; $mac_address = ""; next; } if ($line =~ /ether (.*?) /) { $mac_address = $1; next if not $device; $conf->{$device} = $mac_address; } } foreach my $device (sort {$a cmp $b} keys %{$conf}) { my $say_dev = lc($device); my $say_mac = lc($conf->{$device}); print "\n# Added by 'generate-udev-net' for detected device '$device'.\n"; print "SUBSYSTEM==\"net\", ACTION==\"add\", DRIVERS==\"?*\", ATTR{address}==\"$say_mac\", NAME=\"$say_dev\"\n"; } exit(0);