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()
@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