Skip to content

Instantly share code, notes, and snippets.

@martinklepsch
Created April 22, 2010 19:39
Show Gist options
  • Save martinklepsch/375707 to your computer and use it in GitHub Desktop.
Save martinklepsch/375707 to your computer and use it in GitHub Desktop.
Simple script to reconnect your fritz.box
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Instruct an AVM FRITZ!Box via UPnP_ to reconnect.
This is usually realized with tools like Netcat_ or cURL_. However, when
developing in Python_ anyway, it is more convenient to integrate a native
implementation. This one requires Python_ 2.5 or higher.
UPnP_ (Universal Plug and Play) control messages are based on SOAP_, which is
itself based on XML_, and transmitted over HTTP_.
Make sure UPnP_ is enabled on the FRITZ!Box.
A reconnect only takes a few second while restarting the box takes about up to
a minute; not counting the time needed to navigate through the web interface.
.. _Netcat: http://netcat.sourceforge.net/
.. _cURL: http://curl.haxx.se/
.. _Python: http://www.python.org/
.. _UPnP: http://www.upnp.org/
.. _SOAP: http://www.w3.org/TR/soap/
.. _XML: http://www.w3.org/XML/
.. _HTTP: http://tools.ietf.org/html/rfc2616
:Copyright: 2008 Jochen Kupperschmidt
:Date: 04-Apr-2008
:License: MIT
:Homepage: http://homework.nwsnet.de/products/ef29_instruct-an-avm-fritzbox-via-upnp-to-reconnect
"""
from __future__ import with_statement
from contextlib import closing
import socket
def reconnect(host='fritz.box', port=49000, debug=False):
# Prepare HTTP data to send.
http_body = '\r\n'.join((
'<?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:ForceTermination xmlns:u="urn:schemas-upnp-org:service:WANIPConnection:1"/>',
' </s:Body>',
'</s:Envelope>'))
http_data = '\r\n'.join((
'POST /upnp/control/WANIPConn1 HTTP/1.1',
'Host: %s:%d' % (host, port),
'SoapAction: urn:schemas-upnp-org:service:WANIPConnection:1#ForceTermination',
'Content-Type: text/xml; charset="utf-8"',
'Content-Length: %d' % len(http_body),
'',
http_body))
# Connect to the box and submit SOAP data via HTTP.
with closing(socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as s:
s.connect((host, port))
s.send(http_data)
if debug:
data = s.recv(1024)
print 'Received:', data
if __name__ == '__main__':
reconnect()
@bagong
Copy link

bagong commented Jun 2, 2020

Hey,
das schaut ja toll aus! Auch aktuelle Fritzboxen sehen keine Möglichkeit vor, ein reconnect automatisch zu schedulen. Ist das hier noch aktuell - kann man das noch verwenden? Ich bin leider mit den verwendeten Technologien nicht vertraut, könnte wohl aber ein Python (3) script mit Crontab laufen lassen. Würde das heute noch funktionieren? (Ich hab' 'ne Fritzbox 7590)
Herzliche Grüße!

@martinklepsch
Copy link
Author

Ich kann mich kaum daran erinnern das hier gepostet zu haben aber vielleicht findest du auf der erwähnten Website noch weitere Infos http://homework.nwsnet.de/products/ef29_instruct-an-avm-fritzbox-via-upnp-to-reconnect

@bagong
Copy link

bagong commented Jun 2, 2020

Haha, okay, Danke (aus dem Süden!)

@hprenn
Copy link

hprenn commented Aug 11, 2020

Hi, super Sache. Funktioniert perfekt auch mit der neuesten Firmware 7.12 der FritzBox 7590.
Danke!

@GoaMetz
Copy link

GoaMetz commented Aug 11, 2020

Hi,

... funktioniert auch mit Fritzbox Cable 6591 mit Firmware 7.13.

THX!!

@CarstenG2
Copy link

Hier auch mal eine Powershell-Variante von mir zu dem Thema:
https://gist.github.com/CarstenG2/162ca553f9b5096499a1cc34fb88397b

@theotherp
Copy link

Funktioniert bei mir leider nicht auf der 6591:

<errorCode>401</errorCode>
<errorDescription>Invalid Action</errorDescription>

@GoaMetz
Copy link

GoaMetz commented Dec 16, 2020

Hier auch mal eine Powershell-Variante von mir zu dem Thema:
https://gist.github.com/CarstenG2/162ca553f9b5096499a1cc34fb88397b

Vielen herzlichen Dank Dir, CarstenG2, großartige Arbeit, funktioniert flawless bei meiner 6591 Cable v7.13.

@Paderman
Copy link

Paderman commented Dec 26, 2020

Ich kann mich kaum daran erinnern das hier gepostet zu haben aber vielleicht findest du auf der erwähnten Website noch weitere Infos http://homework.nwsnet.de/products/ef29_instruct-an-avm-fritzbox-via-upnp-to-reconnect

Funktioniert mit der alten Firmware-Version: 103.06.55 auch noch gut 😄

Gab es eigentlich auch ein Plugin für STB's wie Enigma2 welches ja ebenfalls Python installiert hat?

Ich hatte auch mal eine Variante für die Bash gehabt, aber da sponn irgendwie zu viel rum 🤕

@dundunn
Copy link

dundunn commented Jan 19, 2021

Funktioniert bei mir leider nicht auf der 6591:

<errorCode>401</errorCode>
<errorDescription>Invalid Action</errorDescription>

Ich hatte das gleiche Problem. fritzconnection funktioniert jedoch einwandfrei.

@EV21
Copy link

EV21 commented Dec 1, 2022

Damit hier auch mal eine Lösung des Problems steht:
Prüft ganz einfach die vorhandenen Services und die benötigten Parameter unter http://fritz.box:49000/tr64desc.xml

Wichtig zu wissen: DSL und Kabel unterscheiden sich teilweise
Ich habe DSL mit folgenden Parametern

urn='dslforum-org' # ganz wichtig
controlURL='/upnp/control/wanipconnection1'
service='WANIPConnection:1'
action='ForceTermination'

entsprechend das Skript umarbeiten und dann läuft der Haase.

@Paderman
Copy link

Paderman commented Dec 2, 2022

Damit hier auch mal eine Lösung des Problems steht: Prüft ganz einfach die vorhandenen Services und die benötigten Parameter unter http://fritz.box:49000/tr64desc.xml
[...]

Ich bin aus dem Thema fast raus, weil es beinahe 2 Jahre her ist 😅

Folgende Info zeigen folgende FRITZ!Boxen an

FRITZ!Box WLAN 3370 mit der FRITZ!OS:06.55

<serviceId>urn:DeviceInfo-com:serviceId:DeviceInfo1</serviceId>
<controlURL>/upnp/control/deviceinfo</controlURL>
<serviceType>urn:dslforum-org:service:WANIPConnection:1</serviceType>

ForceTermination fehlt

FRITZ!Box Fon WLAN 7270 v2 mit der FRITZ!OS 06.06

<serviceId>urn:DeviceInfo-com:serviceId:DeviceInfo1</serviceId>
<controlURL>/upnp/control/deviceinfo</controlURL>
<serviceType>urn:dslforum-org:service:WANIPConnection:1</serviceType>

ForceTermination fehlt

FRITZ!Box 6591 Cable (lgi) mit der FRITZ!OS: 07.29

<serviceId>urn:DeviceInfo-com:serviceId:DeviceInfo1</serviceId>
<controlURL>/upnp/control/deviceinfo</controlURL>
<serviceType>urn:dslforum-org:service:WANIPConnection:1</serviceType>

ForceTermination fehlt

@EV21
Copy link

EV21 commented Dec 2, 2022

In der XML ist nur die Übersicht aller Services aufgeführt, die unterstützen actions werden in einer jeweils zusätzlichen XML aufgeführt.
unter http://fritz.box:49000/tr64desc.xml
Findet man zum Beispiel

<service>
<serviceType>urn:dslforum-org:service:WANIPConnection:1</serviceType>
<serviceId>urn:WANIPConnection-com:serviceId:WANIPConnection1</serviceId>
<controlURL>/upnp/control/wanipconnection1</controlURL>
<eventSubURL>/upnp/control/wanipconnection1</eventSubURL>
<SCPDURL>/wanipconnSCPD.xml</SCPDURL>
</service>

Folgt man nun http://fritz.box:49000/wanipconnSCPD.xml , dann erhält man die Auflistung aller actions des jeweiligen service

<action>
<name>ForceTermination</name>
</action>

@homeworkprod
Copy link

Cool, dass das Script sich vielen als nützlich erwiesen hat.

Ich habe ihm ein eigenes Repository bei GitHub spendiert und ein paar kleine Anpassungen für aktuelle Python-Versionen vorgenommen, vielleicht hilft es ja jemandem: https://github.com/homeworkprod/fritzbox-reconnect

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment