jumpserver api 批量添加主机
#!/usr/bin/env python3 # -*- coding:utf-8 -*- import requests, json import datetime from httpsig.requests_auth import HTTPSignatureAuth import sys def add_host(hostname, ip): url = 'http://192.168.1.1' + '/api/v1/assets/assets/' #web页面可以查到各种ID信息 数据库不会用 data = { 'hostname': hostname, #主机名 'ip': ip, #IP地址 'platform': 'Linux', #操作系统 'protocols': 'ssh/22', 'nodes': ['8d0a4546-a7b6-4b38-8af9-7b08d58f8cce'], #节点id 'is_active': True } response = requests.post(url, auth=auth, headers=headers, data=data) #state = json.loads(response.json()) #print(state) print(response.text) #创建成功后返回主机id return json.loads(response.text)['id'] #需要先创建好,创建方式参考下面的截图 auth = HTTPSignatureAuth(key_id='XXXXXXXX', secret='XXXXXXXX', 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(sys.argv[1], sys.argv[2])
# web页面