右上角先创建API Key 脚本需要用到
#!/usr/bin/env python3
# -*- coding:utf-8 -*-
import requests
import datetime
from httpsig.requests_auth import HTTPSignatureAuth
import json
def add_host(hostname, ip):
url = 'https://jempserver地址' + '/api/v1/assets/assets/'
#web页面可以查到各种ID信息 数据库不会用
data = {
'hostname': hostname, #主机名
'ip': ip, #IP地址
'platform': 'Linux', #操作系统
'protocols': 'ssh/22',
'admin_user': 'ca412700-e779-431d-8078-b2b86d5d8395', #管理用户ID
'nodes': ['d2f10111-8be6-4fc7-9f2d-04e7175ccace'], #节点ID
'is_active': True
}
response = requests.post(url, auth=auth, headers=headers, data=data)
print(response.text)
#创建成功后返回主机id
return json.loads(response.text)['id']
#删除主机需要获取主机ID 先传入主机IP获取主机ID再删除
def del_host(ip):
url = 'jempserver地址' +
'/api/v1/assets/assets/' + f'?ip={ip}'
response = requests.get(url, auth=auth, headers=headers)
try:
id = json.loads(response.text)[0]['id']
url = 'https://jempserver地址' +
'/api/v1/assets/assets/' + f'{id}/'
response = requests.delete(url, auth=auth, headers=headers)
print(
f"jumpserver del host successful IP:{ip} ID:{id} {response.text}")
except IndexError:
print(f"{response.text}")
#需要先创建好
auth = HTTPSignatureAuth(key_id='', secret='',
algorithm='hmac-sha256', headers=['(request-target)', 'accept', 'date'])
gmt_form = '%a, %d %b %Y %H:%M:%S GMT'
headers = {
'Accept': 'application/json',
'X-JMS-ORG': '00000000-0000-0000-0000-000000000002',
'Date': datetime.datetime.utcnow().strftime(gmt_form)
}
if __name__ == '__main__':
#add_host('server1', '192.168.1.1')
#del_host('192.168.1.1')
pass
公有云创建好虚拟机懒得手动加堡垒机了 ,授权直接授权给所有人,随便用了



