Community discussions

MikroTik App
 
psymax
just joined
Topic Author
Posts: 11
Joined: Tue Sep 22, 2009 9:22 pm

dhcp-client and Auto Gateway change script

Fri Feb 12, 2010 9:22 pm

I have a cable connection and my ISP offers me dynamic IP address. I was concerned if the DHCP client automatically add the option "Routing Mark" to the added default route, but is not possible...

I found this script on another topic and it is framed on my needs(I think), but does not want to work.
# Written by Sam Norris, ChangeIP.com

:log info "Checking default gateway for dhcp interfaces."
:local theName [ /ip dhcp-client get [/ip dhcp-client find status=bound ] interface ]
:local currentGateway [ /ip dhcp-client get [/ip dhcp-client find status=bound ] gateway ]
:local existingGateway [ /ip route get [/ip route find comment=$theName] gateway ]
:log info ( $currentGateway . " - " . $existingGateway )

:if ($currentGateway != $existingGateway) do={

  :log info ("Setting default gateway for " . $theName)
  :local a [ /ip route find comment=$theName ]

  :if ([ :typeof $a ] = nil ) do={
    :log info "adding route..."
    /ip route add dst-address=0.0.0.0/0 gateway=$currentGateway routing-mark=$theName comment=$theName
   } else={
    :log info "changing route..."
    /ip route set $a gateway=$currentGateway
} else={
  :log info "No route changes needed."
}
RouterOS 3.29

[max@core] /system script> run script1
bad argument name else (line 20 column 3)
[max@core] /system script>
 
dssmiktik
Forum Veteran
Forum Veteran
Posts: 732
Joined: Fri Aug 17, 2007 8:42 am

Re: dhcp-client and Auto Gateway change script

Fri Feb 12, 2010 11:47 pm

Your missing a closing brace between line 19-20. The final code should look like this:
# Written by Sam Norris, ChangeIP.com

:log info "Checking default gateway for dhcp interfaces."
:local theName [ /ip dhcp-client get [/ip dhcp-client find status=bound ] interface ]
:local currentGateway [ /ip dhcp-client get [/ip dhcp-client find status=bound ] gateway ]
:local existingGateway [ /ip route get [/ip route find comment=$theName] gateway ]
:log info ( $currentGateway . " - " . $existingGateway )

:if ($currentGateway != $existingGateway) do={

  :log info ("Setting default gateway for " . $theName)
  :local a [ /ip route find comment=$theName ]

  :if ([ :typeof $a ] = nil ) do={
    :log info "adding route..."
    /ip route add dst-address=0.0.0.0/0 gateway=$currentGateway routing-mark=$theName comment=$theName
   } else={
    :log info "changing route..."
    /ip route set $a gateway=$currentGateway
   }
} else={
  :log info "No route changes needed."
}
 
SurferTim
Forum Guru
Forum Guru
Posts: 4637
Joined: Mon Jan 07, 2008 10:31 pm
Location: Miramar Beach, Florida

Re: dhcp-client and Auto Gateway change script

Sat Feb 13, 2010 7:43 pm

What is wrong with
/ip dhcp-client
set 0 add-default-route=yes
presuming this is the only dhcp-client entry.
you might also want to use this:
set 0 use-peer-dns=yes

EDIT: My bad! I see you have the gateway, just no routing mark. It took aminute to troubleshoot it.
# Written by Sam Norris, ChangeIP.com

:log info "Checking default gateway for dhcp interfaces."
:local theName [ /ip dhcp-client get [/ip dhcp-client find status=bound ] interface ]
:local currentGateway [ /ip dhcp-client get [/ip dhcp-client find status=bound ] gateway ]
:local existingGateway [ /ip route get [/ip route find comment=$theName] gateway ]
:log info ( $currentGateway . " - " . $existingGateway )

:if ($currentGateway != $existingGateway) do={

  :log info ("Setting default gateway for " . $theName)
  :local a [ /ip route find comment=$theName ]

  :if ([ :typeof $a ] = nil ) do={
    :log info "adding route..."
    /ip route add dst-address=0.0.0.0/0 gateway=$currentGateway routing-mark=$theName comment=$theName
   } else={
    :log info "changing route..."
    /ip route set $a gateway=$currentGateway
# here is the challenge. Missing a bracket!
   }
} else={
  :log info "No route changes needed."
}
 
psymax
just joined
Topic Author
Posts: 11
Joined: Tue Sep 22, 2009 9:22 pm

Re: dhcp-client and Auto Gateway change script

Tue Feb 16, 2010 1:32 pm

thanks a lot! :-o
 
User avatar
BraZuky
just joined
Posts: 17
Joined: Wed Jun 04, 2008 9:42 am
Location: Manaus
Contact:

Re: dhcp-client and Auto Gateway change script

Tue Aug 27, 2013 10:25 am

Thanks for the topic, I was looking for exact the same solution.

But as I like to understand what I'm using I didn't get how to use this script.

Do I need to schedule this script to run from time to time?

Thanks in advanced. :D
 
davadut
just joined
Posts: 8
Joined: Thu Dec 14, 2006 11:18 am

Re: dhcp-client and Auto Gateway change script

Fri Apr 04, 2014 4:21 am

Your missing a closing brace between line 19-20. The final code should look like this:
# Written by Sam Norris, ChangeIP.com

:log info "Checking default gateway for dhcp interfaces."
:local theName [ /ip dhcp-client get [/ip dhcp-client find status=bound ] interface ]
:local currentGateway [ /ip dhcp-client get [/ip dhcp-client find status=bound ] gateway ]
:local existingGateway [ /ip route get [/ip route find comment=$theName] gateway ]
:log info ( $currentGateway . " - " . $existingGateway )

:if ($currentGateway != $existingGateway) do={

  :log info ("Setting default gateway for " . $theName)
  :local a [ /ip route find comment=$theName ]

  :if ([ :typeof $a ] = nil ) do={
    :log info "adding route..."
    /ip route add dst-address=0.0.0.0/0 gateway=$currentGateway routing-mark=$theName comment=$theName
   } else={
    :log info "changing route..."
    /ip route set $a gateway=$currentGateway
   }
} else={
  :log info "No route changes needed."
}

hello,

thanks for this script this is what i need for my router, but can any one tell me how to use this script because i put this scrip on my mikrotik router when i run the script only show "Checking default gateway for dhcp interfaces."

any one can help me to know how to set this script ?

thank you
 
riyoritsu
just joined
Posts: 2
Joined: Fri Mar 20, 2020 2:33 pm

Re: dhcp-client and Auto Gateway change script

Fri Mar 20, 2020 2:41 pm

good day! i am looking for a way to edit a route with multiple gateway i am using ecmp, and one of the routes contains multiple gateways. what i have so fare is:

note: this script is run inside dhcp advanced options. thats why i have a variable "gateway-address".
if ($bound=1) do={ \
/ip route set [find comment="wan1"] gateway=$"gateway-address"; 
/ip route set [find comment="ecmp"] gateway=($"gateway-address",);
}

Who is online

Users browsing this forum: jprietove and 38 guests