⏱ O‘qish vaqti: 6 daqiqa
Qisqacha
- Routing jadvalidan tarmoq va gatewayni hex satrga aylantiradi
- Natija DHCP option 121 (RFC 3442) formatida bo‘ladi
- Skriptni terminalda bir marta ishga tushirish kifoya
RouterOS’da DHCP server orqali mijozlarga maxsus marshrut ma’lumotlarini yuborish uchun option 121 kerak bo‘ladi. Bu skript routing jadvalidagi har bir yo‘nalishning tarmog‘i va gatewayini hex formatiga o‘zgartiradi, shunda siz uni DHCP konfiguratsiyasiga qo‘shishingiz mumkin.
Talablar
- RouterOS terminaliga administrator huquqi bilan kirish
- Static yoki dinamik yo‘nalishlar bilan to‘ldirilgan
/ip routejadvali - Gateway bitta IP manzil bo‘lishi
Sozlash bosqichlari
1. Skriptni terminalga joylashtirish
Terminalni oching, quyidagi kodni nusxalab, kiriting. Skript har bir yo‘nalish uchun dst-address va gateway ni o‘qiydi, ularni birlashtiradi va hex satrga aylantiradi.
Skript ichida :foreach route in=[/ip route find] do={ sikli ishlatiladi, bu barcha yo‘nalishlarni ko‘rib chiqadi. Natija 0x prefiks bilan ekranga chiqariladi.
Version of the script giving result as DHCP option 121 (RFC 3442)
# Second script converting network and gateway from routing table to hexadecimal string
# according to RFC 3442 - DHCP option 121
# result=0x
# example: dst-address=192.168.2.0/24 gateway=192.168.1.1 => result=0x18c0a802c0a80101
#
# Network can have 0 to 4 octets:
# Width of subnet mask Number of significant octets
# 0 0
# 1- 8 1
# 9-16 2
# 17-24 3
# 25-32 4
#
# (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];
:set decimal [:pick $total2 4]
:set division ($decimal / 16);
:set result ($result . [:pick $hextable $division]);
:set result ($result . [:pick $hextable ($decimal - (16 * $division))]);
:local omit ((32-[:pick $total2 4]) / 8);
:for i from=($omit+1) to=8 step=1 do={
:set j $i;
:if ($j<5) do={:set j ($j-1-$omit);};
:set decimal [:pick $total2 $j]
:set division ($decimal / 16);
:set result ($result . [:pick $hextable $division]);
:set result ($result . [:pick $hextable ($decimal - (16 * $division))]);
};
:put ($result . " for " . $dst);
} else={:put ("Gateway is not a single IP address for ".$dst);}
}
Version of the script with full 4 bytes of destination network
# 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);}}
Credit: https://wiki.mikrotik.com/wiki/Converting_network_and_gateway_from_routing_table_to_hexadecimal_string
2. Natijani tahlil qilish
Skript bajarilgandan so‘ng terminalda har bir yo‘nalish uchun hex satr ko‘rinadi. Bu satrni DHCP server sozlamalarida option 121 qiymati sifatida qo‘shishingiz mumkin.
Tekshirish
Skript bajarilgandan so‘ng terminalda har bir yo‘nalish uchun 0x prefiksli satr ko‘rinadi, bu skript muvaffaqiyatli ishlaganligini bildiradi.
Version of the script giving result as DHCP option 121 (RFC 3442)
# Second script converting network and gateway from routing table to hexadecimal string
# according to RFC 3442 - DHCP option 121
# result=0x
# example: dst-address=192.168.2.0/24 gateway=192.168.1.1 => result=0x18c0a802c0a80101
#
# Network can have 0 to 4 octets:
# Width of subnet mask Number of significant octets
# 0 0
# 1- 8 1
# 9-16 2
# 17-24 3
# 25-32 4
#
# (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];
:set decimal [:pick $total2 4]
:set division ($decimal / 16);
:set result ($result . [:pick $hextable $division]);
:set result ($result . [:pick $hextable ($decimal - (16 * $division))]);
:local omit ((32-[:pick $total2 4]) / 8);
:for i from=($omit+1) to=8 step=1 do={
:set j $i;
:if ($j<5) do={:set j ($j-1-$omit);};
:set decimal [:pick $total2 $j]
:set division ($decimal / 16);
:set result ($result . [:pick $hextable $division]);
:set result ($result . [:pick $hextable ($decimal - (16 * $division))]);
};
:put ($result . " for " . $dst);
} else={:put ("Gateway is not a single IP address for ".$dst);}
}
Version of the script with full 4 bytes of destination network
# 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);}}
Credit: https://wiki.mikrotik.com/wiki/Converting_network_and_gateway_from_routing_table_to_hexadecimal_string
Diqqat:
- Skript faqat gateway bitta IP manzil bo‘lganda natija beradi
- Agar gateway bo‘sh yoki bir nechta IP bo‘lsa, skript «Gateway is not a single IP address» xabarini chiqaradi
Muammolar va yechimlar
Gateway bir nechta IP manzil yoki bo‘sh
Bunday yo‘nalishni o‘chirib, yagona gateway bilan qayta qo‘shing yoki skriptni moslashtiring
Xulosa
Skript routing jadvalidan DHCP option 121 uchun kerakli hex ma’lumotni tez va avtomatik olish imkonini beradi. Natijani DHCP server sozlamalariga qo‘shish orqali mijozlarga aniq marshrut ma’lumotlarini uzatish mumkin.
Manba: Converting Gateway From Routing To hexadecimal — mikrotiktool.github.io