执行

描述

object script.execute(object parameters)

此方法允许在某主机或事件上运行脚本。

::: 请注意 此方法允许任何用户使用。可以在用户角色设置中撤销调用此方法的权限。更多信息见 User roles。 :::

参数

(对象) 参数包含了运行脚本的ID,以及主机ID或者事件ID。 参数 类型 描述
scriptid
(必需)
string 运行脚本的ID。
hostid string 在其上运行脚本的主机ID。
eventid string 在其上运行脚本的事件ID。
manualinput string 用于运行脚本的用户提供的值,替换{MANUALPINPUT}宏。

返回值

(object) 返回脚本执行的结果。 属性 类型 描述
response string 脚本是否成功运行。

可能的值 - successfailed
字符串 脚本输出。
debug object 如果执行 webhook 脚本,则包含 调试对象。对于其他脚本类型,它包含空对象。

示例

运行 webhook 脚本

运行一个向外部服务发送HTTP请求的webhook脚本。 请求:

{
    "jsonrpc": "2.0",
    "method": "script.execute",
    "params": {
        "scriptid": "4",
        "hostid": "30079"
    },
    "id": 1
}

响应

{
    "jsonrpc": "2.0",
    "result": {
        "response": "success",
        "value": "{\"status\":\"sent\",\"timestamp\":\"1611235391\"}",
        "debug": {
            "logs": [
                 {
                     "level": 3,
                     "ms": 480,
                     "message": "[Webhook Script] HTTP status: 200."
                 }
             ],
             "ms": 495
        }
    },
    "id": 1
}

运行自定义脚本

在主机上运行“ping”脚本。

请求:

{
    "jsonrpc": "2.0",
    "method": "script.execute",
    "params": {
        "scriptid": "1",
        "hostid": "30079"
    },
    "auth": "038e1d7b1735c6a5436ee9eae095879e",
    "id": 1
}

响应:

{
    "jsonrpc": "2.0",
    "result": {
        "response": "success",
        "value": "PING 127.0.0.1 (127.0.0.1) 56(84) bytes of data.\n64 bytes from 127.0.0.1: icmp_req=1 ttl=64 time=0.074 ms\n64 bytes from 127.0.0.1: icmp_req=2 ttl=64 time=0.030 ms\n64 bytes from 127.0.0.1: icmp_req=3 ttl=64 time=0.030 ms\n\n--- 127.0.0.1 ping statistics ---\n3 packets transmitted, 3 received, 0% packet loss, time 1998ms\nrtt min/avg/max/mdev = 0.030/0.044/0.074/0.022 ms\n",
        "debug": []
    },
    "id": 1
}

运行自定义脚本

在主机上运行“ping”脚本,在主机上使用命令“ping -c {MANUALINPUT} {HOST.CONN}; case $? in [01]) true;; *) false;; esac”。 请求:

{
    "jsonrpc": "2.0",
    "method": "script.execute",
    "params": {
        "scriptid": "7",
        "hostid": "30079",
        "manualinput": "2"
    },
    "id": 1
}

响应

{
    "jsonrpc": "2.0",
    "result": {
        "response": "success",
        "value": "PING 127.0.0.1 (127.0.0.1) 56(84) bytes of data.\n64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.051 ms\n64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.025 ms\n\n--- 127.0.0.1 ping statistics ---\n2 packets transmitted, 2 received, 0% packet loss, time 1021ms\nrtt min/avg/max/mdev = 0.025/0.038/0.051/0.013 ms",
        "debug": []
    },
    "id": 1
}

源代码

CScript::execute() 在ui/include/classes/api/services/CScript.php