source: admin/WebConsole/comandos/gestores/wakeonlan_repo.php @ 5d05b06

Last change on this file since 5d05b06 was a35b7c4, checked in by OpenGnSys Support Team <soporte-og@…>, 5 years ago

#990 Use client broadcast address on WoL

Some universities have computers in a different subnet where the
ogServer is, but ogServer WoL functionality only supported to send
packet to its own subnets. This commit solves this.

Now ogServer sends two WoL packets per client, one with the broadcast
address of the interface indicated in the config file, the other with
the broadcast address calculated with the address and netmask of the
client.

To ensure that the second WoL works correctly you must configure
correctly the IP and netmask of the clients. Also, you have to configure
the network of your organization to route WoL packet to the correct
subnet.

  • Property mode set to 100644
File size: 2.7 KB
RevLine 
[3bf09ba]1<?php
[15acccd]2// Fichero con funciones para trabajar con el webservice
[3bf09ba]3include_once("../../includes/restfunctions.php");
[15acccd]4
5/**
6        En este punto disponemos de tres variables indicando las ips, las macs y las ids de los
7        ordenadores, este script obtiene cual es el repositorio
8        $cadenaid
9        $cadenaip
10        $cadenamac
11
[560455a]12 */
13
14//Multicast or Unicast
15preg_match_all('!\d{1}!', $atributos, $matches);
16
[15acccd]17// Capturamos todas las ids
18$macs = explode(";",$cadenamac);
[560455a]19$ips = explode(';',$cadenaip);
[a35b7c4]20$netmasks = explode(';', $netmasks_string);
[560455a]21
[a35b7c4]22wol($matches[0][0], $macs, $ips, $netmasks);
[560455a]23
[15acccd]24// Recorremos las ids y vemos cual es la ip del repositorio
25$repos = array();
26$reposAndMacs = array();
27foreach($macs as $mac){
28        $cmd->texto="SELECT repo.ip, repo.apikey FROM ordenadores o,repositorios repo WHERE o.mac=\"".$mac."\" AND o.idrepositorio=repo.idrepositorio";
29        $rs=new Recordset;
30        $rs->Comando=&$cmd;
31    if (!$rs->Abrir())
32        return; // Error al abrir recordset
33    while (!$rs->EOF){
34                $repo = $rs->campos["ip"];
35                if(!existREPO($repo, $repos)){
36                        $repos[count($repos)]=$repo;
37                }
38                // Una vez creado el repo se asigna la mac del pc a su lista
39                if(empty($reposAndMacs[$repo])){
40                        $reposAndMacs[$repo] = array();
41                        $reposAndMacs[$repo]["apikey"] = $rs->campos["apikey"];
42                }
43                // Modificar la mac añadiendo ":" cada dos caracteres
44                $tmp = substr_replace($mac, ":", 2, 0);
45                $tmp = substr_replace($tmp, ":", 5, 0);
46                $tmp = substr_replace($tmp, ":", 8, 0);
47                $tmp = substr_replace($tmp, ":", 11, 0);
48                $tmp = substr_replace($tmp, ":", 14, 0);
49
50                $reposAndMacs[$repo][count($reposAndMacs[$repo])] = $tmp;
51            $rs->Siguiente();
52        }
53        $rs->Cerrar();
54}
[2b00219]55
[15acccd]56// En este punto tenemos un array con todos los repos y cada uno de ellos con una lista de todas las macs que deben arrancar
57// Recorremos cada uno de ellos
58foreach($reposAndMacs as $repo => $macs){
59        // En el array de $macs tenemos la clave "apikey"
[2b00219]60        if($macs["apikey"] !== "") {
[15acccd]61                $apiKeyRepo = $macs["apikey"];
62                unset($macs["apikey"]);
[3bf09ba]63                // Componer datos de conexión para el repositorio.
64                $urls[$repo]['url'] = "https://$repo/opengnsys/rest/repository/poweron";
65                $urls[$repo]['header'] = array('Authorization: '. $apiKeyRepo);
[2b00219]66                $urls[$repo]['post'] = '{"macs": ["' . implode('","', $macs) . '"], "ips": ["' . str_replace(';', '","', $cadenaip) .
67                        '"], "mar": "' . $matches[0][0] . '"}';
[15acccd]68        }
[2b00219]69        else {
[3bf09ba]70                $avisoRepo = true;
[15acccd]71        }
72}
[3bf09ba]73// Enviar petición múltiple a los repositorios afectados.
74if (isset($urls)) {
75        $result = multiRequest($urls);
76        // Comprobar respuesta.
77        foreach ($result as $repo => $res) {
78                if ($res['code'] != 200) {
79                        $avisoRepo = true;
80                }
81        }
82}
83
[15acccd]84
85function existREPO($repo, $repos){
86        $found=false;
87        $index = 0;
88        while(!$found && $index < count($repos)){
89                $r = $repos[$index];
[0ad92e4]90                $index++;
[15acccd]91                if($r == $repo)
92                        $found=true;
93        }
94        return $found;
95}
96
[b6ec162]97
Note: See TracBrowser for help on using the repository browser.