function genSectionEntete() {
$content = "default:\n";
$content .= " community: public\n";
$content .= " snmpport: 161\n\n";
$buffer = "
Section Entête: |
";
$buffer .= "default: ";
$buffer .= "community: public ";
$buffer .= "snmpport: 161 |
";
echo $buffer ;
return $content ;
}
function genSectionSwitch() {
$content = "switch:\n";
$buffer = "Section switch |
";
$resultat = selectSwitch("") ;
if ( !is_string($resultat) ) {
while ( $rowSwitch = mysql_fetch_assoc($resultat) ) {
$content .= " - hostname: " . $rowSwitch['idSwitch'] . "\n";
$content .= " location: " . $rowSwitch['location'] . "\n";
$content .= " level: " . $rowSwitch['level'] . "\n";
$content .= " portignore: \n";
$content .= "\n\n\n";
$buffer .= "".$rowSwitch['idSwitch']." "
.$rowSwitch['location']." "
.$rowSwitch['level']." "
.$rowSwitch['portignore']." |
";
}
}
echo $buffer ;
return $content;
}
function genSectionNetwork() {
$content = "network:\n";
$buffer = "Section Netwotk |
";
$resultat = selectNetwork("") ;
if ( !is_string($resultat) ) {
//TODO: attention je n'ai pas pris en compte dans la génération du fichier plusieurs plage reseau pour un nom reseau
while ( $rowNetwork = mysql_fetch_assoc($resultat) ) {
$content .= " " . $rowNetwork['nameNetwork'] . ":\n";
$content .= " ip-subnet:\n";
$content .= " - add: ".$rowNetwork['idNetwork'] . "\n";
$content .= " interface: " . $rowNetwork['interface'] . "\n";
$content .= " main-router: " . $rowNetwork['mainRouter'] . "\n";
$content .= "\n\n\n";
$buffer .="".$rowNetwork['nameNetwork']." "
."ip-subnet: "
."- add: ".$rowNetwork['idNetwork']." "
."interface: ".$rowNetwork['interface']." "
."main-router: ".$rowNetwork['mainRouter'] ." |
";
}
}
echo $buffer ;
return $content ;
}
function genSectionRouter() {
$content = "router:\n";
$filter = "type = 'routeur'";
$resultat = selectDevice($filter) ;
if ( !is_string($resultat) ) {
while ( $rowDev = mysql_fetch_assoc($resultat) ) {
$content .= " - hostname: " . $rowDev['hostname'] . "\n";
$content .= " mac-address: " . $rowDev['mac'] . "\n";
$buffer .= " - hostname: ".$rowDev['hostname']." "
."mac-address: ".$rowDev['mac']." |
";
}
}
echo $buffer ;
$content .="\n\n";
return $content ;
}
function genKlaskConf() {
$fileKlask = "./klask/klask.conf";
if (!$handle=fopen($fileKlask, "w+")) {
$buffer = "Open file $fileKlask: [FAILED]";
return $buffer;
}
$content = genSectionEntete() ;
$content .= genSectionNetwork() ;
$content .= genSectionRouter() ;
$content .= genSectionSwitch() ;
if (!fwrite($handle, $content)) {
$buffer = "Write in file $fileKlask: [FAILED]";
return $buffer;
}
}
function klaskCommand($idRight) {
$pathKlask = "sudo /usr/sbin/klask" ;
$buffet = "";
if ($idRight == "exportdb" ) {
$command=exec($pathKlask." ".$idRight , $ret) ;
$cpt=0;
$buffer = "Switch | Ports | Hostname | IP | Mac | Date |
";
foreach ($ret as $line) {
$cpt++;
if ($cpt > 2) {
$buffer2 = str_replace('[ ]+', ' ' , trim($line));
$Tsplit = split('[ ]+', $buffer2);
$buffer .= "".trim($Tsplit[0]) ." | ". trim($Tsplit[1]) . " | ". trim($Tsplit[3]) . " | " . trim($Tsplit[4]) ." | " . trim($Tsplit[5]) . " | " . trim($Tsplit[6]) ." " . $Tsplit[7] ." |
";
}
}
return $buffer ;
}else{
if ($idRight == "updatesw") {
$command=exec($pathKlask." ".$idRight , $ret) ;
$buffer = "";
foreach ( $ret as $line ) {
$buffer .= "$line
";
}
return $buffer ;
}else{
if ($idRight == "updatedb") {
$command=exec($pathKlask." ".$idRight , $ret) ;
$buffer = "";
foreach ( $ret as $line ) {
$buffer = "$line
";
}
return $buffer ;
}else{
if ($idRight == "exportsw") {
$command=exec($pathKlask." ".$idRight , $ret) ;
$buffer = "";
foreach ( $ret as $line ) {
if (fnmatch("Switch parent and children port inter-connection", $line)) {
$buffer .= "Switch parent and children port inter-connection |
";
}else{
if ( fnmatch("Switch output port and parent port connection", $line)) {
$buffer .= "Switch output port and parent port connection |
";
}else{
$buffer2 = str_replace('[ ]+', ' ' , trim($line));
$buffer2 = str_replace('[<--+]', ' ' , trim($buffer2));
$Tsplit = split('[ ]+', $buffer2);
$buffer .= "".trim($Tsplit[0]) ." | ". trim($Tsplit[1]) . " | ". trim($Tsplit[3]) . " | ". trim($Tsplit[4])." |
";
}
}
}
return $buffer ;
}else{
if ( $idRight == "genmap") {
$command=exec($pathKlask." exportsw -f dot > ./klask/map.dot" , $ret) ;
$command=exec("dot -Tjpg ./klask/map.dot > ./klask/map.jpg", $ret);
}
}
}
}
}
}
function KlaskSearch($ip) {
$pathKlask = "sudo /usr/sbin/klask" ;
$command=exec($pathKlask." search " ."$ip", $ret) ;
$buffer = "";
$cpt=0;
foreach ( $ret as $line ) {
$cpt++;
if ( $cpt == 2 ) {
if ( !empty($line) ) {
$buffer2 = str_replace('[ ]+', ' ' , trim($line));
$Tsplit = split('[ ]+', $buffer2);
$buffer = "Nom: " . $Tsplit[2] ."
";
$buffer .= "IP: " . $Tsplit[3] . "
";
$buffer .= "Mac: " . $Tsplit[4] . "
";
$buffer .= "Connecté au Switch: " . $Tsplit[0]. "
";
$buffer .= "Port: " . $Tsplit[1];
return $buffer ;
}else{
return "" ;
}
}
}
}
?>