Aktualizacja adresów IP urządzeń na podstawie serwera dhcpd

  1.  Sposób konfiguracji automatycznego rejestrowania przyznanych adresów IP przez serwer DHCPW celu włączenia automatycznego rejestrowania adresów IP przyznanych przez serwery DHCP należy dokonać konfiguracji serwera DHCP poprzez wprowadzenie instrukcji przekazywania informacji o przyznanych adresach do skryptu, który to skrypt wykona polecenie przekazania informacji poprzez API WFS-T do systemu Procesy5. Skrypt taki wymaga stworzenia użytkownika oraz hasła, który to użytkownik powinien być przydzielony do stanowiska/roli, która posiada uprawnienia do niniejszego procesu.
    1. Zaktualizuj plik /etc/dhcpd.conf
      on commit {
      execute("/sbin/skrypty/dhcp_update_ip", "commit", ClientIP, ClientMac, ClientName);
      }
      
      on release {
      execute("/sbin/skrypty/dhcp_update_ip", "release", ClientIP, ClientMac, ClientName);
      }
      
      on expiry {
      execute("/sbin/skrypty/dhcp_update_ip", "expiry", ClientIP, ClientMac, ClientName);
      }
    2. Stwórz skrypt bash do przekazywania żądań do docelowego skryptu opartego o .php /sbin/skrypty/dhcp_update_ip
      #!/bin/sh
      /sbin/skrypty/dhcp_update_ip.php ${*} & 
    3. Stwórz skrypt php do przekazywania żądań do serwera procesy5 /sbin/skrypty/dhcp_update_ip.php
      #!/usr/bin/env php
      <?php
      
      ######################################################
      # Skrypt aktualizujacy adresu IP w systemie PROCESY5 #
      # ID zasobu - 23966                                  #
      # Bzyk @ 2016-08-23                                  #
      ######################################################
      
      function addZero($s) {
      	return (strlen($s)==1?"0":"").$s;
      }
      
      function tuneMac($mac) {
      	return strtoupper(implode(":", array_map("addZero", explode(":", $mac))));
      }
      
      $ACTION = $_SERVER[argv][1];
      $IP = $_SERVER[argv][2];
      $MAC = tuneMac($_SERVER[argv][3]);
      #$MAC = wordwrap(strtoupper(bin2hex($_SERVER[argv][3])), 2 ,":", true);
      $HOST = $_SERVER[argv][4];
      
      #$ACTION = "commit";
      #$IP = '192.168.1.14';
      #$MAC = 'B4:8B:19:8A:36:5A';
      #$MAC='ac:87:a3:03:3b:9a';
      #$HOST = "test-host";
      
      function error($s,$die) {
      	file_put_contents("/tmp/dhcp-php.log",$s,FILE_APPEND);
      	if ($die) die();
      }
      
      if (!$MAC) error("===\n${IP} - MAC error\n",true);
      
      function writeLog($table,$transaction,$result) {
      	file_put_contents("/tmp/dhcp-php.log","===\n${table} @ " . date('Y-m-d H:i:s', time()) . "\n---\n${transaction}\n---\n${result}\n",FILE_APPEND);
      }
      
      $seProto = "https";
      $seHost = "biuro.biall-net.pl";
      $seLogin = "dhcp_update_ip";
      $sePass = "$$$$$$$$";
      $T_NETWORK_SERVER = 'adm.chelmnet.pl';
      
      
      $transactionHead = '<Transaction
        xmlns="http://www.opengis.net/wfs"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        version="1.0.0"
        service="WFS"
        xmlns:p5_default_db="'.$seProto.'://'.$seHost.'/wfs/default_db"
        xmlns:gml="http://www.opengis.net/gml">
        <Insert xmlns="http://www.opengis.net/wfs">
      ';
      
      $transactionTail = '  </Insert>
      </Transaction>';
      
      $updateUrl = "${seProto}://${seLogin}:${sePass}@${seHost}/SE/version-git/wfs-data.php/default_db/?SERVICE=WFS&VERSION=1.0.0&SRSNAME=EPSG:3003&";
      
      // Aktualizacja tabeli DEVICES
      $TABLE = "DEVICES";
      $filter = "
      <GetFeature xmlns=\"http://www.opengis.net/wfs/2.0\"
        xmlns:p5_default_db=\"${seProto}://${seHost}/wfs/default_db\"
        xmlns:ogc=\"http://www.opengis.net/ogc\"
        xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"
        service=\"WFS\"
        version=\"2.0.2\"
        xsi:schemaLocation=\"http://www.opengis.net/wfs/2.0 http://schemas.opengis.net/wfs/2.0/wfs.xsd\">
        <ogc:Filter>        
          <ogc:And>
            <ogc:PropertyIsEqualTo>
              <ogc:PropertyName>S_HW</ogc:PropertyName>
              <ogc:Literal>${MAC}</ogc:Literal>
            </ogc:PropertyIsEqualTo>
            <ogc:Not>
              <ogc:PropertyIsEqualTo>
                <ogc:PropertyName>A_STATUS</ogc:PropertyName>
                <ogc:Literal>DELETED</ogc:Literal>
              </ogc:PropertyIsEqualTo>
            </ogc:Not>
          </ogc:And>
        </ogc:Filter>
      </GetFeature>";
      #$postdata = array('Filter' => $filter);
      #$opts = array('http' => array('method' => 'POST', 'content' => http_build_query($postdata)));
      $opts=array('http'=>array('method'=>'POST','header'=> "Content-type: application/xml\nContent-Length: " . strlen($filter) . "\r\n",'content'=>$filter));
      $context = stream_context_create($opts);
      
      $url = "${seProto}://${seLogin}:${sePass}@${seHost}/SE/version-git/wfs-data.php/default_db/?SERVICE=WFS&VERSION=1.0.0&REQUEST=GetFeature&TYPENAME=p5_default_db:${TABLE}&propertyName=ID,S_IP";
      $result = @file_get_contents($url, false, $context) or error("===\nAPI error\n",true);
      $xml = simplexml_load_string($result);
      
      $findID = $xml->xpath("/wfs:FeatureCollection/gml:featureMember/p5_default_db:${TABLE}/p5_default_db:ID");
      $findIP = $xml->xpath("/wfs:FeatureCollection/gml:featureMember/p5_default_db:${TABLE}/p5_default_db:S_IP");
      
      $DEVICES_ID = (int) $findID[0][0];
      $DEVICES_IP = $findIP[0][0];
      
      $xmlns = 'xmlns="'.$seProto.'://'.$seHost.'/wfs/default_db/'.$TABLE.'"';
      if ($DEVICES_ID && ($IP != $DEVICES_IP)) {
      	$transactionBody = "      <ID ${xmlns}>${DEVICES_ID}</ID>\n";
      	$transactionBody.= "      <S_IP ${xmlns}>${IP}</S_IP>\n";
      	$transaction = $transactionHead . "    <${TABLE} ${xmlns}>\n" . $transactionBody . "    </${TABLE}>\n" . $transactionTail;
      	$opts = array('http' => array('method' => 'POST', 'header' => "Content-type: application/xml\nContent-Length: " . strlen($transaction) . "\n", 'content' => $transaction));
      	$context = stream_context_create($opts);
      	$result = @file_get_contents($updateUrl, false, $context) or error("***\nAPI error\n",false);
      	writeLog($TABLE,$transaction,$result);
      #	file_put_contents("/tmp/dhcp-php.log","===\nDEVICES\n---\n${transaction}\n---\n${result}\n",FILE_APPEND);
      #echo $transaction."\n";
      }
      
      // Aktualizacja tabeli DEVICES_HW
      $TABLE = "DEVICES_HW";
      $filter = "
      <GetFeature xmlns=\"http://www.opengis.net/wfs/2.0\"
        xmlns:p5_default_db=\"${seProto}://${seHost}/wfs/default_db\"
        xmlns:ogc=\"http://www.opengis.net/ogc\"
        xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"
        service=\"WFS\"
        version=\"2.0.2\"
        xsi:schemaLocation=\"http://www.opengis.net/wfs/2.0 http://schemas.opengis.net/wfs/2.0/wfs.xsd\">
        <ogc:Filter>        
          <ogc:And>
            <ogc:PropertyIsEqualTo>
              <ogc:PropertyName>S_HW</ogc:PropertyName>
              <ogc:Literal>${MAC}</ogc:Literal>
            </ogc:PropertyIsEqualTo>
            <ogc:PropertyIsEqualTo>
              <ogc:PropertyName>T_NETWORK_SERVER</ogc:PropertyName>
              <ogc:Literal>${T_NETWORK_SERVER}</ogc:Literal>
            </ogc:PropertyIsEqualTo>
          </ogc:And>
        </ogc:Filter>
      </GetFeature>";
      #$postdata = array('Filter' => $filter);
      #$opts = array('http' => array('method' => 'POST', 'content' => http_build_query($postdata)));
      $opts=array('http'=>array('method'=>'POST','header'=> "Content-type: application/xml\nContent-Length: " . strlen($filter) . "\n",'content'=>$filter));
      $context = stream_context_create($opts);
      
      $url = "${seProto}://${seLogin}:${sePass}@${seHost}/SE/version-git/wfs-data.php/default_db/?SERVICE=WFS&VERSION=1.0.0&REQUEST=GetFeature&TYPENAME=p5_default_db:${TABLE}&propertyName=ID,S_IP,A_STATUS,DEVICES_ID";
      $result = @file_get_contents($url, false, $context) or error("===\nAPI error\n",true);
      $xml  =simplexml_load_string($result);
      
      $findID = @$xml->xpath("/wfs:FeatureCollection/gml:featureMember/p5_default_db:${TABLE}/p5_default_db:ID");
      $findIP = @$xml->xpath("/wfs:FeatureCollection/gml:featureMember/p5_default_db:${TABLE}/p5_default_db:IP");
      $findA_STATUS = @$xml->xpath("/wfs:FeatureCollection/gml:featureMember/p5_default_db:${TABLE}/p5_default_db:A_STATUS");
      $findDEVICES_ID = @$xml->xpath("/wfs:FeatureCollection/gml:featureMember/p5_default_db:${TABLE}/p5_default_db:DEVICES_ID");
      
      $DEVICES_HW_ID = (int) $findID[0][0];
      $DEVICES_HW_IP = $findIP[0][0];
      $DEVICES_HW_A_STATUS = $findA_STATUS[0][0];
      $DEVICES_HW_DEVICES_ID = (int) $findDEVICES_ID[0][0];
      
      $transactionBody = "";
      $xmlns = 'xmlns="'.$seProto.'://'.$seHost.'/wfs/default_db/'.$TABLE.'"';
      $ACTIONS = array(
      	"commit" => array("status" => "NORMAL", "desc" => "Przyznano adres IP"),
      	"release" => array("status" => "OFF_SOFT", "desc" => "Odlaczono urzadzenie"),
      	"expiry" => array("status" => "OFF_SOFT", "desc" => "Uplynal czas dzierzawy")
      );
      
      if ($DEVICES_HW_ID) $transactionBody .= "      <ID ${xmlns}>${DEVICES_HW_ID}</ID>\n";
      else {
      	$transactionBody .= "      <S_HW ${xmlns}>${MAC}</S_HW>\n";
      	$transactionBody .= "      <T_NETWORK_SERVER ${xmlns}>${T_NETWORK_SERVER}</T_NETWORK_SERVER>\n";
      }
      $transactionBody .= "      <S_IP ${xmlns}>${IP}</S_IP>\n";
      $transactionBody .= "      <A_STATUS ${xmlns}>".$ACTIONS[$ACTION]["status"]."</A_STATUS>\n";
      $transactionBody .= "      <A_STATUS_INFO ${xmlns}>".$ACTIONS[$ACTION]["desc"]."</A_STATUS_INFO>\n";
      if ($ACTION == "commit") $transactionBody .= "      <S_HOST ${xmlns}>".$HOST."</S_HOST>\n";
      if ($DEVICES_ID) $transactionBody .= "      <DEVICES_ID ${xmlns}>${DEVICES_ID}</DEVICES_ID>\n";
      
      
      $transaction = $transactionHead . "    <${TABLE} ${xmlns}>\n" . $transactionBody . "    </${TABLE}>\n" . $transactionTail;
      $opts = array('http' => array('method' => 'POST', 'header' => "Content-type: application/xml\nContent-Length: " . strlen($transaction) . "\n", 'content' => $transaction));
      $context = stream_context_create($opts);
      $result = @file_get_contents($updateUrl, false, $context) or error("***\nAPI error\n",false);
      writeLog($TABLE,$transaction,$result);
      #file_put_contents("/tmp/dhcp-php.log","===\nDEVICES_HW\n---\n${transaction}\n---\n${result}\n",FILE_APPEND);
      #echo $transaction."\n";
      
      ?>