Petición
| From: | Luis Martin-Santos | Date: | Mon, 01 Mar 2004 16:02:19 +0000 |
| Subject: | Petición | ||
| Groups: | php.general.es | ||
| Request: | Send a blank email to general-es+get-46116@lists.php.net to get a copy of this message | ||
Esta tarde me han pedido que implementase un parser para los
archivos de interfaces que vienen en la debian (/etc/network/interfaces)
lo he escrito en PHP y este es el resultado. Parece un buen ejemplo de
como usar objetos para cosas cotidianas :D
----------------- ifs.php ---------------------
<?php
$config_ifacefile = "/root/interfaces";
class iface
{
var $name;
var $type;
var $address;
var $netmask;
var $network;
var $broadcast;
var $gateway;
function iface($name)
{
global $config_ifacefile;
$str = file(($config_ifacefile ? $config_ifacefile :
"/etc/network/interfaces"));
for ($i=0;$i<count($str);$i++)
{
$w = explode (" ",rtrim(ltrim($str[$i]))); // Ni con Skip sale más limpio
if ($w[0] == "iface" && $w[1] == $name)
{
$this->name = $w[1];
$this->type = $w[3];
while ($sw[0] != "iface")
{
$i++;
$sw = explode (" ",rtrim(ltrim($str[$i])));
switch ($sw[0])
{
case "address":
$this->address = $sw[1];
break;
case "netmask";
$this->netmask = $sw[1];
break;
case "network";
$this->network = $sw[1];
break;
case "broadcast";
$this->broadcast = $sw[1];
break;
case "gateway";
$this->gateway = $sw[1];
break;
}
if ($i > count ($str)) return ;
}
}
}
}
function changeip($ip)
{
$this->address = $ip;
}
function changemask($mask)
{
$this->netmask = $mask;
}
function changegateway($gateway)
{
$this->gateway = $gateway;
}
function render()
{
$a = "
iface ".$this->name." inet ".$this->type."
".($this->address ? "address " . $this->address : "")."
".($this->netmask ? "netmask " . $this->netmask: "")."
".($this->network ? "network " . $this->network : "")."
".($this->broadcast ? "broadcast " . $this->broadcast :
"")."
".($this->gateway ? "gateway " . $this->gateway: "")."
";
return $a;
}
}
class iface_pipelines
{
var $ifobjs;
function iface_pipelines ($ifaces_names)
{
for ($i=0;$i<count($ifaces_names); $i++)
{
$this->ifobjs[$i] = new iface($ifaces_names[$i]);
}
}
function render()
{
for ($i=0;$i<count($this->ifobjs);$i++)
{
$a .= $this->ifobjs[$i]->render();
}
return $a;
}
}
$int[0] = "eth0";
$int[1] = "lo";
$interfaces = new iface_pipelines($int);
print_r($interfaces);
echo "<pre>".$interfaces->render()."</pre>";