python脚本通过登录ambari页面重启各服务。服务名称可传入
时间: 2023-07-31 22:03:49 浏览: 59
可以通过 Python 的 requests 模块来模拟登录 Ambari 页面,并通过 API 来重启指定的服务。以下是一个简单的示例:
```python
import requests
import json
# Ambari 登录信息
ambari_url = 'http://your.ambari.server:8080'
ambari_username = 'your_username'
ambari_password = 'your_password'
# 登录 Ambari
s = requests.Session()
login_url = ambari_url + '/api/v1/session'
data = {'username': ambari_username, 'password': ambari_password}
response = s.post(login_url, data=data)
# 重启服务
service_name = 'your_service_name'
restart_url = ambari_url + '/api/v1/clusters/your_cluster_name/services/' + service_name
data = {'RequestInfo': {'context': 'Restart ' + service_name + ' Service'}, 'Body': {'ServiceInfo': {'state': 'INSTALLED'}}}
response = s.put(restart_url, data=json.dumps(data))
```
在上述示例中,我们先通过 Session 对象模拟登录 Ambari 页面,然后通过 API 来重启指定的服务。其中,ambari_url 是 Ambari 页面的地址,ambari_username 和 ambari_password 分别是登录 Ambari 页面的用户名和密码。service_name 是需要重启的服务名称。在重启服务时,我们通过 put 方法发送请求,其中 data 是请求的参数,包含了需要重启的服务的信息。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)