Community discussions

MikroTik App
 
cicserver
Member
Member
Topic Author
Posts: 303
Joined: Sun Jul 24, 2011 12:04 pm

Script required for change wan ip

Fri Jan 02, 2015 1:36 pm

I have a 4 public ips available, e.g: 1.1.1.1-1.1.1.4
I have configured only 1 wan ip on mikrotik wan interface i.e 1.1.1.1
Now I have a problem that my ISP some times block my wan ip address for one hour dueto some grey traffic passes sometimes from my LAN which is a requirement for my users. (this check runs hourly on my isp side)
Now I want to setup a script that can change IP if internet stops working on 1.1.1.1, then it should move 1.1.1.2, if 2 also gets block, then move 3 and then to 4 , then back to 1 (like schedule ever minute to check)
 
jarda
Forum Guru
Forum Guru
Posts: 7756
Joined: Mon Oct 22, 2012 4:46 pm

Re: Script required for change wan ip

Fri Jan 02, 2015 2:06 pm

Ok. And what you already did for that?
 
cicserver
Member
Member
Topic Author
Posts: 303
Joined: Sun Jul 24, 2011 12:04 pm

Re: Script required for change wan ip

Mon Jan 05, 2015 7:32 am

Ok. And what you already did for that?
i am using a script (from blog) which can change ip to second only. example is here: BUT THE ISSUE IS IT CAN CHANGE IP TO 2ND IP ONLY, I HAVE /29 POOL AVAILABLE I WANT TO JUMP TO SECOND ONE 3RD ONE AND SO ON.

i am using a script (from blog) which can change ip to second only. example is here:
[# Modified few contents to suite local requirements and added descriptions
# Script Starts here...
# Internet Host to be checked You can modify them as per required, JZ
:local host1   "8.8.8.8"
:local host2   "208.67.222.123"
 # Do not modify data below without proper understanding.
:local i 0;
:local F 0;
:local date;
:local time;
:global InternetStatus;
:global InternetLastChange;
 # PING each host 5 times
:for i from=1 to=5 do={
if ([/ping $host1 count=1]=0) do={:set F ($F + 1)}
if ([/ping $host2 count=1]=0) do={:set F ($F + 1)}
:delay 1;
};
# If both links are down and all replies are timedout, then link is considered down
:if (($F=10)) do={
:if (($InternetStatus="UP")) do={
:log error "WARNING : The INTERNET link seems to be DOWN. Please Check";
:set InternetStatus "DOWN";
##      ADD YOUR RULES HERE, LIKE ROUTE CHANGE OR WHAT EVER IS REQUIRED, Example is below ...
:log error "ISP LINK SEEMS TO BE DOWN, Changing IP Address /jz"
/ip add disable [find comment="ether1_wan_ip_1"]
/ip add enable [find comment="ether1_wan_ip_2"]
#/sys script run siren
:delay 10
:set date [/system clock get date];
:set time [/system clock get time];
:set InternetLastChange ($time . " " . $date);
} else={:set InternetStatus "DOWN";}
} else={
##      If reply is received , then consider the Link is UP
:if (($InternetStatus="DOWN")) do={
:log warning "WARNING :The INTERNET link have been restored";
:set InternetStatus "UP";
/ip add disable [find comment="ether1_wan_ip_2"]
/ip add enable [find comment="ether1_wan_ip_1"]
##      ADD YOUR RULES HERE, LIKE ROUTE CHANGE OR WHAT EVER IS REQUIRED, Example is below ...
:local currentIP
:local externalInterface "ether1"
# get the current IP address from the external interface
:set currentIP [/ip address get [find interface="$externalInterface"] address]
# Strip netmask
:for i from=( [:len $currentIP] - 1) to=0 step=-1 do={
:if ( [:pick $currentIP $i] = "/") do={
:set currentIP [:pick $currentIP 0 $i]
:log warning "ISP LINK RE - CONNECTED with new WAN IP = $currentIP, Please check and confirm /jz"
}
}
:set date [/system clock get date];
:set time [/system clock get time];
:set InternetLastChange ($time . " " . $date);
} else={:set InternetStatus "UP";}
}
# Script Ends Here.
# Thank you
 
User avatar
skot
Long time Member
Long time Member
Posts: 584
Joined: Wed Nov 30, 2011 3:05 am

Re: Script required for change wan ip

Thu Jan 08, 2015 1:41 am

In the script, replace
/ip add disable [find comment="ether1_wan_ip_1"]
/ip add enable [find comment="ether1_wan_ip_2"]
and
/ip add disable [find comment="ether1_wan_ip_2"]
/ip add enable [find comment="ether1_wan_ip_1"]
with this (in both spots)
/ip address
:if ([get [find comment="ether1_wan_ip_1"] disabled] = false) do={
  disable [find comment="ether1_wan_ip_1"]
  enable [find comment="ether1_wan_ip_2"]
} else={
  :if ([get [find comment="ether1_wan_ip_2"] disabled] = false) do={
    disable [find comment="ether1_wan_ip_2"]
    enable [find comment="ether1_wan_ip_3"]
  } else={
    :if ([get [find comment="ether1_wan_ip_3"] disabled] = false) do={
      disable [find comment="ether1_wan_ip_3"]
      enable [find comment="ether1_wan_ip_4"]
    } else={
      :if ([get [find comment="ether1_wan_ip_4"] disabled] = false) do={
        disable [find comment="ether1_wan_ip_4"]
        enable [find comment="ether1_wan_ip_1"]
      }
    }
  }
}
Then, instead of toggling back and forth, it will loop through all 4.
 
cicserver
Member
Member
Topic Author
Posts: 303
Joined: Sun Jul 24, 2011 12:04 pm

Re: Script required for change wan ip

Fri Jan 09, 2015 8:03 am

Thank you @skot

But the problem is that after applying what you posted. when internet is down , it changes ip to second one, but if the 2nd ip is also not working then it dont jump to 3rd or 4th,
in short, if the net is down, it simply changes ip to second one, but if the second ip is also not working , it dont jump to 3rd or 4th. it remain stuck at 2nd ip (may be due to status down variable setting)

How to solve it ? I want that if 2nd is also not working, then it should mvoe to 3rd or 4th whatever i working.

Current code is attached below.
# Script Starts here...
# Internet Host to be checked You can modify them as per required, JZ
:local host1   "8.8.8.8"
:local host2   "208.67.222.123"
# Do not modify data below without proper understanding.
:local i 0;
:local F 0;
:local date;
:local time;
:global InternetStatus;
:global InternetLastChange;
# PING each host 5 times
:for i from=1 to=2 do={
if ([/ping $host1 count=1]=0) do={:set F ($F + 1)}
if ([/ping $host2 count=1]=0) do={:set F ($F + 1)}
};
# If both links are down and all replies are timedout, then link is considered down
:if (($F=4)) do={
:if (($InternetStatus="UP")) do={
:log error "WARNING : The INTERNET link seems to be DOWN. Please Check";
:set InternetStatus "DOWN";
##  ADD YOUR RULES HERE, LIKE ROUTE CHANGE OR WHAT EVER IS REQUIRED, Example is below ...
:log error "ISP LINK SEEMS TO BE DOWN, Changing IP Address /jz"
/ip address
:if ([get [find comment="ether1_wan_ip_1"] disabled] = false) do={
  disable [find comment="ether1_wan_ip_1"]
  enable [find comment="ether1_wan_ip_2"]
} else={
  :if ([get [find comment="ether1_wan_ip_2"] disabled] = false) do={
    disable [find comment="ether1_wan_ip_2"]
    enable [find comment="ether1_wan_ip_3"]
  } else={
    :if ([get [find comment="ether1_wan_ip_3"] disabled] = false) do={
      disable [find comment="ether1_wan_ip_3"]
      enable [find comment="ether1_wan_ip_4"]
    } else={
      :if ([get [find comment="ether1_wan_ip_4"] disabled] = false) do={
        disable [find comment="ether1_wan_ip_4"]
        enable [find comment="ether1_wan_ip_1"]
      }
    }
  }
}
:set date [/system clock get date];
:set time [/system clock get time];
:set InternetLastChange ($time . " " . $date);
} else={:set InternetStatus "DOWN";}
} else={
##  If reply is received , then consider the Link is UP
:if (($InternetStatus="DOWN")) do={
:log warning "WARNING :The INTERNET link have been restored";
:set InternetStatus "UP";

/ip address
:if ([get [find comment="ether1_wan_ip_1"] disabled] = false) do={
  enable [find comment="ether1_wan_ip_1"]
  disable [find comment="ether1_wan_ip_2"]
} else={
  :if ([get [find comment="ether1_wan_ip_2"] disabled] = false) do={
    enable [find comment="ether1_wan_ip_2"]
    disable [find comment="ether1_wan_ip_3"]
  } else={
    :if ([get [find comment="ether1_wan_ip_3"] disabled] = false) do={
      enable [find comment="ether1_wan_ip_3"]
      disable [find comment="ether1_wan_ip_4"]
    } else={
      :if ([get [find comment="ether1_wan_ip_4"] disabled] = false) do={
        enable [find comment="ether1_wan_ip_4"]
        disable [find comment="ether1_wan_ip_1"]
      }
    }
  }
}

##  ADD YOUR RULES HERE, LIKE ROUTE CHANGE OR WHAT EVER IS REQUIRED, Example is below ...
:local currentIP
:local externalInterface "ether1"
# get the current IP address from the external interface
:set currentIP [/ip address get [find interface="$externalInterface"] address]
# Strip netmask
:for i from=( [:len $currentIP] - 1) to=0 step=-1 do={
:if ( [:pick $currentIP $i] = "/") do={
:set currentIP [:pick $currentIP 0 $i]
:log warning "ISP LINK RE - CONNECTED with new WAN IP = $currentIP, Please check and confirm /jz"
}
}
:set date [/system clock get date];
:set time [/system clock get time];
:set InternetLastChange ($time . " " . $date);
} else={:set InternetStatus "UP";}
}
# Script Ends Here.
# Thank you
 
User avatar
aacable
Member
Member
Posts: 435
Joined: Wed Sep 17, 2008 11:58 am
Location: ISLAMIC Republic of PAKISTAN
Contact:

Re: Script required for change wan ip

Fri Jan 09, 2015 12:56 pm

@cicserver
the solution you are looking for is not very mature. Either you should try some tunneling to hide the traffic so that ISP cannot detect it, or contact ISP for some proper solution.

In the meanwhile try this.Following script is for for 8 Public IP's.
# Script Starts here ... Tested with Mikrotik 6.xx ...
# Internet Host to be checked You can modify them as per required, JZ
# Setting various variables to be used later in this script

# Host to be monitor, like google dns and opends servers
:local host1   "8.8.8.8"
:local host2   "208.67.222.123"
:global InternetStatus;
:global InternetLastChange;
:global gmailsmtp
:set gmailsmtp [:resolve "smtp.gmail.com"];

# Set your Gmail ID and Password
:global sendermail YOUR_GMAIL_ID@gmail.com
:global gmailpass YOUR_GMAIL_PASSWORD

# Do not modify data below without proper understanding.
:local i 0;
:local F 0;
:local date;
:local time;

# PING each host 2 times
:for i from=1 to=2 do={
if ([/ping $host1 count=1]=0) do={:set F ($F + 1)}
if ([/ping $host2 count=1]=0) do={:set F ($F + 1)}
};

# If both links are down and all replies are timeout, then link is considered down
:if (($F=4)) do={

##  ADD YOUR RULES HERE, LIKE ROUTE CHANGE OR WHAT EVER IS REQUIRED, Example is below ...
:log error "ISP LINK SEEMS TO BE DOWN, Changing IP Address /jz"

:set InternetStatus "DOWN";
:set date [/system clock get date];
:set time [/system clock get time];
:set InternetLastChange ($time . " " . $date);
/ip address

:if ([get [find comment="ether1_wan_ip_1"] disabled] = false) do={
  disable [find comment="ether1_wan_ip_1"]
  enable [find comment="ether1_wan_ip_2"]

} else={
  :if ([get [find comment="ether1_wan_ip_2"] disabled] = false) do={
    disable [find comment="ether1_wan_ip_2"]
    enable [find comment="ether1_wan_ip_3"]

  } else={
    :if ([get [find comment="ether1_wan_ip_3"] disabled] = false) do={
      disable [find comment="ether1_wan_ip_3"]
      enable [find comment="ether1_wan_ip_4"]

    } else={
      :if ([get [find comment="ether1_wan_ip_4"] disabled] = false) do={
        disable [find comment="ether1_wan_ip_4"]
        enable [find comment="ether1_wan_ip_5"]

    } else={
      :if ([get [find comment="ether1_wan_ip_5"] disabled] = false) do={
        disable [find comment="ether1_wan_ip_5"]
        enable [find comment="ether1_wan_ip_6"]

    } else={
      :if ([get [find comment="ether1_wan_ip_6"] disabled] = false) do={
        disable [find comment="ether1_wan_ip_6"]
        enable [find comment="ether1_wan_ip_7"]

    } else={
      :if ([get [find comment="ether1_wan_ip_7"] disabled] = false) do={
        disable [find comment="ether1_wan_ip_7"]
        enable [find comment="ether1_wan_ip_8"]

    } else={
      :if ([get [find comment="ether1_wan_ip_8"] disabled] = false) do={
        disable [find comment="ether1_wan_ip_8"]
        enable [find comment="ether1_wan_ip_1"]

      }
    }
   }
 }
}
}
}
}
#};
} else={

##      If reply is received , then consider the Link is UP
:if (($InternetStatus="DOWN")) do={
:log warning "WARNING :The INTERNET link have been restored";
:set date [/system clock get date];
:set time [/system clock get time];
:set InternetLastChange ($time . " " . $date);
:set InternetStatus "UP";
:global wanip

# Fetch your PUBLIC ip address from dnsomatic so that its info can be used at required session for info purposes ...
/tool fetch url="http://myip.dnsomatic.com/" mode=http dst-path=mypublicip.txt
local ip [file get mypublicip.txt contents ]
:set wanip "$ip";
:log warning "Internet Monitor by ZAIB Report: Internet seems to be UP with ISP_NAME ip   $wanip"

/tool e-mail send to="RECEVER_EMAIL@gmail.com" password=$gmailpass subject="ISP_NAME Link or IP was DOWN. Now new WAN IP address is $wanip" from=$sendermail server=$gmailsmtp start-tls=yes body="ISP_NAME Link or IP was DOWN. Now new WAN IP address is $wanip *******  Regard's > >>>>> Syed Jahanzaib aacable@hotmail.com ****** "
}
}
}
}
}
}
}
}
:D
 
cicserver
Member
Member
Topic Author
Posts: 303
Joined: Sun Jul 24, 2011 12:04 pm

Re: Script required for change wan ip

Sat Jan 10, 2015 8:49 am

@cicserver
the solution you are looking for is not very mature. Either you should try some tunneling to hide the traffic so that ISP cannot detect it, or contact ISP for some proper solution.
In the meanwhile try this.Following script is for for 8 Public IP's. :D
Its working like a charm :) thanks @aacable & @skot :D
 
mahmadm
just joined
Posts: 2
Joined: Mon Jan 12, 2015 8:59 am

Re: Script required for change wan ip

Mon Jan 12, 2015 3:53 pm

Hello Everybody,

I am new to Mikrotik and forum. My job is to configure Mikrotik with my controller. Right now I investigating telnet (CLI) verses C API to do that. I am inclined towards C API but stuck to get security setting using C API. For example I couldn't able get equivalent API sentence for following CLI command

/int w sec print

Will you please give an expert opinion of using C API instead of telnet for the job and equivalent of above CLI command.

I am new to forum and not able to post my queries so I am posting here.

Best Regards,
Mushtaq
 
mahmadm
just joined
Posts: 2
Joined: Mon Jan 12, 2015 8:59 am

Re: Script required for change wan ip

Tue Jan 13, 2015 12:23 pm

After searchibg through the forum posts I am able to find the equivalent sentence as under
/int/w/security-profiles/print
Now I am able get all parameters value using C api sentence but not able to set most of them.
For example
<<< /interface/set
<<< =disabled=no
<<< =.id=wlan1
<<< 

>>> !done
works. But following code doesn't
<<< /interface/set
<<< =ssid=mikro_test
<<< =.id=wlan1
<<< 

>>> !trap
>>> =message=unknown parameter
Any idea what I am missing?
 
User avatar
skot
Long time Member
Long time Member
Posts: 584
Joined: Wed Nov 30, 2011 3:05 am

Re: Script required for change wan ip

Wed Jan 14, 2015 6:31 pm

You need to do something like this:
/interface/wireless/set
=ssid=mikro_test
=.id=wlan1

Who is online

Users browsing this forum: No registered users and 67 guests