Feature #4002

webservices plugin support

Added by CDuv almost 7 years ago. Updated about 5 years ago.

Status:ClosedStart date:11/13/2012
Priority:NormalDue date:
Assignee:tsmr% Done:

100%

Category:General
Target version:1.6.1

Description

It would be nice to be able to run the shellcommands' commands remotely: One could then use a remote script/software to ask GLPI to execute configured commands on it's inventory, exactly as if he were in front of GLPI web interface.
GLPI already as a plugin to deal with remote interaction: WebServices plugin that creates an XMLRPC, a REST and a SOAP server. I'm proposing to add WebServices support into shellcommands.

Feature list

  • Expose the list of commands and their associated asset types
  • Run any shellcommand on any asset element (computer, printer, networkequipment, etc.).
  • Command to run should be specified via either ID or name
  • Asset element should be specified via either ID or name

shellcommands_-_webservices_plugin_support___4002__patch.7z - CDuv implementation proposal (4.38 KB) CDuv, 11/13/2012 06:17 PM

shellcommands_-_webservices_plugin_support___4002__patch2-based_on_r137.diff Magnifier - Newer patch for this feature, based on r137 to add GLPI 0.84.5 support (7.03 KB) CDuv, 05/24/2014 03:39 AM

Associated revisions

Revision 136
Added by tsmr over 5 years ago

Add patch from CDUV (thanks) webservices plugin support. see #4002

Revision 137
Added by tsmr over 5 years ago

Add patch from CDUV (thanks) webservices plugin support. fix #4002

Revision 139
Added by tsmr about 5 years ago

Add patch from CDUV (thanks) webservices plugin support. see #4002

Revision 140
Added by tsmr about 5 years ago

[0.85] Add patch from CDUV (thanks) webservices plugin support. see #4002

History

#1 Updated by CDuv almost 7 years ago

Here is my implementation proposal.

Description

  • Plugin setup's method plugin_init_shellcommands() adds a plugin hook for plugin webservices named plugin_shellcommands_registerMethods.
    This hook is implemented by plugin_shellcommands_registerMethods() method which declares the two following webservices methods (methods that can be called remotely):
    • shellcommands.list: Implements the feature of listing every shellcommands available (done by PluginShellcommandsWebservice::methodList())
    • shellcommands.run: Implements the feature of running a given shellcommand on a given target from the inventory (done by PluginShellcommandsWebservice::methodRun())

Code evolution done for implemenation

  • For easinessI've created a class constant PluginShellcommandsShellcommand_Item::WOL_COMMAND_NAME containing the name of the special built in Wake on LAN shellcommand.
  • I've moved the command execution algorithm from PluginShellcommandsShellcommand_Item::lauchCommand() to the new methods:
    • PluginShellcommandsShellcommand_Item::getCommandLine(): Creates the command line
    • PluginShellcommandsShellcommand_Item::execCommand(): Actually executes the command
  • I've cloned the process of resolving the value of link paramater (eg. [NAME], [IP], etc.) from PluginShellcommandsShellcommand_Item::showPluginFromItems() into a new method that only deals with it, without printing any text or HTML code: PluginShellcommandsShellcommand_Item::resolveLinkOfCommand()

Methods documentation

Method shellcommands.list

This method is only accessible by authenticated users

Lists available shellcommands.

Method input

This method accepts an hastable of options:
Option name Option description
help Option to get usage information (Optional)

Method return

This method returns an hashtable of hashtables. Each sub-hashtable corresponds to a shellcommand.

Structure of an shellcommand hashtable:
Key name Value description
id The ID of the shellcommand
name The name of the shellcommand
targets An hashtable of target types this shellcommand can be run onto (eg. Computer, Printer)

Call example

$ php testxmlrpc.php --host=glpi.server.tld --url=plugins/webservices/xmlrpc.php --session=abcdef123456 --method=shellcommands.list
+ Calling 'shellcommands.list' on http://glpi.server.tld/plugins/webservices/xmlrpc.php?session=abcdef123456
+ Response: Array
(
    [0] => Array
        (
            [id] => 5
            [name] => etherwake
            [targets] => Array
                (
                    [0] => Computer
                )

        )

    [1] => Array
        (
            [id] => 6
            [name] => Nslookup IP
            [targets] => Array
                (
                    [0] => Computer
                    [1] => NetworkEquipment
                    [2] => Printer
                )

         )

    [2] => Array
        (
            [id] => 4
            [name] => Nslookup NAME
            [targets] => Array
                (
                    [0] => Computer
                    [1] => NetworkEquipment
                    [2] => Printer
                )

        )

    [3] => Array
        (
            [id] => 1
            [name] => Ping
            [targets] => Array
                (
                    [0] => Computer
                    [1] => NetworkEquipment
                    [2] => Printer
                )

         )

    [4] => Array
        (
            [id] => 2
            [name] => Tracert
            [targets] => Array
                (
                    [0] => Computer
                    [1] => NetworkEquipment
                    [2] => Printer
                )

        )

    [5] => Array
        (
            [id] => 3
            [name] => Wake on Lan
            [targets] => Array
                (
                    [0] => Computer
                )

         )

)

Method shellcommands.run

This method is only accessible by authenticated users

Run a given available shellcommand on a specified target

Method input

This method accepts an hastable of options:
Option name Option description
command_id One or more ID of shellcommand(s) to run (incompatible with command_name)
command_name One or more name of shellcommand(s) to run (incompatible with command_id)
target_type Type of the target to run the shellcommand(s) on
target_id One or more ID of inventory element(s) on which run the shellcommand(s) (incompatible with target_name)
target_name One or more name of inventory element(s) on which run the shellcommand(s) (incompatible with target_id)
help Option to get usage information (Optional)

Method return

This method returns an hashtable given results on the shellcommand execution.

Structure of an shellcommand hashtable:
Key name Value description
callResults Raw output of the shellcommand(s)
invalidCommands ID or name(s) of unknown specified shellcommand(s) (therefore not run)
invalidTargets ID or name(s) of unknown specified inventory element(s) (therefore not impacted)

Call examples

Trying to run shellcommand named Nslookup NAME on Computer named myfileserver:

$ php testxmlrpc.php --host=glpi.server.tld --url=plugins/webservices/xmlrpc.php --session=abcdef123456 --method=shellcommands.run --command_name="Nslookup NAME" target_type=Computer target_name=myfileserver
+ Calling 'shellcommands.run' on http://glpi.server.tld/plugins/webservices/xmlrpc.php?session=abcdef123456 
+ Response: Array
(
    [callResults] => Server:            192.168.0.1
Address:        192.168.0.1#53

Non-authoritative answer:
Name:   myfileserver.mycompany.tld
Address: 192.168.0.42
    [invalidCommands] => Array
        (
        )

    [invalidTargets] => Array
        (
        )

)

Trying to run invalid shellcommand of ID -1:

$ php testxmlrpc.php --host=glpi.server.tld --url=plugins/webservices/xmlrpc.php --session=abcdef123456 --method=shellcommands.run --command_name=-1 target_type=Computer target_name=myfileserver
+ Calling 'shellcommands.run' on http://glpi.server.tld/plugins/webservices/xmlrpc.php?session=abcdef123456 
+ Response: Array
(
    [callResults] =>
    [invalidCommands] => Array
        (
            [0] => -1
        )

    [invalidTargets] => Array
        (
        )

)

#2 Updated by CDuv over 5 years ago

  • Status changed from New to Feedback

One year later, proposed code didn't make it to release. Is there a problem with that feature or the implementation?

#3 Updated by tsmr over 5 years ago

  • Status changed from Feedback to Resolved
  • % Done changed from 0 to 100

Applied in changeset r137.

#4 Updated by tsmr over 5 years ago

  • Category set to General
  • Assignee set to tsmr
  • Target version set to 1.6.1

#5 Updated by CDuv over 5 years ago

As discussed with tsmr on IRC, I've looked on r137 for bugs (original patch #4002#note-1 was quite aged and based on now old GLPI-PROJECT version).

Attached are the following fixes (all related to GLPI changes):
  • mysql_real_escape_string() replaced by $DB->escape()
  • Now looks for IP addresses in glpi_ipaddresses table (was using glpi_networkports)
  • No longer reads internal IP and MAC addresses of network equipment (because there is no more such things with recent GLPI releases)

I've also refactored the IP and MAC tag => address process: now using a loop on theses tags, avoids code duplication.

Now works with GLPI version 0.84.5 and WebServices version 1.4.2.

#6 Updated by CDuv about 5 years ago

Any chance this gets merged?

#7 Updated by tsmr about 5 years ago

  • Status changed from Feedback to Closed

Also available in: Atom PDF