Skip to content

Instantly share code, notes, and snippets.

@raven428
Created October 10, 2017 06:04
Show Gist options
  • Save raven428/5a9d3e5bcaa5138c5f2b77d01979f409 to your computer and use it in GitHub Desktop.
Save raven428/5a9d3e5bcaa5138c5f2b77d01979f409 to your computer and use it in GitHub Desktop.
#!/usr/bin/env perl
#
# $Id: ms-classless-routes-decoder.pl,v 1.2 2017/10/10 05:59:07 raven Exp $
#
use strict;
use warnings;
my $in = $ARGV[0];
my @in = split(/:/, $in);
foreach(@in) { $_ = hex($_); }
my @routes = ();
while($#in >= 0) {
my $mask = shift(@in);
if($mask == 0) {
for(1..4) { shift(@in); };
} elsif(($mask >= 1) and ($mask <= 8)) {
my $route = shift(@in) . '.0.0.0/' . $mask . ' ';
for(1..4) { $route .= shift(@in) . '.'; };
print("$route\n");
} elsif(($mask >= 9) and ($mask <= 16)) {
my $route = '';
$route .= shift(@in) . '.' . shift(@in) . '.0.0/' . $mask . ' ' .
shift(@in) . '.' . shift(@in) . '.' . shift(@in) . '.' . shift(@in);
print("$route\n");
} elsif(($mask >= 17) and ($mask <= 24)) {
my $route = '';
$route .= shift(@in) . '.' . shift(@in) . '.' . shift(@in) . '.0/' . $mask . ' ' .
shift(@in) . '.' . shift(@in) . '.' . shift(@in) . '.' . shift(@in);
print("$route\n");
} elsif(($mask >= 24) and ($mask <= 32)) {
my $route = '';
$route .= shift(@in) . '.' . shift(@in) . '.' . shift(@in) . '.' . shift(@in) . '/' . $mask . ' ' .
shift(@in) . '.' . shift(@in) . '.' . shift(@in) . '.' . shift(@in);
print("$route\n");
};
};
#10:c0:a8:a:31:9c:1:c:a:30:a:31:9c:1:18:a:64:c:a:31:9c:1:18:4e:1d:0:a:31:9c:1
# http://www.ietf.org/rfc/rfc3442.txt
@raven428
Copy link
Author

raven428 commented Oct 10, 2017

dhcpd.conf:

option ms-classless-static-routes code 249 = array of integer 8;
option rfc3442-classless-static-routes code 121 = array of integer 8;

/etc/dhclient-exit-hooks:

new_routes249=`/path/ms-classless-routes-decoder.pl ${new_option_249}`
old_routes249=`/path/ms-classless-routes-decoder.pl ${old_option_249}`
printf "${new_routes249}" | awk '{ print "delete " $1 }' | xargs -n 2 route
printf "${old_routes249}" | awk '{ print "delete " $1 }' | xargs -n 2 route
printf "${new_routes249}" | awk '{ print "add " $0 }' | xargs -n 3 route

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment