Scripts pour Freebox

01 - Préambule

Depuis fin juin, nous pouvons interagir avec la Freebox Revolution au travers l'API du nouveau Freebox OS.

02 - Processus d'interrogation

Le processus est décomposé en 2 phases :

  • l'autorisation de l'application après une validation physique sur la Freebox.
  • la création d'une session sur la Freebox.

03 - Phase d'autorisation

Pour cela il faut lancer la commande :

curl -X POST -i -H "Content-type: application/json" -X POST http://mafreebox.freebox.fr/api/v1/login/authorize/ -d '
{
   "app_id": "fr.prestaopen.cacti",
   "app_name": "Cacti_request",
   "app_version": "0.1",
   "device_name": "Eon_Master"
}    ' >/tmp/post_authorize.log 2>/dev/null

Un message apparaît sur la Freebox. Vous pouvez valider ou non l'application. Sans intervention de votre part, l'application n'est pas validée.

Ecran de validation

Appuyer sur la flèche de droite puis sur le bouton de validation.

Nous sauvegardons la variable app_token dans le fichier /tmp/post_authorize.log.

Fichier freeboxv6_adsl_api_post_authorize.sh :

#!/bin/sh
clear
curl -X POST -i -H "Content-type: application/json" -X POST http://mafreebox.freebox.fr/api/v1/login/authorize/ -d '
{
   "app_id": "fr.prestaopen.cacti",
   "app_name": "Cacti_request",
   "app_version": "0.1",
   "device_name": "Eon_Master"
}    ' >/tmp/post_authorize.log 2>/dev/null

Fichier post_authorize.log :

HTTP/1.1 200 OK
Server: nginx
Date: Fri, 16 Aug 2013 09:18:15 GMT
Content-Type: application/json; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive

{"success":true,"result":{"app_token":"1CKzz6w\/Qh0\/oCJuSyVdShSCGpFMm+xeyucdDrz3pUs85c4rrcIQC5R51fLRXNoF","track_id":0}}

04 - Modification des droits de l'application

Sur la console d'administration mafreebox.freebox.fr, il faut modifier les droits de l'application.

Login mafreebox

Se connecter avec votre mot de passe et cliquer sur Connexion.

Paramètres de la Freebox

 

Lancer le programme gérant les paramètres de la Freebox.

Gestion des droits

Lancer l'application pour la gestion des accès.

Fenêtre Gestion des accès

Cliquer sur l'onglet Applications.

Droits avant modification

 

Votre application n'a pas droits aux modifications des réglages de la Freebox. Cliquer sur l'icone de modification.

Ajout de droits

Cocher Modification des réglages de la Freebox et cliquer sur OK.

Droit après modification

 

Votre application a les bons droits. Cliquer sur OK pour sortir et fermer la fenêtre.

 

 

05 - Phase de session et d'interrogation

Nous devons dans un premier temps, récupérer l'app_token.

app_token=`grep "app_token" "/tmp/post_authorize.log" | cut -f 4 -d ':' | cut -f 1 -d ','| sed 's/\\\//g' |sed "s/\"//g"| sed "s/\r//g" | sed "s/\n//g"` 

Ce code va chercher la valeur de app_token et le nettoyer des caractères "\".

curl -H "Content-type: application/json" -X GET http://mafreebox.freebox.fr/api/v1/login/>/tmp/challenge.log 2>/dev/null
challenge=`grep "challenge" "/tmp/challenge.log" | cut -f 5 -d ':' | cut -f 1 -d ','| sed 's/\\\//g' |sed "s/\"//g"| sed "s/\r//g" | sed "s/\n//g"` 

Ce code interroge la Freebox pour recevoir un challenge et va nettoyer le challenge.

password=`echo -n $challenge | openssl dgst -sha1 -hmac $app_token`

Nous devons créer un password pour cette session.

curl -i -H "Content-type: application/json" -X POST http://mafreebox.freebox.fr/api/v1/login/session/ -d '
{
   "app_id": "fr.prestaopen.cacti",
   "password": "'$password'"
}    '>/tmp/session_token.log 2>/dev/null
session_token=`grep "session_token" "/tmp/session_token.log" | cut -f 3 -d ':' | cut -f 1 -d ','| sed 's/\\\//g' |sed "s/\"//g"| sed "s/\r//g" | sed "s/\n//g"` 

La session est ouverte par ce code  et nous avons maintenant un session_token.

06 - Interrogation DSL

Nous allons voir ici comment interroger la Freebox pour recevoir les valeurs concernant l'ADSL :

  • Rate_up
  • Rate_down
  • snr_up
  • Snr_down

07 - Rate_up

Cette donnée se trouve dans la base dsl et se nomme rate_up. Le code d'interrogation est celui-ci :

curl -i  -X POST http://mafreebox.freebox.fr/api/v1/rrd/ -H "X-Fbx-App-Auth: $session_token" -d '
{
   "db": "dsl",
   "fields": [ "rate_up" ],
   "precision": 1
}    '> /tmp/dsl_rate_up.log 2>/dev/null

Il est nettoyé ici :

rate_up=`cat /tmp/dsl_rate_up.log | grep -oE "[^{]+$"| grep -oE '":.*,' | sed -n 's/\([^:]*:\)\([^,]*\),.*/\2/p'`

08 - Rate_down

Cette donnée se trouve dans la base dsl et se nomme rate_down. Le code d'interrogation est celui-ci :

 

curl -i  -X POST http://mafreebox.freebox.fr/api/v1/rrd/ -H "X-Fbx-App-Auth: $session_token" -d '
{
   "db": "dsl",
   "fields": [ "rate_down" ],
   "precision": 1
}    '> /tmp/dsl_rate_down.log 2>/dev/null

Il est nettoyé ici :

 

rate_down=`cat /tmp/dsl_rate_down.log | grep -oE "[^{]+$"| grep -oE ',".*,' | sed -n 's/\([^{]*:\)\([^,]*\),.*/\2/p' | sed 's/.\{2\}$//'`

Attention : l'agencement des valeurs est différente pour cette donnée.

09 - Snr_up

Cette donnée se trouve dans la base dsl et se nomme snr_up. Le code d'interrogation est celui-ci :

 

curl -i  -X POST http://mafreebox.freebox.fr/api/v1/rrd/ -H "X-Fbx-App-Auth: $session_token" -d '
{
   "db": "dsl",
   "fields": [ "snr_up" ],
   "precision": 1
}    '> /tmp/dsl_snr_up.log 2>/dev/null

Il est nettoyé ici :

 

snr_up=`cat /tmp/dsl_snr_up.log | grep -oE "[^{]+$"| grep -oE '":.*,' | sed -n 's/\([^:]*:\)\([^,]*\),.*/\2/p'`

10 - Snr_down

Cette donnée se trouve dans la base dsl et se nomme snr_down. Le code d'interrogation est celui-ci :

 

curl -i  -X POST http://mafreebox.freebox.fr/api/v1/rrd/ -H "X-Fbx-App-Auth: $session_token" -d '
{
   "db": "dsl",
   "fields": [ "snr_down" ],
   "precision": 1
}    '> /tmp/dsl_snr_down.log 2>/dev/null

Il est nettoyé ici :

 snr_down=`cat /tmp/dsl_snr_down.log | grep -oE "[^{]+$"| grep -oE '":.*,' | sed -n 's/\([^:]*:\)\([^,]*\),.*/\2/p'`

11 - Formatage des résultats

Nous allons mettre en forme les résultats pour Cacti avec le code ci-dessous :

printf "rate_up:%s rate_down:%s snr_up:%s snr_down:%s \n" $rate_up $rate_down $snr_up $snr_down;

12 - Conclusion

Voici le résultat de notre interrogation :

Résultat

Voila nous avons un script pour interroger notre Freebox. Nous verrons dans un autre article comment définir les graphes de notre Freebox sur Cacti.

13 - Annexes

Programme pour lire les valeurs rrd de la niche dsl :

#!/bin/sh
app_token=`grep "app_token" "/tmp/post_authorize.log" | cut -f 4 -d ':' | cut -f 1 -d ','| sed 's/\\\//g' |sed "s/\"//g"| sed "s/\r//g" | sed "s/\n//g"` 
curl -H "Content-type: application/json" -X GET http://mafreebox.freebox.fr/api/v1/login/>/tmp/challenge.log 2>/dev/null
challenge=`grep "challenge" "/tmp/challenge.log" | cut -f 5 -d ':' | cut -f 1 -d ','| sed 's/\\\//g' |sed "s/\"//g"| sed "s/\r//g" | sed "s/\n//g"` 
password=`echo -n $challenge | openssl dgst -sha1 -hmac $app_token`
curl -i -H "Content-type: application/json" -X POST http://mafreebox.freebox.fr/api/v1/login/session/ -d '
{
   "app_id": "fr.prestaopen.cacti",
   "password": "'$password'"
}    '>/tmp/session_token.log 2>/dev/null
session_token=`grep "session_token" "/tmp/session_token.log" | cut -f 3 -d ':' | cut -f 1 -d ','| sed 's/\\\//g' |sed "s/\"//g"| sed "s/\r//g" | sed "s/\n//g"` 
curl -i  -X POST http://mafreebox.freebox.fr/api/v1/rrd/ -H "X-Fbx-App-Auth: $session_token" -d '
{
   "db": "dsl",
   "fields": [ "rate_up" ],
   "precision": 100
}    '> /tmp/dsl_rate_up.log 2>/dev/null
rate_up=`cat /tmp/dsl_rate_up.log | grep -oE "[^{]+$"| grep -oE '":.*,' | sed -n 's/\([^:]*:\)\([^,]*\),.*/\2/p'`
curl -i  -X POST http://mafreebox.freebox.fr/api/v1/rrd/ -H "X-Fbx-App-Auth: $session_token" -d '
{
   "db": "dsl",
   "fields": [ "rate_down" ],
   "precision": 100
}    '> /tmp/dsl_rate_down.log 2>/dev/null
rate_down=`cat /tmp/dsl_rate_down.log | grep -oE "[^{]+$"| grep -oE ',".*,' | sed -n 's/\([^{]*:\)\([^,]*\),.*/\2/p' | sed 's/.\{2\}$//'`
curl -i  -X POST http://mafreebox.freebox.fr/api/v1/rrd/ -H "X-Fbx-App-Auth: $session_token" -d '
{
   "db": "dsl",
   "fields": [ "snr_up" ],
   "precision": 100
}    '> /tmp/dsl_snr_up.log 2>/dev/null
snr_up=`cat /tmp/dsl_snr_up.log | grep -oE "[^{]+$"| grep -oE '":.*,' | sed -n 's/\([^:]*:\)\([^,]*\),.*/\2/p'`
curl -i  -X POST http://mafreebox.freebox.fr/api/v1/rrd/ -H "X-Fbx-App-Auth: $session_token" -d '
{
   "db": "dsl",
   "fields": [ "snr_down" ],
   "precision": 100
}    '> /tmp/dsl_snr_down.log 2>/dev/null
snr_down=`cat /tmp/dsl_snr_down.log | grep -oE "[^{]+$"| grep -oE '":.*,' | sed -n 's/\([^:]*:\)\([^,]*\),.*/\2/p'`
printf "rate_down:%s rate_up:%s snr_down:%s snr_up:%s \n" $rate_down $rate_up $snr_down $snr_up;

Programme pour lire les valeurs rrd de la niche net :

#!/bin/sh
app_token=`grep "app_token" "/tmp/post_authorize.log" | cut -f 4 -d ':' | cut -f 1 -d ','| sed 's/\\\//g' |sed "s/\"//g"| sed "s/\r//g" | sed "s/\n//g"` 
curl -H "Content-type: application/json" -X GET http://mafreebox.freebox.fr/api/v1/login/>/tmp/challenge.log 2>/dev/null
challenge=`grep "challenge" "/tmp/challenge.log" | cut -f 5 -d ':' | cut -f 1 -d ','| sed 's/\\\//g' |sed "s/\"//g"| sed "s/\r//g" | sed "s/\n//g"` 
password=`echo -n $challenge | openssl dgst -sha1 -hmac $app_token`
curl -i -H "Content-type: application/json" -X POST http://mafreebox.freebox.fr/api/v1/login/session/ -d '
{
   "app_id": "fr.prestaopen.cacti",
   "password": "'$password'"
}    '>/tmp/session_token.log 2>/dev/null
session_token=`grep "session_token" "/tmp/session_token.log" | cut -f 3 -d ':' | cut -f 1 -d ','| sed 's/\\\//g' |sed "s/\"//g"| sed "s/\r//g" | sed "s/\n//g"` 
curl -i  -X POST http://mafreebox.freebox.fr/api/v1/rrd/ -H "X-Fbx-App-Auth: $session_token" -d '
{
   "db": "net",
   "fields": [ "bw_up" ],
   "precision": 1
}    '> /tmp/net_bw_up.log 2>/dev/null
bw_up=`cat /tmp/net_bw_up.log | grep -oE "[^{]+$"| grep -oE ',".*,' | sed -n 's/\([^{]*:\)\([^,]*\),.*/\2/p' | sed 's/.\{2\}$//'`
curl -i  -X POST http://mafreebox.freebox.fr/api/v1/rrd/ -H "X-Fbx-App-Auth: $session_token" -d '
{
   "db": "net",
   "fields": [ "bw_down" ],
   "precision": 1
}    '> /tmp/net_bw_down.log 2>/dev/null
bw_down=`cat /tmp/net_bw_down.log | grep -oE "[^{]+$"| grep -oE '":.*,' | sed -n 's/\([^:]*:\)\([^,]*\),.*/\2/p'`
curl -i  -X POST http://mafreebox.freebox.fr/api/v1/rrd/ -H "X-Fbx-App-Auth: $session_token" -d '
{
   "db": "net",
   "fields": [ "rate_up" ],
   "precision": 1
}    '> /tmp/net_rate_up.log 2>/dev/null
rate_up=`cat /tmp/net_rate_up.log | grep -oE "[^{]+$"| grep -oE '":.*,' | sed -n 's/\([^:]*:\)\([^,]*\),.*/\2/p'`
curl -i  -X POST http://mafreebox.freebox.fr/api/v1/rrd/ -H "X-Fbx-App-Auth: $session_token" -d '
{
   "db": "net",
   "fields": [ "rate_down" ],
   "precision": 1
}    '> /tmp/net_rate_down.log 2>/dev/null
rate_down=`cat /tmp/net_rate_down.log | grep -oE "[^{]+$"| grep -oE ',".*,' | sed -n 's/\([^{]*:\)\([^,]*\),.*/\2/p' | sed 's/.\{2\}$//'`
printf "bw_up:%s bw_down:%s rate_up:%s rate_down:%s \n" $bw_up $bw_down $rate_up $rate_down;

Nous utilisons 2 programmes pour lire les valeurs rrd de la niche switch :

Pour les données en réception

#!/bin/sh
app_token=`grep "app_token" "/tmp/post_authorize.log" | cut -f 4 -d ':' | cut -f 1 -d ','| sed 's/\\\//g' |sed "s/\"//g"| sed "s/\r//g" | sed "s/\n//g"` 
curl -H "Content-type: application/json" -X GET http://mafreebox.freebox.fr/api/v1/login/>/tmp/challenge.log 2>/dev/null
challenge=`grep "challenge" "/tmp/challenge.log" | cut -f 5 -d ':' | cut -f 1 -d ','| sed 's/\\\//g' |sed "s/\"//g"| sed "s/\r//g" | sed "s/\n//g"` 
password=`echo -n $challenge | openssl dgst -sha1 -hmac $app_token`
curl -i -H "Content-type: application/json" -X POST http://mafreebox.freebox.fr/api/v1/login/session/ -d '
{
   "app_id": "fr.prestaopen.cacti",
   "password": "'$password'"
}    '>/tmp/session_token.log 2>/dev/null
session_token=`grep "session_token" "/tmp/session_token.log" | cut -f 3 -d ':' | cut -f 1 -d ','| sed 's/\\\//g' |sed "s/\"//g"| sed "s/\r//g" | sed "s/\n//g"` 
		curl -i  -X POST http://mafreebox.freebox.fr/api/v1/rrd/ -H "X-Fbx-App-Auth: $session_token" -d '
{
   "db": "switch",
   "fields": [ "rx_1" ],
   "precision": 1
}    '> /tmp/switch_rx_1.log 2>/dev/null
switch_rx_1=`cat /tmp/switch_rx_1.log | grep -oE "[^{]+$"| grep -oE ',".*,' | sed -n 's/\([^{]*:\)\([^,]*\),.*/\2/p' | sed 's/.\{2\}$//'`
		curl -i  -X POST http://mafreebox.freebox.fr/api/v1/rrd/ -H "X-Fbx-App-Auth: $session_token" -d '
{
   "db": "switch",
   "fields": [ "rx_2" ],
   "precision": 1
}    '> /tmp/switch_rx_2.log 2>/dev/null
switch_rx_2=`cat /tmp/switch_rx_2.log | grep -oE "[^{]+$"| grep -oE '":.*,' | sed -n 's/\([^:]*:\)\([^,]*\),.*/\2/p'`
		curl -i  -X POST http://mafreebox.freebox.fr/api/v1/rrd/ -H "X-Fbx-App-Auth: $session_token" -d '
{
   "db": "switch",
   "fields": [ "rx_3" ],
   "precision": 1
}    '> /tmp/switch_rx_3.log 2>/dev/null
switch_rx_3=`cat /tmp/switch_rx_3.log | grep -oE "[^{]+$"| grep -oE '":.*,' | sed -n 's/\([^:]*:\)\([^,]*\),.*/\2/p'`
		curl -i  -X POST http://mafreebox.freebox.fr/api/v1/rrd/ -H "X-Fbx-App-Auth: $session_token" -d '
{
   "db": "switch",
   "fields": [ "rx_4" ],
   "precision": 1
}    '> /tmp/switch_rx_4.log 2>/dev/null
switch_rx_4=`cat /tmp/switch_rx_4.log | grep -oE "[^{]+$"| grep -oE ',".*,' | sed -n 's/\([^{]*:\)\([^,]*\),.*/\2/p' | sed 's/.\{2\}$//'`
 printf "switch_rx_1:%s switch_rx_2:%s switch_rx_3:%s switch_rx_4:%s \n" $switch_rx_1 $switch_rx_2 $switch_rx_3 $switch_rx_4 ;

Pour les données en émission

#!/bin/sh
app_token=`grep "app_token" "/tmp/post_authorize.log" | cut -f 4 -d ':' | cut -f 1 -d ','| sed 's/\\\//g' |sed "s/\"//g"| sed "s/\r//g" | sed "s/\n//g"` 
curl -H "Content-type: application/json" -X GET http://mafreebox.freebox.fr/api/v1/login/>/tmp/challenge.log 2>/dev/null
challenge=`grep "challenge" "/tmp/challenge.log" | cut -f 5 -d ':' | cut -f 1 -d ','| sed 's/\\\//g' |sed "s/\"//g"| sed "s/\r//g" | sed "s/\n//g"` 
password=`echo -n $challenge | openssl dgst -sha1 -hmac $app_token`
curl -i -H "Content-type: application/json" -X POST http://mafreebox.freebox.fr/api/v1/login/session/ -d '
{
   "app_id": "fr.prestaopen.cacti",
   "password": "'$password'"
}    '>/tmp/session_token.log 2>/dev/null
session_token=`grep "session_token" "/tmp/session_token.log" | cut -f 3 -d ':' | cut -f 1 -d ','| sed 's/\\\//g' |sed "s/\"//g"| sed "s/\r//g" | sed "s/\n//g"` 
		curl -i  -X POST http://mafreebox.freebox.fr/api/v1/rrd/ -H "X-Fbx-App-Auth: $session_token" -d '
{
   "db": "switch",
   "fields": [ "tx_1" ],
   "precision": 1
}    '> /tmp/switch_tx_1.log 2>/dev/null
switch_tx_1=`cat /tmp/switch_tx_1.log | grep -oE "[^{]+$"| grep -oE ',".*,' | sed -n 's/\([^{]*:\)\([^,]*\),.*/\2/p' | sed 's/.\{2\}$//'`
		curl -i  -X POST http://mafreebox.freebox.fr/api/v1/rrd/ -H "X-Fbx-App-Auth: $session_token" -d '
{
   "db": "switch",
   "fields": [ "tx_2" ],
   "precision": 1
}    '> /tmp/switch_tx_2.log 2>/dev/null
switch_tx_2=`cat /tmp/switch_tx_2.log | grep -oE "[^{]+$"| grep -oE '":.*,' | sed -n 's/\([^:]*:\)\([^,]*\),.*/\2/p'`
		curl -i  -X POST http://mafreebox.freebox.fr/api/v1/rrd/ -H "X-Fbx-App-Auth: $session_token" -d '
{
   "db": "switch",
   "fields": [ "tx_3" ],
   "precision": 1
}    '> /tmp/switch_tx_3.log 2>/dev/null
switch_tx_3=`cat /tmp/switch_tx_3.log | grep -oE "[^{]+$"| grep -oE '":.*,' | sed -n 's/\([^:]*:\)\([^,]*\),.*/\2/p'`
		curl -i  -X POST http://mafreebox.freebox.fr/api/v1/rrd/ -H "X-Fbx-App-Auth: $session_token" -d '
{
   "db": "switch",
   "fields": [ "tx_4" ],
   "precision": 1
}    '> /tmp/switch_tx_4.log 2>/dev/null
switch_tx_4=`cat /tmp/switch_tx_4.log | grep -oE "[^{]+$"| grep -oE ',".*,' | sed -n 's/\([^{]*:\)\([^,]*\),.*/\2/p' | sed 's/.\{2\}$//'`
 printf "switch_tx_1:%s switch_tx_2:%s switch_tx_3:%s switch_tx_4:%s \n" $switch_tx_1 $switch_tx_2 $switch_tx_3 $switch_tx_4 ;

Programme pour lire les valeurs rrd de la niche temp :

#!/bin/sh
app_token=`grep "app_token" "/tmp/post_authorize.log" | cut -f 4 -d ':' | cut -f 1 -d ','| sed 's/\\\//g' |sed "s/\"//g"| sed "s/\r//g" | sed "s/\n//g"` 
curl -H "Content-type: application/json" -X GET http://mafreebox.freebox.fr/api/v1/login/>/tmp/challenge.log 2>/dev/null
challenge=`grep "challenge" "/tmp/challenge.log" | cut -f 5 -d ':' | cut -f 1 -d ','| sed 's/\\\//g' |sed "s/\"//g"| sed "s/\r//g" | sed "s/\n//g"` 
password=`echo -n $challenge | openssl dgst -sha1 -hmac $app_token`
curl -i -H "Content-type: application/json" -X POST http://mafreebox.freebox.fr/api/v1/login/session/ -d '
{
   "app_id": "fr.prestaopen.cacti",
   "password": "'$password'"
}    '>/tmp/session_token.log 2>/dev/null
session_token=`grep "session_token" "/tmp/session_token.log" | cut -f 3 -d ':' | cut -f 1 -d ','| sed 's/\\\//g' |sed "s/\"//g"| sed "s/\r//g" | sed "s/\n//g"` 
curl -i  -X POST http://mafreebox.freebox.fr/api/v1/rrd/ -H "X-Fbx-App-Auth: $session_token" -d '
{
   "db": "temp",
   "fields": [ "temp1" ],
   "precision": 100
}    '> /tmp/temp1.log 2>/dev/null
temp1=`cat /tmp/temp1.log | grep -oE "[^{]+$"| grep -oE '":.*,' | sed -n 's/\([^:]*:\)\([^,]*\),.*/\2/p'`
curl -i  -X POST http://mafreebox.freebox.fr/api/v1/rrd/ -H "X-Fbx-App-Auth: $session_token" -d '
{
   "db": "temp",
   "fields": [ "temp2" ],
   "precision": 100
}    '> /tmp/temp2.log 2>/dev/null
temp2=`cat /tmp/temp2.log | grep -oE "[^{]+$"| grep -oE '":.*,' | sed -n 's/\([^:]*:\)\([^,]*\),.*/\2/p'`
curl -i  -X POST http://mafreebox.freebox.fr/api/v1/rrd/ -H "X-Fbx-App-Auth: $session_token" -d '
{
   "db": "temp",
   "fields": [ "temp3" ],
   "precision": 100
}    '> /tmp/temp3.log 2>/dev/null
temp3=`cat /tmp/temp3.log | grep -oE "[^{]+$"| grep -oE '":.*,' | grep -oE '3":.*,' | sed -n 's/\([^:]*:\)\([^,]*\),.*/\2/p' |  sed -e "s/}]//g"`
 printf "temp1:%s temp2:%s temp3:%s \n" $temp1 $temp2 $temp3;

Ces programmes seront appelés par Cacti.

Aucun commentaire