jumpserver api 批量删除主机
#!/usr/bin/env python3
# -*- coding:utf-8 -*-
import requests import sys import json import time jumpurl = 'http://192.168.1.1' username = 'admin' password = "passwd" print() if len(sys.argv) < 2: print('请输入ip') exit(2) def getBearerToken(): url = jumpurl + "/api/v1/authentication/auth/" header = { "accept": "application/json", "Content-Type": "application/json" } data = { "username": username, "password": password } res = requests.post(url, headers=header, data=json.dumps(data)) if res.status_code == 201: return res.json()['token'] else: print(res.status_code) print('用户名密码有误') exit(3) ip = sys.argv[1] token = getBearerToken() fullurl = jumpurl + '/api/v1/assets/assets/?ip=' + ip header = { "accept": "application/json", "Authorization": "Bearer " + token } res = requests.delete(headers=header,url=fullurl) if res.status_code == 204: print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()),ip,'删除成功') else: print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()),ip,'删除失败')