ip firewall nat
add action=src-nat chain=srcnat comment="" disabled=no src-address=192.168.11.0/24 to-addresses=8.8.8.135
please help create script:
IP adress 8.8.8.129 be it oneself RANDOM change from area 8.8.8.129/25 every hour
thanks.
/ip firewall nat
add action=src-nat chain=srcnat comment=1 disabled=no src-address=192.168.11.0/24 to-addresses=8.8.8.129
:do {
#set rules comment
:local com "1"
#set start count
:local start "129"
#set ip address prefix
:local prefix "8.8.8."
/ip firewall nat
:foreach a in=[find comment=$com] do={
:local cuip [get $a to-addresses]
:local aa [:pick $cuip 6 9]
:if ($aa!="254") do={
:local bb ($aa+1)
set $a to-addresses=($prefix.$bb)
}
:if ($aa="254") do={
:local bb $start
set $a to-addresses=($prefix.$bb)
}}}
first you must add a rules for:Code: Select all/ip firewall nat add action=src-nat chain=srcnat comment=1 disabled=no src-address=192.168.11.0/24 to-addresses=8.8.8.129
Code: Select all:do { #set rules comment :local com "1" #set start count :local start "129" #set ip address prefix :local prefix "8.8.8." /ip firewall nat :foreach a in=[find comment=$com] do={ :local cuip [get $a to-addresses] :local aa [:pick $cuip 6 9] :if ($aa!="254") do={ :local bb ($aa+1) set $a to-addresses=($prefix.$bb) } :if ($aa="254") do={ :local bb $start set $a to-addresses=($prefix.$bb) }}}
$IP&0.0.0.255
Yes this will work with both, Integer and IP data types. ok thank's I will add some examples in wiki.Very cool!!!Code: Select all$IP&0.0.0.255
IMHO, this should be documented in the Wiki, preferably with some example like that. Right now, the bitwise operators give off the impression they work only for integers, and not between two IPs (which I understand are treated as integers "under the hood", but still - the fact this treatment takes place is not intuitive).
The problem here is with pick command - picking from 6th digit to 9th digit works fine with "8.8.8.x" but it fails with higher IPs because starting from 6th to 9th digit for IP "192.168.16.x" will give you different output (6.1)
Best solution is to use Bitwise operators to only take specific part of IP
For example:
{
:local IP 8.8.8.8
:put (($IP&0.0.0.255))
}
will always give you last 8 bits of IP.
Here is working code for all IP formats:
{
:local com "1"
:local cuip
:local start "129"
:local end
:local endNum
:local first
:local firstNum
:local nextIP
:while (true) do={
:delay 60m;
:foreach a in=[/ip firewall nat find where comment=$com] do={
:set $cuip [/ip firewall nat get $a to-addresses];
###Get last
:set $end ($cuip&0.0.0.255);
:set $first ($cuip&255.255.255.0);
:set $endNum [:pick $end 6 ([:len $end])];
:set $firstNum [:pick $first 0 ([:len $first]-2)];
:if ($endNum>=129 && $endNum<=253) do={:set $endNum ($endNum +1);}
:if ($endNum=254) do={:set $endNum $start;}
:set $nextIP ($firstNum.".".$endNum);
/ip firewall nat set $a to-addresses="$nextIP";
}
}
}
This script will change all NAT rules with comment="1", changing to-address end from 129-253
It will not change to-address if IP end is less then 129