Community discussions

MikroTik App
 
johndol
just joined
Topic Author
Posts: 2
Joined: Wed Jan 17, 2024 8:15 pm

Help me - make script change ip adress every rto

Wed Jan 17, 2024 8:25 pm

Hi, i new user using mikrotik, and i using ROS 7, i have problem. i have conection from my ISP using ip 10.130.x.x/17 and the "x" ip is random and change every time. anyone can help me make script to change that ip every the internet conection RTO? the rules just working when internet conection RTO (3 second) then the ip adress from ether 1 change to 10.130.x.x/17 (note: the "x" ip random)
Please help me to make this script. Thanks alot
 
User avatar
vingjfg
Member
Member
Posts: 344
Joined: Fri Oct 20, 2023 1:45 pm

Re: Help me - make script change ip adress every rto

Wed Jan 17, 2024 9:13 pm

Hi @ johndol,

I am not entirely sure what you are asking, for example I do not understand what you want to change.

Your ISP assigns the external interface of your router an IP address, in the range 10.130.0.0/17, and that IP changes quite often. When the IP changes. what do you want to modify?

You mention a rule, are you referring to a firewall rule? a NAT rule? Something else?

Also, feel free to export your router configuration and post it here. You can do that by using the following command in the terminal (CLI), downloading the file "configexport.txt" using winbox or webfig, and adding it as an attachment to your message.
/export file=configexport.txt
 
johndol
just joined
Topic Author
Posts: 2
Joined: Wed Jan 17, 2024 8:15 pm

Re: Help me - make script change ip adress every rto

Wed Jan 17, 2024 9:40 pm

Hi @vingjfg

sorry if make you confuse, i just want change my wan ip adress on ether1 when my internet conection down.

/system script
add name=change-ip-on-rto source={
    :local pingResult [/ping 8.8.8.8 count=3];
    :log info "Ping Result: $pingResult";
    :if (($pingResult->"received") = 0) do={
        :local newHost ([:tostr [:pick [:tonum [:rand 256]] 0 3]]);
        :local newIP ("10.130." . $newHost . ".1/17");
        :log info ("RTO detected, changing IP address to $newIP");
        :put ("Changing IP address to $newIP");
        /ip address set [find interface="ether1"] address=$newIP;
    }
}

i ask chat gpt to help me make the script , but didnt work.
Last edited by tangent on Thu Jan 18, 2024 7:59 am, edited 4 times in total.
Reason: put script in "code" block
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 12032
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Help me - make script change ip adress every rto

Thu Jan 18, 2024 11:25 am

[…] :rand […]
i ask chat gpt to help me make the script , but didnt work.
It is useless to ask the artificial deficiency.
ChatGPT invents the script just to please you.
The instructions are invented (like :rand) and do not exist.
Obviously they don't work...

Explaining what the OP wants for others who are having trouble understanding:
After 3 seconds that the "internet no longer works" the IP must be changed to another one, because perhaps the traffic assigned to that address has run out.
To overcome this limit, he wants to deceive the service provider by changing the IP address to a different one.
It is missing in detail whether the IP receives it via DHCP randomly or it must be entered "by hand".

Obviously, OP, ignore @vingjfg requst, and I do not make the same request, for obtain the same result...
 
User avatar
vingjfg
Member
Member
Posts: 344
Joined: Fri Oct 20, 2023 1:45 pm

Re: Help me - make script change ip adress every rto

Thu Jan 18, 2024 10:24 pm

Something like this should do the job. Please review before running as it hasn't been fully tested. Also, know that you are using it under your own responsibility.
/system script
add name=change-ip-on-rto source={
 # Is google pingable?
 :local pingResult [/ping 8.8.8.8 count=3];
 if ($pingResult = 0 ) do={
     # Google is not reachable!
     # Generate a "random" value
     # Note - not really random, low entropy
     # But should be enough for a non-cryptographic use
     :local counter 0
     :local intNames [ /interface find name]
     :foreach intName in=$intNames do={
       :set counter ($counter + [/interface get $intName tx-byte])
     }
     # A bit of trickery to change some bits
     # Truncate the counter to a 32-bit value and xor each 8-bit segment together
     :set counter ( ( $counter ^ 0xc3c3c3c3 ) & 0xffffffff )
     :local upperCounter (($counter >> 16) & 0xffff )
     :local lowerCounter ( $counter & 0xffff )
     :set counter ( $upperCounter ^ $lowerCounter )
     :set upperCounter (($counter >> 8) & 0xff )
     :set lowerCounter ( $counter & 0xff )
     :local newMidbyte ( $upperCounter ^ $lowerCounter )
     :local newIP ( "10.130." . $newMidbyte . ".1/17" )
     # Add a message in the logs
     :log info "No response from google - New IP: $newIP"
     # And change the IP of ether1
     /ip address set [ find interface="ether1" ] address=$newIP
    }
}
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 12032
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Help me - make script change ip adress every rto

Thu Jan 18, 2024 10:49 pm

The "name" in [/interface find name] is completely useless.


Is easy obtain a random number between 1 to 254 (or 0 to 255)...

v7 only
:put [:rndnum from=1 to=254]

v6 only
viewtopic.php?f=9&t=175453&p=858629#p858629
:global randomnum do={
    /system resource irq
    :local tmpsum 0
    :foreach i in=[find] do={:set tmpsum ($tmpsum + [get $i count])}
    :set   tmpsum [:tostr $tmpsum]
    :local lentmp [:len   $tmpsum]
    :return [:tonum [:pick $tmpsum ($lentmp - 3) $lentmp]]
}
:put ([$randomnum % 255])


The script must check if the new IP is the same IP as before: ~%2,5 of the possibility of the same number is too high...


This fail if ether1, for some reason, have multiple IPs
/ip address set [ find interface="ether1" ] address=$newIP
must be something like:
/ip address set [find where interface="ether1" and address=$oldIP] address=$newIP
and $oldIP is also used for check if is set the same previous IP...


For obtain easily a random number on v6, using the sum of tx-byte as base, this is more easy than all those calculations you put on script.
{
    :local total 0
    /interface
    :foreach item in=[find] do={
       :set total ($total + [get $item tx-byte])
    }
    :put ($total % 255)
}
Your method and this have the same identical probability to produce same number on two (or more) consecutive calls, if no tx happen...
 
jaclaz
Forum Veteran
Forum Veteran
Posts: 733
Joined: Tue Oct 03, 2023 4:21 pm

Re: Help me - make script change ip adress every rto

Thu Jan 18, 2024 11:36 pm

As a side note, I don't see a real need for a "random" IP, It seems that the only requisite is that the new IP is different from the old one.
If this is the case adding one to the last part (or last two parts) of the old address (and wrapping around to 1 when 255 is reached) will cycle through all possibile addresses without needing to check for collisions.
And if you run enough times the random generator it will before or later produce the sequence 1,2,3,4, ...254 so this sequence can also be called random, a predictable sort of random, of course.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 12032
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Help me - make script change ip adress every rto

Thu Jan 18, 2024 11:50 pm

If this is the case adding one to the last part (or last two parts) of the old address (and wrapping around to 1 when 255 is reached) will cycle through all possibile addresses without needing to check for collisions.

Yes, but is more easy write on this way
:local newIP "10.130.$[:rndnum from=0 to=255].1/17"

than read previous IP, add one with check for overflow ;)
{
# these two simulate the reading of the current values
:local previousIP  10.130.7.1
:local previousSub 27

:local newIP 0.0.0.0
:if ($previousIP = 10.130.255.1) do={:set newIP 10.130.0.1} else={:set newIP ($previousIP + 0x100)}
:put "$newIP/$previousSub"
}
 
jaclaz
Forum Veteran
Forum Veteran
Posts: 733
Joined: Tue Oct 03, 2023 4:21 pm

Re: Help me - make script change ip adress every rto

Fri Jan 19, 2024 1:01 am

Yep, an alternate, possibly even easier, approach would be to check if the part (let's call it $oldval ) is already 254, and if it is set it to 0.
Then generate the new random in the range from $oldval+1 to 254.
 
User avatar
anav
Forum Guru
Forum Guru
Posts: 19649
Joined: Sun Feb 18, 2018 11:28 pm
Location: Nova Scotia, Canada
Contact:

Re: Help me - make script change ip adress every rto

Fri Jan 19, 2024 1:04 am

So basically, the OP asked for a way to deceive the service providers mechanism to prevent abuse of his internet connection?
I thought it was a simple case like my own fibre dynamic IP provider, when the IP changes so does the gateway but the gateway used in my Routing Rules does not get updated,
and thus the need for a script in IP DHCP settings.

If the first statement is true, then IMHO, the entire post should removed by the moderator. Ethically speaking. :-)
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 12032
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Help me - make script change ip adress every rto

Fri Jan 19, 2024 1:04 am

Probably yes...
[…] because perhaps the traffic assigned to that address has run out.
To overcome this limit, he wants to deceive the service provider by changing the IP address to a different one.
[…]

But I think in the end it's the usual language problem that causes misunderstandings,
and the requirements poorly explained, as usual...
 
User avatar
anav
Forum Guru
Forum Guru
Posts: 19649
Joined: Sun Feb 18, 2018 11:28 pm
Location: Nova Scotia, Canada
Contact:

Re: Help me - make script change ip adress every rto

Fri Jan 19, 2024 1:08 am

Interesting, with my limited knowledge would never have seen that coming.
I certainly would have not posted after you noted the possibility.
I have reported the post as well. :-)
Hopefully, there is a clean explanation as the script is interesting nonetheless.
 
johndol
just joined
Topic Author
Posts: 2
Joined: Wed Jan 17, 2024 8:15 pm

Re: Help me - make script change ip adress every rto

Sun Apr 28, 2024 7:17 pm

If this is the case adding one to the last part (or last two parts) of the old address (and wrapping around to 1 when 255 is reached) will cycle through all possibile addresses without needing to check for collisions.

Yes, but is more easy write on this way
:local newIP "10.130.$[:rndnum from=0 to=255].1/17"

than read previous IP, add one with check for overflow ;)
{
# these two simulate the reading of the current values
:local previousIP  10.130.7.1
:local previousSub 27

:local newIP 0.0.0.0
:if ($previousIP = 10.130.255.1) do={:set newIP 10.130.0.1} else={:set newIP ($previousIP + 0x100)}
:put "$newIP/$previousSub"
}
thanks sir, i susscess make in ros7 for that script. The script is like this :
:local nameETH "ether1";
:local ping "8.8.8.8";
:local pingResult [/ping 8.8.8.8 count=3];
 if ($pingResult = 0 ) do={
:log error ("Interface $nameETH no conection");
:delay 1s;
:do {
:local newIP "10.139.$[:rndnum from=2 to=170].$[:rndnum from=2 to=250]/17";
/ip address set [ find interface="ether1" ] address=$newIP;
:log warning ("static IP changed");
:delay 1s;
:if ([/ping address=$ping interface="$nameETH" count=3] != 0) do={
:log warning ("Interface $nameETH sucsesfully conected");
};
but how i must change the script if i using ros 6 ?
please help me sir

Who is online

Users browsing this forum: GoogleOther [Bot] and 28 guests