Code : Tout sélectionner
#include <ButtonConstants.au3>
#include <ComboConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#Include <Array.au3>
$Form1 = GUICreate("LiveBox - Suppression de périphériques", 435, 153, 192, 124)
$Combo1 = GUICtrlCreateCombo("Périphépiques déconnectés", 24, 72, 385, 25)
        GUICtrlSetState($Combo1, $GUI_DISABLE)
$Label1 = GUICtrlCreateLabel("Selectionnez un périphérique à supprimer", 24, 56, 198, 16)
$Button1 = GUICtrlCreateButton("Supprimer", 336, 112, 75, 25)
$Button2 = GUICtrlCreateButton("Supprimer tout", 24, 112, 75, 25)
    GUICtrlSetState($Button1, $GUI_DISABLE)
    GUICtrlSetState($Button2, $GUI_DISABLE)
$Input1 = GUICtrlCreateInput("admin", 24, 24, 65, 21)
    GUICtrlSetState($Input1, $GUI_DISABLE)
$Input2 = GUICtrlCreateInput("", 96, 24, 161, 21, $ES_PASSWORD) ;Password
$Label2 = GUICtrlCreateLabel("Utilisateur", 24, 8, 50, 16)
$Label3 = GUICtrlCreateLabel("Mot de passe", 96, 8, 68, 16)
$Label4 = GUICtrlCreateLabel("Livebox IP/host", 264, 8, 79, 16)
$Input3 = GUICtrlCreateInput("livebox", 264, 24, 81, 21)
$Button3 = GUICtrlCreateButton("Connexion", 352, 22, 59, 24)
GUISetState(@SW_SHOW)
Global $USER = "admin", $LIVEBOX, $PASSWORD, $sContextID 
Global $sDevice_list, $sMenu, $sDefaut_device, $aDevices
$oHTTP = ObjCreate("winhttp.winhttprequest.5.1")
While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            _Logout()
            Exit
        Case $Button3 ;Connexion ********************************************************
            $PASSWORD = GUICtrlRead($Input2)
            $LIVEBOX = GUICtrlRead($Input3)
        
            if ($PASSWORD ="") Then
                msgbox(48,"ERREUR","Veuillez indiquer votre mot de passe Livebox")
            Elseif ($LIVEBOX ="") Then
                msgbox(48,"ERREUR","Veuillez indiquer une IP ou un nom d'hote pour votre LiveBox")
            Else
                if $sContextID<>"" OR auth_livebox()="" then
                    Device_list()
                    GUICtrlSetState($Combo1, $GUI_ENABLE)
                    GUICtrlSetState($Button1, $GUI_ENABLE)
                    GUICtrlSetState($Button2, $GUI_ENABLE)
                    GUICtrlSetData($Combo1, "", "") ; destruction liste précédente
                    If $sMenu = "" Then 
                        $sMenu = "Pas de périphériques à supprimer"
                        $sDefaut_device = "Pas de périphériques à supprimer"
                    Endif
                    GUICtrlSetData($Combo1, $sMenu, $sDefaut_device)
                    ;msgbox(0,"Connexion","Connexion établie.")
                endif
            EndIf
        Case $Button1 ;supprimer 1 périphérique *******************************************
            $sDevice = GUICtrlRead($Combo1)
            $aMac = StringSplit($sDevice,"/")
            if $sDevice<>"" Then
                $sMac = StringReplace($aMac[2]," ","") ; on enelève les espaces
                Delete_Device($sMac)
                msgbox(0,"Suppression","Périphérique supprimé.")
                Device_list()
                GUICtrlSetData($Combo1, "", "") ; destruction liste précédente
                GUICtrlSetData($Combo1, $sMenu, $sDefaut_device)
            Endif
        Case $Button2 ;supprimer tous les périphériques ************************************
            Delete_All()
            msgbox(0,"Suppression","Tous les périphériques ont été supprimés.")
            Device_list()
            GUICtrlSetData($Combo1, "", "") ; destruction liste précédente
            GUICtrlSetData($Combo1, $sMenu, $sDefaut_device)
    EndSwitch
WEnd
Func _Logout()
    $oHTTP.Open("POST", "http://192.168.1.1/logout", False)
    $oHTTP.Send()
    $oHTTP = 0
EndFunc
Func auth_livebox()
    $str = "/authenticate?username=" & $USER & "&password=" & $PASSWORD
    $oHTTP.Open("POST", "http://" & $LIVEBOX & $str, False)
    $oHTTP.SetRequestHeader("Content-Type", "application/json")
    $oHTTP.Send()
    $oReceived = $oHTTP.ResponseText
    $sContextID = StringRegExp($oReceived, 'contextID":"([^"]+)"|()$', 1)[0]
    if $sContextID="" Then
        msgbox(48,"ERREUR"," Mot de passe incorrect : " & $PASSWORD)
        return "NOLOGIN"
    EndIf
EndFunc     ;==>auth_livebox
Func Device_list()
 ; Livebox 2
    $PARAMS = '{"parameters":{}}'
    $oHTTP.Open("POST", "http://" & $LIVEBOX & "/sysbus/Hosts:getDevices", False)
    $oHTTP.SetRequestHeader("X-Context", $sContextID)
    $oHTTP.Send($PARAMS)
    $sDevice_list = $oHTTP.ResponseText
    $aDevices = StringRegExp($sDevice_list, '(?>"physAddress":"([^"]+))(?>.*?hostName":"([^"]+))(?>.*?"active":([^,]+))', 3)
; _ArrayDisplay($aDevices, "tous")
    If isArray($aDevices) then
        $sMenu = ""
        $sDefaut_device = ""
        for $i=0 to ubound($aDevices) - 1 step 3
            if $aDevices[$i+2] = "false" Then   
                If $sDefaut_device = "" Then $sDefaut_device = $aDevices[$i+1] & " / " & $aDevices[$i]
                $sMenu &= $aDevices[$i+1] & " / " & $aDevices[$i] &  "|"
            EndIf
        Next
    Endif
EndFunc ;==>Device_list
Func Delete_Device($Mac_address)
    If not StringRegExp($Mac_address, '^(\w\w:){5}\w\w$') Then
        msgbox(48,"ERREUR","Erreur d'adresse")
    Else
        $PARAMS = '{"parameters":{"physAddress":"' & $Mac_address & '"}}'
        $oHTTP.Open("POST", "http://" & $LIVEBOX & "/sysbus/Hosts:delHost", False)
        $oHTTP.SetRequestHeader("X-Context", $sContextID)
        $oHTTP.Send($PARAMS)
    Endif
EndFunc ;==>Delete_Device
Func Delete_All()
    For $i=0 to ubound($aDevices) - 1 step 3
        if $aDevices[$i+2] = "false" Then
            $PARAMS = '{"parameters":{"physAddress":"' & $aDevices[$i] & '"}}'
            $oHTTP.Open("POST", "http://" & $LIVEBOX & "/sysbus/Hosts:delHost", False)
            $oHTTP.SetRequestHeader("X-Context", $sContextID)
            $oHTTP.Send($PARAMS)
        EndIf
    Next
EndFunc ;==>Delete_All