栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

jumpserver api添加删除主机

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

jumpserver api添加删除主机

右上角先创建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

公有云创建好虚拟机懒得手动加堡垒机了 ,授权直接授权给所有人,随便用了

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/641778.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号