script.execute

描述

object script.execute(object parameters)

此方法允许在一个主机或事件上run脚本。URL类型脚本除外,这些脚本不可执行。

此方法适用于任何类型的用户。调用该方法的权限可以在用户角色设置中撤销。更多信息请参阅User roles

参数

(object) 包含要run的脚本ID的参数,可以是主机的ID或事件ID及手动输入值。

参数 数据类型 描述
scriptid ID 要run的脚本的ID。

参数行为:
- 必填
hostid ID 要在其上run脚本的主机的ID。

参数行为:
- 如果未设置eventid则为必填
eventid ID 要在其上run脚本的事件的ID。

参数行为:
- 如果未设置hostid则为必填
manualinput string 用户提供的用于run脚本的值,将替换
{MANUALINPUT}宏。

返回值

(object) 返回脚本执行结果。

属性 数据类型 描述
response string 脚本是否run成功。

可能值 - success
value string 脚本输出。
debug object 当执行webhook脚本时包含调试object。对于其他脚本类型,包含空的object。

示例

运行 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"  },  "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 -c {MANUALINPUT} {HOST.CONN}; case $? in [01]) true;; *) false;; esac"在一个主机上运行"ping"脚本.

执行请求:

{  "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 文件中。