Created
May 8, 2019 08:03
-
-
Save ba0f3/d79630c8fe0c08620db4cb33148649ae to your computer and use it in GitHub Desktop.
Transfer of static routes to DHCP http://linuxconfig.net/manuals/howto/transfer-of-static-routes-to-dhcp.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl | |
use strict; | |
# Usage: | |
# make_classless_option({ "subnet/mask" => "router", "subnet/mask" => "router", ... }); | |
# subnet the subnet address, 4 dot-separated numbers | |
# mask the subnet mask length (e.g. /24 corresponds to 255.255.255.0, /8 corresponds to 255.0.0.0) | |
# router the router address, 4 dot-separated numbers | |
sub make_classless_option{ | |
my $routes = shift; | |
my ($s1, $s2, $s3, $s4, $len, @bytes, $net, $mask, $destination, $router); | |
$len = 2; | |
@bytes = (); | |
foreach $destination(keys %{$routes}) { | |
($net, $mask) = split('/', $destination); | |
$router = $routes->{$destination}; | |
($s1, $s2, $s3, $s4) = split(/\./, $net); | |
push(@bytes, sprintf('%02x', $mask)); | |
push(@bytes, sprintf('%02x', $s1)); | |
push(@bytes, sprintf('%02x', $s2)) if($mask > 8); | |
push(@bytes, sprintf('%02x', $s3)) if($mask > 16); | |
push(@bytes, sprintf('%02x', $s4)) if($mask > 24); | |
($s1, $s2, $s3, $s4) = split(/\./, $router); | |
push(@bytes, sprintf('%02x', $s1)); | |
push(@bytes, sprintf('%02x', $s2)); | |
push(@bytes, sprintf('%02x', $s3)); | |
push(@bytes, sprintf('%02x', $s4)); | |
} | |
return join(':', @bytes); | |
} | |
# Example of use | |
print make_classless_option({ | |
"10.230.0.0/16" => "10.230.178.145" | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment