[comment]: # ({cd91b3fe-cd91b3fe})
# script.get

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

[comment]: # ({96ec37bc-96ec37bc})
### 描述

`integer/array script.get(object parameters)`

该方法允许根据给定参数检索脚本。

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

:::

[comment]: # ({/96ec37bc-96ec37bc})

[comment]: # ({57cc81e7-c425a65a})
### 参数

`(object)` 定义所需输出的参数。

该方法支持以下参数。

| 参数 | [数据类型](/manual/api/reference_commentary#数据类型) | 描述 |
|--|--|------|
| groupids | string/array | 仅返回可在给定主机组上run的脚本。 |
| hostids | string/array | 仅返回可在给定主机上run的脚本。 |
| scriptids | string/array | 仅返回具有指定ID的脚本。 |
| usrgrpids | string/array | 仅返回可由给定用户组中的用户run的脚本。 |
| selectGroups | query | 返回包含脚本可run的主机组的[groups](/manual/api/reference/hostgroup/object)属性。 |
| selectHosts | query | 返回包含脚本可run的主机的[hosts](/manual/api/reference/host/object)属性。 |
| selectActions | query | 返回包含脚本关联动作的[actions](/manual/api/reference/action/object)属性。 |
| sortfield | string/array | 按指定属性排序结果。<br><br>可能值为：`scriptid`和`name`。 |
| countOutput | boolean | 这些参数在[通用get方法参数](/manual/api/reference_commentary#通用get方法参数)中有描述。 |
| editable | boolean | ^           |
| excludeSearch | boolean | ^           |
| filter | object | ^           |
| limit | integer | ^           |
| output | query | ^           |
| preservekeys | boolean | ^           |
| search | object | ^           |
| searchByAny | boolean | ^           |
| searchWildcardsEnabled | boolean | ^           |
| sortorder | string/array | ^           |
| startSearch | boolean | ^           |

[comment]: # ({/57cc81e7-c425a65a})

[comment]: # ({7223bab1-7223bab1})
### 返回值

`(integer/array)` 返回以下两种情况之一:

-   一个由objects组成的array;
-   检索到的objects数量，如果使用了`countOutput`参数。

[comment]: # ({/7223bab1-7223bab1})

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

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

[comment]: # ({59acc8d9-6d428f32})
#### 检索所有脚本

获取所有已配置的脚本。

请求：

```json
{
    "jsonrpc": "2.0",
    "method": "script.get",
    "params": {
        "output": "extend"
    },
    "auth": "038e1d7b1735c6a5436ee9eae095879e",
    "id": 1
}
```
响应：

```json
{
    "jsonrpc": "2.0",
    "result": [    
        {
            "scriptid": "1",
            "name": "Ping",
            "command": "/bin/ping -c 3 {HOST.CONN} 2>&1",
            "host_access": "2",
            "usrgrpid": "0",
            "groupid": "0",
            "description": "",
            "confirmation": "",
            "type": "0",
            "execute_on": "1",
            "timeout": "30s",
            "parameters": []
        },
        {
            "scriptid": "2",
            "name": "Traceroute",
            "command": "/usr/bin/traceroute {HOST.CONN} 2>&1",
            "host_access": "2",
            "usrgrpid": "0",
            "groupid": "0",
            "description": "",
            "confirmation": "",
            "type": "0",
            "execute_on": "1",
            "timeout": "30s",
            "parameters": []
        },
        {
            "scriptid": "3",
            "name": "Detect operating system",
            "command": "sudo /usr/bin/nmap -O {HOST.CONN} 2>&1",
            "host_access": "2",
            "usrgrpid": "7",
            "groupid": "0",
            "description": "",
            "confirmation": "",
            "type": "0",
            "execute_on": "1",
            "timeout": "30s",
            "parameters": []
        },
        {
            "scriptid": "4",
            "name": "Webhook",
            "command": "try {\n var request = new HttpRequest(),\n response,\n data;\n\n request.addHeader('Content-Type: application/json');\n\n response = request.post('https://localhost/post', value);\n\n try {\n response = JSON.parse(response);\n }\n catch (error) {\n response = null;\n }\n\n if (request.getStatus() !== 200 || !('data' in response)) {\n throw 'Unexpected response.';\n }\n\n data = JSON.stringify(response.data);\n\n Zabbix.Log(3, '[Webhook Script] response data: ' + data);\n\n return data;\n}\ncatch (error) {\n Zabbix.Log(3, '[Webhook Script] script execution failed: ' + error);\n throw 'Execution failed: ' + error + '.';\n}",
            "host_access": "2",
            "usrgrpid": "7",
            "groupid": "0",
            "description": "",
            "confirmation": "",
            "type": "5",
            "execute_on": "1",
            "timeout": "30s",
            "parameters": [
                {
                    "name": "token",
                    "value": "{$WEBHOOK.TOKEN}"
                },
                {
                    "name": "host",
                    "value": "{HOST.HOST}"
                },
                {
                    "name": "v",
                    "value": "2.2"
                }
            ]
        }
    ],
    "id": 1
}
```

[comment]: # ({/59acc8d9-6d428f32})

[comment]: # ({b0b740ec-b0b740ec})
### 另请参阅

-   [主机-object](/manual/api/reference/host/object#主机-object)
-   [主机组-object](/manual/api/reference/hostgroup/object#主机组-object)

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

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

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

[comment]: # ({/90dfc753-90dfc753})
