Kanpoko Kontrola

Interesgarria izan daiteke Internet Atzipen Kontrola kanpoko sistemekin kudeatzea. Hau da: Web orria erabili beharrean, beste sistemaren bat erabiltzea. Banda megnetikodun txartel irakurlea, ahotsa, ... Gaur egun sistema informatiko oso potenteak daude, oso txikiak eta hainbat sensorerekin.

Hau errazteko, web orria dagoen tokian, beste programa bat jarriko dugu, eta programa hori HTTP protokoloaren bitartez deitu dezakegu. Honela, edozein programazio lengoaian egin dezakegu aplikazio bat IAK itxi edo irekitzeko. Baita ere, cURL erabil dezakegu. Programa hau, /var/www/iak/iak_getset_room.php izango da. /var/www/iak/iak.php programaren kasuan bezala, erabiltzaile izena eta pasahitza behar da erabiltzeko.

Hona hemen programa:

<?php

/*================================================================================================
Get / Set room state. For external interface.
--------------------------------------------------------------------------------------------------
LHIAK 6.0
2014-05-21
    Adapted to SQUID3 and LHIAK 6.0. SQUID 2 support droped. Cosmetic changes.
    Changed parameters. Now, parameters are 'room' and 'action'.
    'room' values are '01', '02', ......., '99'
    'action' options are 'status', 'open', 'close' and 'switch'.
LHIAK 5.2
2014-03-03
    First version
2014-03-06
    Changed parameters. Now, parameters are 'room' and 'action'.
    'action' options are 'get', 'set', 'reset' and 'switch'.
================================================================================================*/

$PROG = "sudo /bin/sed";
$CONF_FILE = "/etc/squid3/iak.rooms.conf";

$SQUID3RELOAD = "sudo service squid3 reload";

$LOGFILE = "/var/log/iak/iak_getset_room.log";

$ADDR = $_SERVER ['REMOTE_ADDR'];
$host = gethostbyaddr($ADDR);
$user = getenv('REMOTE_USER');

$nowdate = date('[Y-m-d H:i]');

$act = 0;

if (isset($_GET['room']) && isset($_GET['action'])){

    $classroomnumber = $_GET['room'];
    $action = $_GET['action'];

    $act = 1;
}

if (isset($_POST['room']) && isset($_POST['action'])){

    $classroomnumber = $_POST['room'];
    $action = $_POST['action'];

    $act = 1;
}

if ($act) {
    $room = "room-" . $classroomnumber;

    exec("sudo /bin/echo  $nowdate - $user - $host - $ADDR - Classroom: $room - Action: $action >> $LOGFILE");

    $state = exec("cat $CONF_FILE | grep $room | grep http_access | awk '{print $2}'");
    switch ($action) {
        case "open":
            exec("sudo /bin/sed -i s/http_access\ deny\ $room/http_access\ allow\ $room/ $CONF_FILE");
            break;
        case "close":
            exec("sudo /bin/sed -i s/http_access\ allow\ $room/http_access\ deny\ $room/ $CONF_FILE");
            break;
        case "switch":
            if ($state == "allow")
                exec("sudo /bin/sed -i s/http_access\ allow\ $room/http_access\ deny\ $room/ $CONF_FILE");
            else
                exec("sudo /bin/sed -i s/http_access\ deny\ $room/http_access\ allow\ $room/ $CONF_FILE");
            break;
    }

    exec("$SQUID3RELOAD");
}

$state = exec("cat $CONF_FILE | grep $room | grep http_access | awk '{print $2}'");
if ($state == "allow") echo "1\n"; else echo "0\n";
?>

Aginduaren parametroak:

- room (01, 02, 03, ..., 99)
- action (status, open, close, switch)

Erabilpen adibideak:

   curl -u user1:user1 http://iak.nire-eskola.net/iak/iak_getset_room.php?room=02

   curl -u user1:user1 --data "room=02&action=close" http://iak.nire-eskola.net/iak/iak_getset_room.php

   curl -u user1:user1 --data "room=02&action=switch" http://iak.nire-eskola.net/iak/iak_getset_room.php

   curl -u user1:user1 -d "room=02&action=status" http://iak.nire-eskola.net/iak/iak_getset_room.php


Ongi izan!