0

Im trying to make a plugin in OCS Inventory to get all the open ports from a host. Im having big troubles in the agent file. I really dont understand all the files in my plugin but I had compared to other plugins and it seens okay.

I can install the plugin correctly in the server but when I want to send information from the agent to the server, the tables are empty.

Im going to put all my code here hooping that someone can help me :)

Map.pm


package Apache::Ocsinventory::Plugins::Openports::Map;

use strict;

use Apache::Ocsinventory::Map;
$DATA_MAP{openports} = {
    mask => 0,
    multi => 1,
    auto => 1,
    delOnReplace => 1,
    sortBy => 'PORTNUMBER',
    writeDiff => 0,
    cache => 0,
    fields => {
        ADDRESS => {},
        PORTNUMBER => {}<
    }
};

1;

cd_openports.php

if (AJAX) {
    parse_str($protectedPost['ocs']['0'], $params);
    $protectedPost += $params;
    ob_start();
    $ajax = true;
} else {
    $ajax = false;
}

print_item_header("Open Ports");



if (!isset($protectedPost['SHOW'])) {
    $protectedPost['SHOW'] = 'NOSHOW';
}

$form_name = "openports";

$table_name = $form_name;
$tab_options = $protectedPost;

$tab_options['form_name'] = $form_name;
$tab_options['table_name'] = $table_name;

echo open_form($form_name);

$list_fields = array(
    'Address'              => 'ADDRESS',
    'Port Number'          => 'PORTNUMBER'
);

$list_col_cant_del = $list_fields;
$default_fields = $list_fields;

$sql = prepare_sql_tab($list_fields);
$sql['SQL']  .= "FROM $table_name WHERE (hardware_id=$systemid)";

array_push($sql['ARG'], $systemid);

$tab_options['ARG_SQL']=$sql['ARG'];
$tab_options['ARG_SQL_COUNT']=$systemid;

ajaxtab_entete_fixe($list_fields, $default_fields, $tab_options, $list_col_cant_del);

echo close_form();

if ($ajax) {
    ob_end_clean();
    tab_req($list_fields, $default_fields, $list_col_cant_del, $sql['SQL'], $tab_options);
    ob_start();
}
?>

hook.xml

<?xml version="1.0" encoding="UTF-8"?>
<hookstore>
    <hook type="lang">
        <value>en_GB</value>
    </hook>
    <hook type="cdentry">
        <identifier>cd_openports</identifier>
        <translation>57420</translation>
        <category>admin</category>
        <available>openports</available>
    </hook>
</hookstore>

infos.json

{
"displayName" : "Puertos",
"author" : ["Borges"],
"contributor" : [],
"supportedAgent" : ["Windows"],
"version" : "1",
"licence" : "GPLv2",
"description" : 
    {
        "fr" : "Un exemple de plugin",
        "en" : "A plugin example 2"
    }
}

install.php

<?php
/**
 * The following functions are used by the extension engine to generate a new table
 * for the plugin / destroy it on removal.
 */

/**
 * This function is called on installation and is used to
 * create database schema for the plugin
 */
function extension_install_openports(){
    $commonObject = new ExtensionCommon;

    $commonObject -> sqlQuery("DROP TABLE `openports`;");

    $commonObject -> sqlQuery(
        "CREATE TABLE `openports` (
        `ID` INT(11) NOT NULL AUTO_INCREMENT,
        `HARDWARE_ID` INT(11) NOT NULL,
        `ADDRESS` VARCHAR(255) DEFAULT NULL,
        `PORTNUMBER` INT(11) DEFAULT NULL,
        `STATE` VARCHAR(255) DEFAULT NULL,
        PRIMARY KEY (`ID`,`HARDWARE_ID`)) ENGINE=INNODB;"
    );
}

/**
 * This function is called on removal and is used to
 * destroy database schema for the plugin
 */
function extension_delete_openports()
{
    $commonObject = new ExtensionCommon;
    $commonObject -> sqlQuery("DROP TABLE IF EXISTS `openports`;");
}

/**
 * This function is called on plugin upgrade
 */
function extension_upgrade_openports()
{

}
?>

I really think that the problem is on my agent file, I made it on PowerShell. I changed the code like 20 times and I just see:

The code of my file of the agent (ports.ps1) is:

ports.ps1

$ports = Get-NetTCPConnection -State Listen | Select-Object LocalAddress, LocalPort

$xml = "<OPENPORTS>`n"
foreach ($port in $ports) {
    $xml += "<ADDRESS>" + $port.LocalAddress + "</ADDRESS>`n"
    $xml += "<PORTNUMBER>" + $port.LocalPort + "</PORTNUMBER>`n"
}
$xml += "</OPENPORTS>`n"

[Console]::OutputEncoding = [System.Text.Encoding]::UTF8
[Console]::WriteLine($xml)
$xml)
Expoespa
  • 7
  • 4

0 Answers0