Script for public ip detection

Due to a higher number of customers on our ISP (public IP’s are not enough, so they had to NAT) I can’t connect to my Mikrotik, unless I disconnect and reconnect couple times to get a public IP again.

Is there a script that determine public IP range, and when it detects a fake range like 172.x.x.x or 10.x.x.x it disconnects and reconnects by itself with at least 10 retries until it get a public IP?

Please excuse my ignorance, but tried to look for such script before posting but with no avail.

Help appreciated,

Thanks.

I don’t have anything specifically for that… but I could write one using something I already have pretty easily…

How exactly do you want to run it? E.g. ever minute and have the script just check once… or ??? Run it every 10 minutes etc?..

I’d recommend just a script to run every minute… if it detects a bad ip release and renew the interface…

What ranges would the fake IP be in?.. would it always be 192. or ??

If you think the script should run for every minutes there’s no harm in that, whilst it’s enough to detect the IP upon initiating the PPPeo connection for one time.

Fake IP starts by 192.x.x.x - 172.x.x.x - 10.x.x.x I’m sure we can add more to the script if I see more IP range not listed. But plz take into consideration adding the retries to detect the IP while connecting and disconnecting if I always get a fake IP, I need the router stop running the script otherwise I stay without internet if all the Public IP’s are taken..

If you have anything on your mind please share, I might missed something.

Thank you in advance efaden

I’m not sure I entirely understand what your doing I guess…

You want it to check the IP to see if it is private and renew the DHCP if it is private, but only sometimes?.. how do I know when to renew it?

I can write a script pretty quickly… but I need to understand the problem and exactly how you want the script to work.

What I need is a script to renew the pppoe ip address if it’s fake. Usually, I click “Reconnect” to let the IP get renewed. Sometimes I get lucky I get a public ip instantly, or I need to click couple times because I’d be connected from fake ip range..


The attachment shows you that I’m connected from Public IP. Hopefully, I explained correctly.

Here’s a photo showing you the fake pppoe ip..

Explain this…

But plz take into consideration adding the retries to detect the IP while connecting and disconnecting if I always get a fake IP, I need the router stop running the script otherwise I stay without internet if all the Public IP’s are taken..

1 * Lets say the script detected a fake IP, it should disconnect then reconnect trying to gain a Public IP. (This would be the first attempt)

2 * Lets say now the connection established but still with fake IP, your script will detect the fake IP then disconnects again and reconnect.. This would be the second attempt, and so on…

3* If it succeed to get a public IP, connection should remain established until further reboot or disconnection..


4* After several consecutive failure attempts to detect a Public IP (Your script is programmed to try 10 times) The script should reconnect and stop operating (it doesn’t matter here if it’s public or fake) .. Because if it kept on trying without getting a public IP, I wont be able to connect to the internet due to the loop of connecting and disconnecting.

You can schedule the script to run again after 1 hour to start it detection…

I suppose where I am confused is that if you have a fake IP does your internet still work?

Sure, the internet will remain working..

This is the whole issue is when I get a fake IP from my ISP I won’t be able reach the Mikrotik.. Here where your script will do it job to detect the fake ip and try to get a public one instead ; )

Ok… I get it now… I’ll try to write something to cover it… Give me a day to think about the best way… it will likely take me a few days to a week or two to actually get the time to sit down and write it.

-ERic

Also… how are you triggering the reconnect?.. are you just doing a dhcp-client renew? or what command are you using? I need to know what command to call to retrigger it.

-Eric

Just to be clear, I have nothing to do with the local network dhcp.. All the work should be done on the PPPeO client interface.

Disabling then re-enabling the PPPeO client interface would work with you or click “Reconnect” from the Quick Set tab as per the attached photo..

I don’t use PPPoE… I need a console command I can trigger.

I guess ( interface pppoe-client disable …) I’m not sure if this will trigger it..


[admin@RB751G-2HnD] /interface> pppoe-client
[admin@RB751G-2HnD] /interface pppoe-client>
[admin@RB751G-2HnD] /interface pppoe-client>

.. – go up to interface
add – Create a new item
comment – Set comment for items
disable – Disable items
edit –
enable – Enable items
export – Print or save an export script that can be used to restore configuration
find – Find items by value
get – Gets value of item’s property
monitor – Monitor interface status
print – Print values of item properties
remove – Remove item
scan –
set – Change item properties

Untested… but I think this is close…

#
# Variables
#
:local currentLocalSiteInterface "ether01-gateway"
:local privateRanges "192.168.0.0/16,172.16.0.0/12,10.0.0.0/8" 
:local loopCount 10

#
# Script
#
:local hadMatch
:local currentLoop 0

do {
	:set hadMatch false 
	:local currentLocalSite [/ip dhcp-client get [/ip dhcp-client find interface=$currentLocalSiteInterface] address]
	:local currentLocalSiteAddress ([:pick $currentLocalSite 0 [:find $currentLocalSite "/"]] & 
		(255.255.255.255 << (32 - [:pick $currentLocalSite ([:find $currentLocalSite "/"] + 1) [:len $currentLocalSite]])) 

	:foreach privateRange in=[:toarray $list ] do={ 
		:if (([:pick $privateRange 0 [:find $privateRange "/"]] & 
			(255.255.255.255 << (32 - [:pick $privateRange ([:find $privateRange "/"] + 1) [:len $privateRange]]))) = 
			($currentLocalSiteAddress & (255.255.255.255 << (32 - [:pick $privateRange ([:find $privateRange "/"] + 1) [:len $privateRange]])))) do={ 

			/interface disable $currentLocalSiteInterface
			/interface enable $currentLocalSiteInterface

			:set hadMatch true
			:set currentLoop ($currentLoop + 1)
		} 
	} 
} while (($currentLoop < $loopCount) && ($hadMatch))

This is closer… I don’t really have much more time to work on this… you should be able to make this work though… I also don’t have a pppoe-server to test with…

#
# Variables
#
:local currentLocalSiteInterface "pppoe-out1"
:local privateRanges "192.168.0.0/16,172.16.0.0/12,10.0.0.0/8" 
:local loopCount 10

#
# Script
#
:local hadMatch
:local currentLoop 0

:if ([/interface pppoe-client get [/interface pppoe-client find name=$currentLocalSiteInterface] running]) do={
	do {
		:set hadMatch false 
		:local currentLocalSite [/ip address get [/ip address find interface=$currentLocalSiteInterface] address]
		:local currentLocalSiteAddress ([:pick $currentLocalSite 0 [:find $currentLocalSite "/"]] & 
			(255.255.255.255 << (32 - [:pick $currentLocalSite ([:find $currentLocalSite "/"] + 1) [:len $currentLocalSite]])) 

		:foreach privateRange in=[:toarray $list ] do={ 
			:if (([:pick $privateRange 0 [:find $privateRange "/"]] & 
				(255.255.255.255 << (32 - [:pick $privateRange ([:find $privateRange "/"] + 1) [:len $privateRange]]))) = 
				($currentLocalSiteAddress & (255.255.255.255 << (32 - [:pick $privateRange ([:find $privateRange "/"] + 1) [:len $privateRange]])))) do={ 

				/interface disable $currentLocalSiteInterface
				/interface enable $currentLocalSiteInterface

				:set hadMatch true
				:set currentLoop ($currentLoop + 1)
			} 
		} 
	} while (($currentLoop < $loopCount) && ($hadMatch))
}

I’m going to try you script, but I have to change the > local currentLocalSiteInterface to pppoe-DialUp and test it..

Efaden,

The script isn’t doing anything… Is there a way to fix it properly ?

I don’t really have a setup to test and debug. If you want to give me access to your box I can try to debug it live. But it’s just going to require some troubleshooting.