Documentation of steps to export data from a Fritz!Box and mangle it into a format for import into MikroTik routers.
The Fritz!Box (tested with v7.29) provides an API to query data from the router:
http://fritz.box/query.lua?sid=<session-id>&network=landevice:settings/landevice/list(<variable-list>)
The session ID is part of any URL within the web interface when logged in.
The list of available properties is quite extensive. The list of properties needed for the purpose of data migration is:
Variable | Description |
---|---|
|
MAC address of device |
|
Current / assigned IP address |
|
Name of device (can bes user given) |
|
Does the network device use DHCP at all? (Static IP configured on the device itself). |
|
Is DHCP configured for this device to always have the same address? |
|
|
|
Resulting query:
http://fritz.box/query.lua?sid=<session-id>&network=landevice:settings/landevice/list(mac,ip,name,dhcp,static_dhcp,active,online)
The returned data has the JSON
format and is expected to be saved as landevices.json
for the next steps.
{"network":[
{
"_node":"landevice0",
"online":"0",
"name":"Android-3",
"active":"0",
"dhcp":"1",
"mac":"01:23:45:67:89:AB",
"static_dhcp":"0",
"ip":"10.0.234.1"
},
{
"_node":"landevice1",
"online":"0",
"name":"Android-5",
"active":"0",
"dhcp":"1",
"mac":"12:23:34:45:56:67",
"static_dhcp":"1",
"ip":"10.0.25.11"
},
{
"_node":"landevice2",
"online":"1",
"name":"Notebook-1",
"active":"0",
"dhcp":"1",
"mac":"22:33:44:55:66:77",
"static_dhcp":"1",
"ip":"10.0.25.2"
},
{
"_node":"landevice3",
"online":"0",
"name":"Entertain",
"active":"1",
"dhcp":"0",
"mac":"30:D3:EE:EE:EE:EE",
"static_dhcp":"0",
"ip":""
}
]}
This data is also available as sample file: landevices.json.
The returned JSON
data can be easily queried and manipulated with jq.
DHCP leases can be configured such as
/ip dhcp-server lease
add client-id=1:22:33:44:55:66:77 mac-address=22:33:44:55:66:77 address=10.0.25.2 comment="Notebook-1"
add client-id=1:12:23:34:45:56:67 mac-address=12:23:34:45:56:67 address=10.0.25.11 comment="Android-5"
-
MikroTik
cat data/landevices.json | jq -c '.network|=sort_by(.ip) | .network[] | del(._node)'
{"online":"0","name":"Entertain","active":"1","dhcp":"0","mac":"30:D3:EE:EE:EE:EE","static_dhcp":"0","ip":""}
{"online":"0","name":"Android-3","active":"0","dhcp":"1","mac":"01:23:45:67:89:AB","static_dhcp":"0","ip":"10.0.234.1"}
{"online":"0","name":"Android-5","active":"0","dhcp":"1","mac":"12:23:34:45:56:67","static_dhcp":"1","ip":"10.0.25.11"}
{"online":"1","name":"Notebook-1","active":"0","dhcp":"1","mac":"22:33:44:55:66:77","static_dhcp":"1","ip":"10.0.25.2"}
cat data/landevices.json | jq -c '.network|=sort_by(.ip | split(".") | map(tonumber)) | .network[] | del(._node)'
{"online":"0","name":"Entertain","active":"1","dhcp":"0","mac":"30:D3:EE:EE:EE:EE","static_dhcp":"0","ip":""}
{"online":"1","name":"Notebook-1","active":"0","dhcp":"1","mac":"22:33:44:55:66:77","static_dhcp":"1","ip":"10.0.25.2"}
{"online":"0","name":"Android-5","active":"0","dhcp":"1","mac":"12:23:34:45:56:67","static_dhcp":"1","ip":"10.0.25.11"}
{"online":"0","name":"Android-3","active":"0","dhcp":"1","mac":"01:23:45:67:89:AB","static_dhcp":"0","ip":"10.0.234.1"}
Entries need to have a MAC, an IP address and need to have been used for static assignments.
cat data/landevices.json | jq -c '.network[] | select(.static_dhcp == "1") | select(.mac != "") | select(.ip != "") | del(._node)'
{"online":"0","name":"Android-5","active":"0","dhcp":"1","mac":"12:23:34:45:56:67","static_dhcp":"1","ip":"10.0.25.11"}
{"online":"1","name":"Notebook-1","active":"0","dhcp":"1","mac":"22:33:44:55:66:77","static_dhcp":"1","ip":"10.0.25.2"}
cat data/landevices.json | jq '.network[] | @sh "add client-id=1:\(.mac) mac-address=\(.mac) address=\(.ip) comment=\"\(.name)\""'
"add client-id=1:'01:23:45:67:89:AB' mac-address='01:23:45:67:89:AB' address='10.0.234.1' comment=\"'Android-3'\""
"add client-id=1:'12:23:34:45:56:67' mac-address='12:23:34:45:56:67' address='10.0.25.11' comment=\"'Android-5'\""
"add client-id=1:'22:33:44:55:66:77' mac-address='22:33:44:55:66:77' address='10.0.25.2' comment=\"'Notebook-1'\""
"add client-id=1:'30:D3:EE:EE:EE:EE' mac-address='30:D3:EE:EE:EE:EE' address='' comment=\"'Entertain'\""
cat data/landevices.json | jq -r '.network[] | @sh "add client-id=1:\(.mac) mac-address=\(.mac) address=\(.ip) comment=\"\(.name)\""'
-r
is short for --raw-output
add client-id=1:'01:23:45:67:89:AB' mac-address='01:23:45:67:89:AB' address='10.0.234.1' comment="'Android-3'"
add client-id=1:'12:23:34:45:56:67' mac-address='12:23:34:45:56:67' address='10.0.25.11' comment="'Android-5'"
add client-id=1:'22:33:44:55:66:77' mac-address='22:33:44:55:66:77' address='10.0.25.2' comment="'Notebook-1'"
add client-id=1:'30:D3:EE:EE:EE:EE' mac-address='30:D3:EE:EE:EE:EE' address='' comment="'Entertain'"
cat data/landevices.json | jq -r '.network|=sort_by(.ip | split(".") | map(tonumber) ) | .network[] | select(.static_dhcp == "1") | select(.mac != "") | select(.ip != "") | @text " add client-id=1:\(.mac) mac-address=\(.mac) address=\(.ip) comment=\"\(.name)\""' | (echo "/ip dhcp-server lease"; cat)
/ip dhcp-server lease
add client-id=1:22:33:44:55:66:77 mac-address=22:33:44:55:66:77 address=10.0.25.2 comment="Notebook-1"
add client-id=1:12:23:34:45:56:67 mac-address=12:23:34:45:56:67 address=10.0.25.11 comment="Android-5"
cat data/landevices.json | jq -r '.network|=sort_by(.ip | split(".") | map(tonumber) ) | .network[] | select(.static_dhcp == "1") | select(.name != "") | select(.ip != "") | @text " add name=\"\(.name)\" address=\(.ip)"' | (echo "/ip dns static"; cat)
/ip dns static
add name="Notebook-1" address=10.0.25.2
add name="Android-5" address=10.0.25.11
The shell script combines both functions and takes a given file as argument.
scripts/fritz2tik.sh data/landevices.json
/ip dhcp-server lease
add client-id=1:22:33:44:55:66:77 mac-address=22:33:44:55:66:77 address=10.0.25.2 comment="Notebook-1"
add client-id=1:12:23:34:45:56:67 mac-address=12:23:34:45:56:67 address=10.0.25.11 comment="Android-5"
/ip dns static
add name="Notebook-1" address=10.0.25.2
add name="Android-5" address=10.0.25.11
The script also allows usage of a domain which will be used as part of the hostnames:
scripts/fritz2tik.sh data/landevices.json lan
/ip dhcp-server lease
add client-id=1:22:33:44:55:66:77 mac-address=22:33:44:55:66:77 address=10.0.25.2 comment="Notebook-1"
add client-id=1:12:23:34:45:56:67 mac-address=12:23:34:45:56:67 address=10.0.25.11 comment="Android-5"
/ip dns static
add name="Notebook-1.lan" address=10.0.25.2
add name="Android-5.lan" address=10.0.25.11