Converting network and gateway from routing table to hexadecimal string

From MikroTik Wiki
Revision as of 16:23, 31 December 2011 by Dasiu (talk | contribs) (Created page with "<pre># # Script converting network and gateway from routing table to hexadecimal string # dst-address=192.168.2.0/24 gateway=192.168.1.1 => result=0x18c0a80200c0a80101 # # (c) Da...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
#
# Script converting network and gateway from routing table to hexadecimal string
# dst-address=192.168.2.0/24 gateway=192.168.1.1 => result=0x18c0a80200c0a80101
#
# (c) Daniel Starnowski 2011
#
:foreach route in=[/ip route find] do={
:local dst [/ip route get $route dst-address];
:local gateway [/ip route get $route gateway];
:if ($gateway=[:toip $gateway]) do={
  :local total ($dst . "." . $gateway);
  :local result "0x";
  :local hextable [:toarray "0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f"];
  :local total2 "";
  :local decimal;
  :local division;
  :local i;
  :local j;
  :for i from=1 to=[:len $total] step=1 do={
   :set j [:pick $total ($i-1) $i];
   :if (($j=".") or ($j="/")) do={:set j ","};
   :set total2 ($total2 . $j);
   };
  :set total2 [:toarray $total2];

  :for i from=0 to=8 step=1 do={
   :set j $i;
   :if ($j<5) do={if ($j=0) do={:set j 4;} else={:set j ($j-1);};};
   :set decimal [:pick $total2 $j ($j+1)]
   :set division ($decimal / 16);
   :set result ($result . [:pick $hextable $division]);
   :set result ($result . [:pick $hextable ($decimal - (16 * $division))]);
   };
  :put $result;
} else={:put ("Gateway is not a single IP address for ".$dst);}
}