On this page
执行
描述
object script.execute(object parameters)
此方法允许在某主机或事件上运行脚本。
::: 请注意 此方法允许任何用户使用。可以在用户角色设置中撤销调用此方法的权限。更多信息见 User roles。 :::
参数
(object) 包含要运行的脚本 ID,以及主机 ID 或事件 ID 和 manualinput 值的参数。
| 参数 | 类型 | 描述 |
|---|---|---|
| scriptid | ID | 要运行的脚本的 ID。 参数行为: - 必需 |
| hostid | ID | 要在其上运行脚本的主机 ID。 参数行为: - 如果未设置 eventid,则为 必需 |
| eventid | ID | 要在其上运行脚本的事件 ID。 参数行为: - 如果未设置 hostid,则为 必需 |
| manualinput | string | 用户提供的用于运行脚本的值,替换 {MANUALINPUT} 宏。 |
返回值
(object) 返回脚本执行结果。
| 属性 | Type | 描述 |
|---|---|---|
| response | string | 脚本是否成功运行。 可能的值 - success。 |
| value | string | 脚本输出。 |
| debug | object | 如果执行的是 webhook 脚本,则包含一个 debug 对象。对于其他脚本类型,则包含空对象。 |
示例
运行 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”脚本,命令为 "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\n",
"debug": []
},
"id": 1
}
源代码
CScript::execute() 在ui/include/classes/api/services/CScript.php。