Community discussions

MUM Europe 2020
 
User avatar
THG
Member
Member
Topic Author
Posts: 472
Joined: Thu Oct 15, 2009 1:05 am

Calculate DHCP option 121 (Classless Static Route Option)

Thu Jan 13, 2011 10:19 pm

I have a perl script to calculate routes to hexadecimal, so it works with MikroTik DHCP Server. I'm now asking if anyone has anything similar, Linux binary or source code etc.?

The Classless Static Route Option for Dynamic Host Configuration Protocol (DHCP) version 4.

http://www.networksorcery.com/enp/rfc/rfc3442.txt
#!/usr/bin/perl -w

use strict;

    print "\033[2J";    #clear the screen
    print "\033[0;0H";  #jump to 0,0

sub option_121 {
    my $gw = shift;
    my $string = '';
    my ($subnet, $mask, $b0, $b1, $b2, $b3);
    foreach my $cidr (@_) {
        ($subnet,  $mask) = split('/', $cidr);
        ($b0, $b1, $b2, $b3) = split(/\./, $subnet);
        $string .= sprintf('%02x', $mask);
        $string .= sprintf('%02x', $b0) if($mask > 0);
        $string .= sprintf('%02x', $b1) if($mask > 8);
        $string .= sprintf('%02x', $b2) if($mask > 16);
        $string .= sprintf('%02x', $b3) if($mask > 24);
        $string .= sprintf('%02x%02x%02x%02x', split(/\./, $gw));
    }
    return $string;
}

if(@ARGV < 2)
{
    print "\n";
    print "Usage: $0 [gateway] [host|network]/[bitmask]\n\n";
    print "Example: $0 192.168.0.1 192.168.10.0/24\n\n";
}
elsif($ARGV[0] =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/)
{
    print "DHCP option 121 (249) hex string: ".option_121(@ARGV)."\n";
}
else
{
    print "Invalid gateway IP address: '$ARGV[0]'\n";
}
Last edited by THG on Sat Dec 31, 2011 5:19 pm, edited 1 time in total.
 
User avatar
astounding
Member Candidate
Member Candidate
Posts: 121
Joined: Tue Dec 16, 2008 12:17 am

Re: Calculate DHCP option 121 (Classless Static Route Option

Sat Jan 15, 2011 5:06 am

No Linux binaries nor C/C++ code here. How about a quick Ruby translation using the excellent 'ipaddress' gem?
(See: https://github.com/bluemonk/ipaddress for 'ipaddress' gem info...)

I ripped this from some DHCP Ruby code I've been using (a Ruby library that parses and generates DHCP packets) and added the hexification like your script.

Oh, this script makes an assumption that the GATEWAY IP address will always be ON or PART of the network. Therefore each argument is a gateway IP/mask where the mask can either be a four-octet version (i.e. "255.255.255.0") or a CIDR integer number of mask bits (0 through 32 inclusive).

So if the network is 192.168.100.0/24 and the gateway is at .1, I would do:
./scriptname.rb 192.168.100.1/24
and would see output
NET: 192.168.100.0/24	GATEWAY: 192.168.100.1	DHCP(Opt. 121): 18c0a864c0a86401
Here's the Ruby script:
#!/usr/bin/env ruby

require 'rubygems'
require 'ipaddress'

if ARGV.size < 1
  STDERR.print "\n" +
    "Usage: #{$0} gateway/mask [gateway/mask ...]\n" +
    "\n" +
    "Example: #{$0} 192.168.100.1/24\n" +
    "\n"
  exit -1
end

ARGV.each do |gnet|
  gnet = IPAddress::IPv4.new(gnet)
  hex  = (
    gnet.prefix.to_i.chr +                    ## Mask bits (single byte)
    gnet.network.data[0,(gnet.prefix+7)/8] +  ## Compressed Network IP
    gnet.data                                 ## Gateway IP
  ).unpack('H*')[0]                            
  puts "NET: #{gnet.network.to_string}\tGATEWAY: #{gnet.to_s}\tDHCP(Opt. 121): #{hex}"
end
Unlike the prior posted Perl script, this won't handle a gateway IP that doesn't fall within the network without some adaptation. (Does that ever happen? Would it ever work?)

Aaron out.
 
User avatar
THG
Member
Member
Topic Author
Posts: 472
Joined: Thu Oct 15, 2009 1:05 am

Re: Calculate DHCP option 121 (Classless Static Route Option

Sat Jan 15, 2011 10:47 pm

Thank you for the script, I'll take a look at it after ruby is installed on my linux computer.

I'll also try to create a web formular using javascript.
 
cieplik206
Trainer
Trainer
Posts: 286
Joined: Sun Jul 01, 2007 12:25 am
Contact:

Re: Calculate DHCP option 121 (Classless Static Route Option)

Wed Mar 07, 2018 9:08 pm

You can try this generator if you want to use it.
https://ip-pro.eu/en/mikrotik_dhcp_option_121_generator
https://www.wispcasts.com - Mikrotik Video Tutorials.

The best place where you can improve your skills!

Facebook - F/wispcasts
Twitter - @wispcasts

Who is online

Users browsing this forum: No registered users and 3 guests