Scripts for mikrotik to operate a FritzBox, leveraging FritzBox TR064 SOAP API

Dear all,

I recently had to script some operations for mikrotik to programmatically interact with FritzBox.
It’s a topic not very well documented (only post on subject here: Can't post request ptopperly with fetch tool), so I’m here pasting the scripts I ended building - hopefully this can help someone.

Those work as of today (2026-02-01):

  • executed on RouterOS running 7.21.2
  • connecting to FritzBox running 7.6 and 8.20

Script 1:

Without authentication to perform a few things: getting addresses (IPv4, IPv6), getting IPv6 prefix, forcing a disconnect/reconnect

# this connects to the FritzBox using TR064 SOAP and operates without authentication

# input parameters
:local hostname "192.168.178.1"; # hostname/IP to the box
:local method "GetExternalIPAddress"; # API method to use (see below valid methods)


# valid methods:
# "GetExternalIPAddress" - get WAN ipv4 address
# "X_AVM_DE_GetExternalIPv6Address" - get WAN ipv6 address
# "X_AVM_DE_GetIPv6Prefix" - get WAN ipv6 prefix
# "ForceTermination" - reconnect the WAN


# variable initialization
:local result

# -- execute --
# prepare the fetch
:local fetchMaxTime 30s
:local fetchMaxRedirects 5
:local fetchHeaderField "Content-Type: text/xml; charset='utf-8',SOAPAction: urn:schemas-upnp-org:service:WANIPConnection:1#$method"
:local fetchUrl "http://$hostname:49000/igdupnp/control/WANIPConn1"
:local fetchHttpData "<?xml version='1.0' encoding='utf-8'?><s:Envelope xmlns:s='http://schemas.xmlsoap.org/soap/envelope/' s:encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'> <s:Body> <u:$method xmlns:u='urn:schemas-upnp-org:service:WANIPConnection:1' /></s:Body></s:Envelope>"

:set result [/tool fetch http-method=post http-header-field="$fetchHeaderField" url="$fetchUrl" http-data="$fetchHttpData" http-max-redirect-count=$fetchMaxRedirects duration=$fetchMaxTime as-value output=user];

:put $result

Script 2:
Reboot the device (needs authentication)

# this connects to the FritzBox using TR064 SOAP and operates with authentication

# input parameters
:local hostname "192.168.178.1"; # hostname/IP to the box
:local username "myUsername"; # API username
:local password "myPassword"; # API password
:local method "Reboot"; # API method to use (see below valid methods)


# valid methods:
# "Reboot" - reboot the device


# variable initialization
:local result

# -- execute --
# prepare the fetch
:local fetchMaxTime 30s
:local fetchMaxRedirects 5
:local fetchHeaderField "Content-Type: text/xml; charset='utf-8',SoapAction:urn:dslforum-org:service:DeviceConfig:1#$method"
:local fetchUrl "http://$hostname:49000/upnp/control/deviceconfig"
:local fetchHttpData "<?xml version='1.0' encoding='utf-8'?><s:Envelope s:encodingStyle='http://schemas.xmlsoap.org/soap/encoding/' xmlns:s='http://schemas.xmlsoap.org/soap/envelope/'><s:Body><u:$method xmlns:u='urn:dslforum-org:service:DeviceConfig:1'></u:$method></s:Body></s:Envelope>"
:local fetchHttpAuthScheme "digest"

:set result [/tool fetch http-method=post http-header-field="$fetchHeaderField" user="$username" password="$password" url="$fetchUrl" http-data="$fetchHttpData" http-max-redirect-count=$fetchMaxRedirects duration=$fetchMaxTime http-auth-scheme=$fetchHttpAuthScheme as-value output=user];

:return $result