#!/bin/bash

#
# Es müsste so auch per FHEM gehen:
# get FritzBox tr064Command DeviceConfig:1 deviceconfig Reboot
#

# von: https://github.com/nicoh88/cron_fritzbox-reboot

#######################################################
### Autor: Nico Hartung <nicohartung1@googlemail.com> #
#######################################################

# Skript sollte ab FritzOS 6.0 (2013) funktioneren - also auch für die 6.8x und 6.9x
# Dieses Bash-Skript nutzt das Protokoll TR-064 nicht die WEBCM-Schnittstelle

# http://fritz.box:49000/tr64desc.xml
# https://wiki.fhem.de/wiki/FRITZBOX#TR-064
# https://avm.de/service/schnittstellen/

# Thanks to Dragonfly (https://homematic-forum.de/forum/viewtopic.php?t=27994)

###=======###
# Variablen #
###=======###

# Please store your FritzBox data in the file ~/fritzbox_reboot.conf
# in the format
# IPS="192.168.178.2"
# FRITZUSER=""
# FRITZPW="this_is_my_password"
if [ ! -e ~/fritzbox_reboot.conf ]
then
  echo "Please store your FritzBox data in ~/fritzbox_reboot.conf"
  exit
fi
source ~/fritzbox_reboot.conf 

###====###
# Skript #
###====###

location="/upnp/control/deviceconfig"
uri="urn:dslforum-org:service:DeviceConfig:1"
action='Reboot'

for IP in ${IPS}; do
	curl -k -m 5 --anyauth -u "$FRITZUSER:$FRITZPW" http://$IP:49000$location -H 'Content-Type: text/xml; charset="utf-8"' -H "SoapAction:$uri#$action" -d "<?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:$action xmlns:u='$uri'></u:$action></s:Body></s:Envelope>" -s > /dev/null
#	curl --ciphers 'DEFAULT:!DH' -k -m 5 --anyauth -u "$FRITZUSER:$FRITZPW" https://$IP:49443$location -H 'Content-Type: text/xml; charset="utf-8"' -H "SoapAction:$uri#$action" -d "<?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:$action xmlns:u='$uri'></u:$action></s:Body></s:Envelope>"
done
