Community discussions

MikroTik App
 
ramx2
just joined
Topic Author
Posts: 20
Joined: Mon Jun 01, 2015 3:21 pm

Script for public ip detection

Mon Jun 01, 2015 3:39 pm

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.
 
efaden
Forum Guru
Forum Guru
Posts: 1711
Joined: Sat Mar 30, 2013 1:55 am
Location: New York, USA

Re: Script for public ip detection

Mon Jun 01, 2015 6:51 pm

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 ??
 
ramx2
just joined
Topic Author
Posts: 20
Joined: Mon Jun 01, 2015 3:21 pm

Re: Script for public ip detection

Mon Jun 01, 2015 7:47 pm

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
 
efaden
Forum Guru
Forum Guru
Posts: 1711
Joined: Sat Mar 30, 2013 1:55 am
Location: New York, USA

Re: Script for public ip detection

Tue Jun 02, 2015 12:51 pm

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.
 
ramx2
just joined
Topic Author
Posts: 20
Joined: Mon Jun 01, 2015 3:21 pm

Re: Script for public ip detection

Tue Jun 02, 2015 2:47 pm

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.
You do not have the required permissions to view the files attached to this post.
 
ramx2
just joined
Topic Author
Posts: 20
Joined: Mon Jun 01, 2015 3:21 pm

Re: Script for public ip detection

Tue Jun 02, 2015 3:09 pm

Here's a photo showing you the fake pppoe ip..
You do not have the required permissions to view the files attached to this post.
 
efaden
Forum Guru
Forum Guru
Posts: 1711
Joined: Sat Mar 30, 2013 1:55 am
Location: New York, USA

Re: Script for public ip detection

Tue Jun 02, 2015 4:44 pm

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..
 
ramx2
just joined
Topic Author
Posts: 20
Joined: Mon Jun 01, 2015 3:21 pm

Re: Script for public ip detection

Tue Jun 02, 2015 7:51 pm

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...
 
efaden
Forum Guru
Forum Guru
Posts: 1711
Joined: Sat Mar 30, 2013 1:55 am
Location: New York, USA

Re: Script for public ip detection

Tue Jun 02, 2015 8:42 pm

I suppose where I am confused is that if you have a fake IP does your internet still work?
 
ramx2
just joined
Topic Author
Posts: 20
Joined: Mon Jun 01, 2015 3:21 pm

Re: Script for public ip detection

Tue Jun 02, 2015 8:51 pm

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 ; )
 
efaden
Forum Guru
Forum Guru
Posts: 1711
Joined: Sat Mar 30, 2013 1:55 am
Location: New York, USA

Re: Script for public ip detection

Tue Jun 02, 2015 8:54 pm

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
 
efaden
Forum Guru
Forum Guru
Posts: 1711
Joined: Sat Mar 30, 2013 1:55 am
Location: New York, USA

Re: Script for public ip detection

Tue Jun 02, 2015 8:59 pm

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
 
ramx2
just joined
Topic Author
Posts: 20
Joined: Mon Jun 01, 2015 3:21 pm

Re: Script for public ip detection

Tue Jun 02, 2015 9:17 pm

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..
You do not have the required permissions to view the files attached to this post.
 
efaden
Forum Guru
Forum Guru
Posts: 1711
Joined: Sat Mar 30, 2013 1:55 am
Location: New York, USA

Re: Script for public ip detection

Tue Jun 02, 2015 9:21 pm

I don't use PPPoE.... I need a console command I can trigger.
 
ramx2
just joined
Topic Author
Posts: 20
Joined: Mon Jun 01, 2015 3:21 pm

Re: Script for public ip detection

Tue Jun 02, 2015 9:35 pm

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
 
efaden
Forum Guru
Forum Guru
Posts: 1711
Joined: Sat Mar 30, 2013 1:55 am
Location: New York, USA

Re: Script for public ip detection

Tue Jun 02, 2015 9:48 pm

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))
 
efaden
Forum Guru
Forum Guru
Posts: 1711
Joined: Sat Mar 30, 2013 1:55 am
Location: New York, USA

Re: Script for public ip detection

Wed Jun 03, 2015 3:57 am

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))
}
 
ramx2
just joined
Topic Author
Posts: 20
Joined: Mon Jun 01, 2015 3:21 pm

Re: Script for public ip detection

Wed Sep 02, 2015 1:19 pm

I'm going to try you script, but I have to change the > local currentLocalSiteInterface to pppoe-DialUp and test it..
Last edited by ramx2 on Fri Sep 11, 2015 8:39 am, edited 1 time in total.
 
ramx2
just joined
Topic Author
Posts: 20
Joined: Mon Jun 01, 2015 3:21 pm

Re: Script for public ip detection

Thu Sep 10, 2015 8:50 pm

Efaden,

The script isn't doing anything... Is there a way to fix it properly ?
 
efaden
Forum Guru
Forum Guru
Posts: 1711
Joined: Sat Mar 30, 2013 1:55 am
Location: New York, USA

Re: Script for public ip detection

Fri Sep 11, 2015 2:42 am

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.
 
ramx2
just joined
Topic Author
Posts: 20
Joined: Mon Jun 01, 2015 3:21 pm

Re: Script for public ip detection

Fri Sep 11, 2015 8:53 am

I don't mind at all, actually I'd be grateful.. The problem if the router is connected from a fake IP you won't be able to access it.. This is why your script comes to help here to make sure if the router is connected from a fake IP it will disconnect and try to get a public IP.. Anyway the best thing to do is to use Teamviewer in that case you won't be kicked out.

Let me know when you're free so we could sort it out.
 
efaden
Forum Guru
Forum Guru
Posts: 1711
Joined: Sat Mar 30, 2013 1:55 am
Location: New York, USA

Re: Script for public ip detection

Fri Sep 11, 2015 1:13 pm

I don't mind at all, actually I'd be grateful.. The problem if the router is connected from a fake IP you won't be able to access it.. This is why your script comes to help here to make sure if the router is connected from a fake IP it will disconnect and try to get a public IP.. Anyway the best thing to do is to use Teamviewer in that case you won't be kicked out.

Let me know when you're free so we could sort it out.
Shoot me an email and we can coordinate. Efaden at Gmail.

Sent from my SCH-I545 using Tapatalk
 
User avatar
chechito
Forum Guru
Forum Guru
Posts: 1772
Joined: Sun Aug 24, 2014 3:14 am
Location: Bogota Colombia
Contact:

Re: Script for public ip detection

Fri Sep 11, 2015 5:21 pm

make a vpn to access remotely to the device
 
ramx2
just joined
Topic Author
Posts: 20
Joined: Mon Jun 01, 2015 3:21 pm

Re: Script for public ip detection

Fri Sep 11, 2015 5:34 pm

VPN already configured :) but you can't access it without Real/Public IP
 
User avatar
chechito
Forum Guru
Forum Guru
Posts: 1772
Joined: Sun Aug 24, 2014 3:14 am
Location: Bogota Colombia
Contact:

Re: Script for public ip detection

Fri Sep 11, 2015 5:46 pm

VPN already configured :) but you can't access it without Real/Public IP

you can access knowing the public ip to it is nated, you can use ip cloud to know that public ip.

with scripts you can update local and remote ip on vpn endpoints

shedule a local ping to remote lan ip to trigger vpn establishment
 
efaden
Forum Guru
Forum Guru
Posts: 1711
Joined: Sat Mar 30, 2013 1:55 am
Location: New York, USA

Re: Script for public ip detection

Sun Sep 13, 2015 2:35 am

Worked through it via TeamViewer. For those interested... final version.
#
# 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=$privateRanges 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
            :log error "Matched - $privateRange"
         } 
      }
      :set currentLoop ($currentLoop + 1)
  
      :if ($hadMatch) do={
        :delay 5
      }
   } while (($currentLoop < $loopCount) && ($hadMatch))  
}
 
ramx2
just joined
Topic Author
Posts: 20
Joined: Mon Jun 01, 2015 3:21 pm

Re: Script for public ip detection

Sun Sep 13, 2015 3:02 am

Efaden did a great job.
Thank you mate :D
 
User avatar
saintofinternet
Forum Veteran
Forum Veteran
Posts: 761
Joined: Thu Oct 15, 2009 3:52 am

Re: Script for public ip detection

Tue Feb 06, 2018 5:40 am

is that script working perfectly??
Worked through it via TeamViewer. For those interested... final version.
#
# 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=$privateRanges 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
            :log error "Matched - $privateRange"
         } 
      }
      :set currentLoop ($currentLoop + 1)
  
      :if ($hadMatch) do={
        :delay 5
      }
   } while (($currentLoop < $loopCount) && ($hadMatch))  
}
by professionals, for professionals....
Don't forget to give KARMA!!!
 
cicserver
Member
Member
Posts: 303
Joined: Sun Jul 24, 2011 12:04 pm

Re: Script for public ip detection

Tue Feb 06, 2018 1:19 pm

I used this one with some modification & it worked well :) ,
 
User avatar
saintofinternet
Forum Veteran
Forum Veteran
Posts: 761
Joined: Thu Oct 15, 2009 3:52 am

Re: Script for public ip detection

Thu Feb 08, 2018 3:33 am

I used this one with some modification & it worked well :) ,
could you kindly help with what modification you did? as most of the lines are commented and i am not so sure about which to uncomment.
by professionals, for professionals....
Don't forget to give KARMA!!!
 
ramx2
just joined
Topic Author
Posts: 20
Joined: Mon Jun 01, 2015 3:21 pm

Re: Script for public ip detection

Mon Aug 20, 2018 11:05 pm

Well the original script isn't working with the new firmwares, maybe someone here could fix it.
 
ramx2
just joined
Topic Author
Posts: 20
Joined: Mon Jun 01, 2015 3:21 pm

Re: Script for public ip detection

Mon Aug 20, 2018 11:12 pm

I used this one with some modification & it worked well :) ,
could you kindly help with what modification you did? as most of the lines are commented and i am not so sure about which to uncomment.


Well here's a working script, just remember to change the WAN interface to match your WAN interface name. As you can see mine is called : pppoe-Dial-Up
You have also to add a schedule to run this script repeatedly.




# Script to find if wan link have private ip and act accordingly,
# Tested with Mikrotik ROS 5.x & 6.x versions
# 19-APR-2016 / Syed Jahanzaib
 
# Set your WAN Interface name , i have added pppoe-Dial-Up , change it as required
:local WANINTERFACE
:set WANINTERFACE pppoe-Dial-Up
 
# Find Public IP from pppoe-Dial-Up interface & cut subnet
:local WANIP [/ip address get [find where interface=$WANINTERFACE] address];
:set WANIP [:pick $WANIP 0 ([:len $WANIP]-3) ];
 
# Match if IP address starts with private address 10.*
:if ($WANIP ~"^[0-9 ]*10") do={
:log warning "Private ip address found !!!"
# Set your action here , like Re-Connect the pppoe-link
/interface pppoe-client enable pppoe-Dial-Up
:delay 1
} else={
 
# Match if IP address starts with private address 172.*
:if ($WANIP ~"^[0-9 ]*172") do={
:log warning "Private ip address found !!!"
# Set your action here , like Re-Connect the pppoe-link
/interface pppoe-client enable pppoe-Dial-Up
:delay 1
} else={
 
# Match if IP address starts with private address 192.*
:if ($WANIP ~"^[0-9 ]*192") do={
:log warning "Private ip address found !!!"
# Set your action here , like Re-Connect the pppoe-link
/interface pppoe-client enable pppoe-Dial-Up
:delay 1
} else={

# If above statement do not match, then consider it a public ip and take no action, just log : ~ )
:log warning "Public IP - $WANIP - Found, OK ! No action required"
# OR Set your desire action here if required
}
}
}
# Script Ends Here ...

Who is online

Users browsing this forum: No registered users and 33 guests