Community discussions

MikroTik App
 
kotsius
just joined
Topic Author
Posts: 16
Joined: Mon Jan 19, 2009 9:58 am

Dynamic IP address update script for multiple VPN clients

Tue Mar 12, 2013 2:32 pm

Hi all!

The following script adaptation checks the connect-to address of up to 5 PPTP clients in one go. It compares each IP to the corresponding IP address returned by the DNS server and, when the two do not match, it updates the VPN client and the log. The log is also informed upon failure to resolve the host name. (I happen to use DynDNS, hence the log text message.) As the script is not resource-intensive, it should be able to run every minute without problems.
#  REQUIRES DNS TO BE SET UP ON MIKROTIK

:local dnHost1 "~~~~~e.g._no1_host.dyndns.org~~~~~"
:local dnHost2 "~~~~~e.g._no2_host.dyndns.org~~~~~"
:local dnHost3 "~~~~~e.g._no3_host.dyndns.org~~~~~"
:local dnHost4 0
:local dnHost5 0
:local inFace1 "~~~~~e.g._no1_pptp_client_interface_name~~~~~"
:local inFace2 "~~~~~e.g._no2_pptp_client_interface_name~~~~~"
:local inFace3 "~~~~~e.g._no3_pptp_client_interface_name~~~~~"
:local inFace4 0
:local inFace5 0

# ALL PRECEDING ENTRIES MUST BE REPLACED BY ACTUAL DATA INSIDE DOUBLE QUOTES OR ZEROS

:local aa
:local dnHosts [:toarray ($dnHost1,$dnHost2,$dnHost3,$dnHost4,$dnHost5)]
:local inFaces [:toarray ($inFace1,$inFace2,$inFace3,$inFace4,$inFace5)]
:local ipNowIs
:for pp from=0 to=4 do={
	:set dnHost1 [:pick $dnHosts $pp]
	:set inFace1 [:pick $inFaces $pp]
	:if ( ($dnHost1!=0) && ($inFace1!=0) ) do={
		:set ipNowIs [:resolve $dnHost1]
		:if ( [:typeof $ipNowIs]!="ip" ) do={
			:log info "••• $inFace1: Failed to resolve DynDNS host name."
		} else={
			:set aa [/interface pptp-client find name=$inFace1]
			:if ( [/interface pptp-client get $aa connect-to]!=$ipNowIs ) do={
				/interface pptp-client set $aa connect-to=$ipNowIs
				:log info "••• $inFace1: Remote address updated to $ipNowIs."
} } } }
I hope that others find this script helpful. All comments and ideas for improvement are most welcome.
 

Who is online

Users browsing this forum: Google [Bot], Jonty and 36 guests