Skip to content

Instantly share code, notes, and snippets.

@ba0f3
Created May 8, 2019 08:03
Show Gist options
  • Save ba0f3/d79630c8fe0c08620db4cb33148649ae to your computer and use it in GitHub Desktop.
Save ba0f3/d79630c8fe0c08620db4cb33148649ae to your computer and use it in GitHub Desktop.
#!/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