#Region ;**** Directives created by AutoIt3Wrapper_GUI **** #AutoIt3Wrapper_icon=lan.ico #AutoIt3Wrapper_outfile=wol.exe #EndRegion ;**** Directives created by AutoIt3Wrapper_GUI **** #include #include #include #include #include #include #include #include #include #Include ;Récupération Mac adresse de la carte connexion au réseau local par WMI $objWMIService = ObjGet("winmgmts:\\" & @ComputerName & "\root\CIMV2") $colItems = $objWMIService.ExecQuery( "SELECT * FROM Win32_NetworkAdapter where NetConnectionID = 'Connexion au réseau local'") For $objItem in $colItems $mac = $objItem.MACAddress Next ;Requete afin de récupérer le masque de sous réseau de la carte connexion au réseau local par WMI $objWMIService = ObjGet("winmgmts:\\" & @ComputerName & "\root\cimv2") $colNetAdapters = $objWMIService.ExecQuery("Select * from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE and MACAddress = '" & $mac & "'") Dim $tab[2] For $objItem in $colNetAdapters $tab = $objItem.IPSubnet Next $pas =_ArrayToString($tab, ",") ;Recherce du caractère "," utilisé dans windows vista ex: 255.255.255.0,64 sous xp ilya seulement 255.255.255.0 $pos = StringInStr($pas, ",") if $pos <> 0 then ;si caractère trouvé extraction du masque de sous réseau $pas = StringLeft($pas, $pos - 1) EndIf ;Masque de sous réseau $arrSubnetMask = StringSplit($pas, ".") If IsArray($arrSubnetMask) <> 1 Then Exit EndIf $arrIPAddress = StringSplit(@IPAddress1, ".") If $arrSubnetMask[1] = 255 Then $strBroadcastIP = $arrIPAddress[1] Else $strBroadcastIP = 255 - $arrSubnetMask[1] EndIf For $i = 2 to 4 If $arrSubnetMask[$i] = 255 Then $strBroadcastIP = $strBroadcastIP & "." & $arrIPAddress[$i] Else $strBroadcastIP = $strBroadcastIP & "." & 255 - $arrSubnetMask[$i] EndIf Next $ServeurSQL = "localhost" ;Nom du serveur SQL dans ce cas poste local $tableName = "wol" ;Nom de la base de données $query = "Select * From " & $tableName & " order by netbios" #Region ### START Koda GUI section ### Form=C:\Documents and Settings\Administrateur.MONTADY\Bureau\Programmation\AutoIt v3\Autoit\WOL\frmwol.kxf $frmwol = GUICreate("Wake on Lan", 640, 480) GUISetFont(10, 400, 0, "Tahoma") $listwake = GUICtrlCreateListView("NetBios | IP | Mac", 16, 20, 475, 401); Définition des entêtes de colonnes ;Définition de la largeur des colonnes GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 0, 120) GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 1, 120) GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 2, 210) ;Appel de la fonction de remplissage de la listview RempliListe() $cmdOK = GUICtrlCreateButton("OK", 500, 40, 135, 40, $WS_GROUP) GUICtrlSetFont(-1, 11, 400, 0, "Tahoma") $cmdping = GUICtrlCreateButton("Ping", 500, 85, 135, 40, $WS_GROUP) GUICtrlSetFont(-1, 11, 400, 0, "Tahoma") $cmdAjout = GUICtrlCreateButton("Ajouter...", 10, 430, 150, 40, $WS_GROUP) GUICtrlSetFont(-1, 11, 400, 0, "Tahoma") $cmdModif = GUICtrlCreateButton("Modifier... ", 170, 430, 150, 40, $WS_GROUP) GUICtrlSetFont(-1, 11, 400, 0, "Tahoma") $cmdSuppr = GUICtrlCreateButton("Supprimer", 330, 430, 150, 40, $WS_GROUP) GUICtrlSetFont(-1, 11, 400, 0, "Tahoma") $cmdfermer = GUICtrlCreateButton("Fermer", 520, 420, 109, 57, $WS_GROUP) GUICtrlSetFont(-1, 11, 400, 0, "Tahoma") GUICtrlSetState($listwake, $GUI_FOCUS) GUISetState(@SW_SHOW) #EndRegion ### END Koda GUI section ### While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit Case $cmdfermer Exit Case $cmdOK ;Bouton d'envoi du réveil ;Lecture du nom netbios de la listview $str = GUICtrlRead(GUICtrlRead($listwake)) $val2 = StringInStr($str, "|", 0, 1) $Netbiosname = Stringleft($str, $val2 - 1) if $Netbiosname = "" Then ;Si aucune ligne de la listview sélectionnée Msgbox (4096 + 16, "Message important", "Veuillez choisir un poste à démarrer SVP!") Else $adoCon = ObjCreate ("ADODB.Connection") $adoCon.Open ("Provider=SQLNCLI.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=wol;Data Source=" & $ServeurSQL) $adoRs = ObjCreate ("ADODB.Recordset") $adoRs.CursorType = 1 $adoRs.LockType = 3 ;Requete sur le nom netbios pour extraction de l'adresse MAC $req = "select * from wol where netbios = '" & $Netbiosname & "'" $adoRs.Open ($req, $adoCon) $adoRs.movefirst $MACAddress = $adoRs.Fields("mac").value $ip = $adoRs.Fields("ip").value $adoRs.Close $adoCon.Close ;Mise en forme de l'adresse MAC $MACAddress = StringReplace($MACAddress, ":", "") $MACAddress = StringReplace($MACAddress, "-", "") UDPStartUp() $connexion = UDPOpen($strBroadcastIP, 7) $res = UDPSend($connexion, GenerateMagicPacket($MACAddress)) MsgBox(4096 + 64, "Réveil initié", "Le réveil de " & $Netbiosname & " a été initié sur la Mac Adresse " & StringUpper($MACAddress)) UDPCloseSocket($connexion) UDPShutdown() Run ("cmd") sleep(500) Send ("ping " & $ip & " -t {ENTER}") EndIf Case $cmdModif ;Lecture du nom netbios dans la listview $modif = _GUICtrlListView_GetItemTextString($listwake, -1) $val2 = StringInStr($modif, "|", 0, 1) $modif = Stringleft($modif, $val2 - 1) if $modif <> "" Then ;Si une ligne a été sélectionnée Ajout($modif) ;Appel de la fonction qui ouvrira le formulaire de saisie Else ;Si aucune ligne sélectionnée Msgbox (4096 + 16, "Message important", "Veuillez choisir un poste à modifier SVP!") EndIf Case $cmdAjout Ajout("ajout") Case $cmdSuppr ;Lecture du nom netbios dans la listview $suppr = _GUICtrlListView_GetItemTextString($listwake, -1) $val2 = StringInStr($suppr, "|", 0, 1) $suppr = Stringleft($suppr, $val2 - 1) if $suppr <> "" Then ;Si une ligne a été sélectionnée Supprime($suppr) ;Appel de la fonction qui supprimera les données Else ;Si aucune ligne sélectionnée Msgbox (4096 + 16, "Message important", "Veuillez choisir un poste à supprimer SVP!") EndIf case $cmdping ;Lecture du nom netbios de la listview $str = GUICtrlRead(GUICtrlRead($listwake)) $val2 = StringInStr($str, "|", 0, 1) $Netbiosname = Stringleft($str, $val2 - 1) if $Netbiosname = "" Then ;Si aucune ligne de la listview sélectionnée Msgbox (4096 + 16, "Message important", "Veuillez choisir un poste à pinguer!") Else $adoCon = ObjCreate ("ADODB.Connection") $adoCon.Open ("Provider=SQLNCLI.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=wol;Data Source=" & $ServeurSQL) $adoRs = ObjCreate ("ADODB.Recordset") $adoRs.CursorType = 1 $adoRs.LockType = 3 ;Requete sur le nom netbios pour extraction de l'adresse MAC $req = "select * from wol where netbios = '" & $Netbiosname & "'" $adoRs.Open ($req, $adoCon) $adoRs.movefirst $ip = $adoRs.Fields("ip").value $adoRs.Close $adoCon.Close Run ("cmd") sleep(500) Send ("ping " & $ip & " -t {ENTER}") EndIf EndSwitch WEnd ;################################################################################# ; Fonctions ;################################################################################# ; This function convert a MAC Address Byte (e.g. "1f") to a char Func HexToChar($strHex) Return Chr(Dec($strHex)) EndFunc ;################################################################################# ; This function generate the "Magic Packet" Func GenerateMagicPacket($strMACAddress) $MagicPacket = "" $MACData = "" For $p = 1 To 11 Step 2 $MACData = $MACData & HexToChar(StringMid($strMACAddress, $p, 2)) Next For $p = 1 To 6 $MagicPacket = HexToChar("ff") & $MagicPacket Next For $p = 1 To 16 $MagicPacket = $MagicPacket & $MACData Next Return $MagicPacket EndFunc ;################################################################################# Func Ajout($modif) if $modif <> "ajout" Then $adoCon = ObjCreate ("ADODB.Connection") $adoCon.Open ("Provider=SQLNCLI.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=wol;Data Source=" & $ServeurSQL) $adoRs = ObjCreate ("ADODB.Recordset") $adoRs.CursorType = 1 $adoRs.LockType = 3 $req = "select * from wol where netbios = '" & $modif & "'" $adoRs.Open ($req, $adoCon) $Netbiosname = $adoRs.Fields("Netbios").value $IPAddresse = $adoRs.Fields("ip").value $MACAddress = $adoRs.Fields("mac").value $adoRs.Close $adoCon.Close EndIf #Region ### START Koda GUI section ### Form=C:\Documents and Settings\Administrateur.MONTADY\Bureau\Programmation\AutoIt v3\Autoit\WOL\frmNPoste.kxf $frmNPoste = GUICreate("Nouveau poste", 496, 150) GUISetFont(10, 400, 0, "Tahoma") if $modif = "ajout" then $InNetbios = GUICtrlCreateInput("", 152, 20, 181, 24) Else $InNetbios = GUICtrlCreateInput(StringUpper($Netbiosname), 152, 20, 181, 24) EndIf $IPAddress = _GUICtrlIpAddress_Create($frmNPoste, 152, 52, 177, 25) if $modif = "ajout" then _GUICtrlIpAddress_Set($IPAddress, "0.0.0.0") Else _GUICtrlIpAddress_Set($IPAddress, $IPAddresse) EndIf if $modif = "ajout" then $InMAc = GUICtrlCreateInput("", 152, 88, 189, 24) Else $InMAc = GUICtrlCreateInput(StringUpper($MACAddress), 152, 88, 189, 24) EndIf $lblNetbios = GUICtrlCreateLabel("Netbios:", 24, 24, 123, 20, $SS_RIGHT) $lblIP = GUICtrlCreateLabel("IP:", 24, 56, 123, 20, $SS_RIGHT) $lblmac = GUICtrlCreateLabel("MAC Adresse:", 24, 92, 123, 20, $SS_RIGHT) $cmdOK2 = GUICtrlCreateButton("OK", 376, 20, 101, 41, $WS_GROUP) $cmdAnnuler = GUICtrlCreateButton("Annuler", 376, 68, 101, 41, $WS_GROUP) GUISetState() #EndRegion ### END Koda GUI section ### While 1 $nMsg2 = GUIGetMsg() Select Case $nMsg2 = $GUI_EVENT_CLOSE if WinActive("Nouveau poste") Then GUIDelete($frmNPoste) ExitLoop EndIf Case $nMsg2 = $cmdAnnuler if WinActive("Nouveau poste") Then GUIDelete($frmNPoste) ExitLoop EndIf Case $nMsg2 = $cmdOK2 $adoCon = ObjCreate ("ADODB.Connection") $adoCon.Open ("Provider=SQLNCLI.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=wol;Data Source=" & $ServeurSQL) $adoRs = ObjCreate ("ADODB.Recordset") $adoRs.CursorType = 1 $adoRs.LockType = 3 if $modif = "ajout" then $adoRs.Open ($query, $adoCon) if $adoRs.recordcount <> 0 Then $adoRs.movefirst Endif $adoRs.Addnew Else $adoRs.Open ($req, $adoCon) $adoRs.movefirst EndIf $adoRs.Fields("Netbios").value = GUICtrlRead($InNetbios) $adoRs.Fields("ip").value = _GUICtrlIpAddress_Get($IPAddress) $adoRs.Fields("mac").value = GUICtrlRead($InMAc) $adoRs.Update Sleep(500) RempliListe() Sleep(500) GUIDelete($frmNPoste) ExitLoop EndSelect WEnd EndFunc ;################################################################################# Func RempliListe() _GUICtrlListView_DeleteAllItems($listwake) ;Effacement de la listview $adoCon = ObjCreate ("ADODB.Connection") $adoCon.Open ("Provider=SQLNCLI.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=wol;Data Source=" & $ServeurSQL) $adoRs = ObjCreate ("ADODB.Recordset") $adoRs.CursorType = 3 $adoRs.LockType = 3 $adoRs.Open ($query, $adoCon) if $adoRs.recordcount <> 0 Then $adoRs.movefirst ;Remplissage de la listview avec les données Do $Netbios= StringUpper($adoRs.Fields("Netbios").value) $IPAddress= $adoRs.Fields("ip").value $MACAddresse = StringUpper($adoRs.Fields("mac").value) GUICtrlCreateListViewItem($Netbios & "|" & $IPAddress & "|" & $MACAddresse, $listwake) $adoRs.Movenext Until $adoRs.EOF $adoRs.Close $adoCon.Close Endif EndFunc ;################################################################################# Func Supprime($suppr) if MsgBox (4096 + 16 + 4, "Confirmation de suppression", "Voulez vous vraiment supprimer le poste " & $suppr & " ?") = 6 then $adoCon = ObjCreate ("ADODB.Connection") $adoCon.Open ("Provider=SQLNCLI.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=wol;Data Source=" & $ServeurSQL) $adoRs = ObjCreate ("ADODB.Recordset") $adoRs.CursorType = 1 $adoRs.LockType = 3 $reqsuppr = "select * from wol where netbios ='" & $suppr & "'" $adoRs.Open ($reqsuppr, $adoCon) $adoRs.movefirst ;Suppression des données Do $adoRs.Delete $adoRs.Movenext Until $adoRs.EOF $adoRs.Close $adoCon.Close Sleep(100) RempliListe() Sleep(100) EndIf EndFunc