[comment]: # ({3a2a8d58-3a2a8d58})
# script.execute

[comment]: # ({/3a2a8d58-3a2a8d58})

[comment]: # ({60a9dbe3-60a9dbe3})
### 描述

`object script.execute(object parameters)`

此方法允许在一个主机或事件上run脚本.

::: noteclassic
此方法对所有类型的用户可用. 调用该方法的权限可以在用户角色设置中撤销. 更多信息请参阅[User
roles](/manual/web_interface/frontend_sections/administration/user_roles).

:::

[comment]: # ({/60a9dbe3-60a9dbe3})

[comment]: # ({962e590e-e4fa8ac7})
### 参数

`(object)` 参数包含要run的脚本ID以及主机的ID或事件的ID.

| 参数 | [数据类型](/manual/api/reference_commentary#数据类型) | 描述 |
|--|--|------|
| **scriptid**<br>(required) | string | 要run的脚本ID. |
| hostid | string | 要在其上run脚本的主机ID. |
| eventid | string | 要在其上run脚本的事件ID. |

[comment]: # ({/962e590e-e4fa8ac7})

[comment]: # ({47d23fd5-dde54e60})
### 返回值

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

| 属性 | [数据类型](/manual/api/reference_commentary#数据类型) | 描述 |
|--|--|------|
| response | string | 脚本是否run成功执行。<br><br>可能值 - `success`。 |
| value | string | 脚本输出内容。 |
| debug | object | 当执行webhook脚本时包含[调试](/manual/api/reference/script/object#调试)。对于其他脚本类型，包含空的object。 |

[comment]: # ({/47d23fd5-dde54e60})

[comment]: # ({b41637d2-b41637d2})
### 示例

[comment]: # ({/b41637d2-b41637d2})

[comment]: # ({719944b8-03cb1cf3})
#### 运行 webhook 脚本

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

请求:

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

```json
{
    "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
}
```

[comment]: # ({/719944b8-03cb1cf3})

[comment]: # ({36041e06-baebff97})
#### 运行自定义脚本

在一个主机上运行"ping"脚本.

请求:

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

```json
{
    "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
}
```

[comment]: # ({/36041e06-baebff97})

[comment]: # ({e7dc45a5-e7dc45a5})
### 来源

CScript::execute() 位于 *ui/include/classes/api/services/CScript.php* 文件中。

[comment]: # ({/e7dc45a5-e7dc45a5})
