华为云新实例自动添加到jms

# coding: utf-8
from huaweicloudsdkcore.auth.credentials import BasicCredentials
from huaweicloudsdkcore.exceptions import exceptions
from huaweicloudsdkcore.http.http_config import HttpConfig
from huaweicloudsdkecs.v2 import *
from huaweicloudsdkecs.v2.region.ecs_region import EcsRegion

import requests
import json
from pprint import pprint
import subprocess
def get_token():
    url = 'http://ip/api/v1/authentication/auth/'
    query_args = {
        "username": "admin",
        "password": "jumpserver"
    }
    response = requests.post(url, data=query_args)
    return json.loads(response.text)['token']
def get_user_info():
    url = 'http://ip/api/v1/users/users/'
    token = get_token()
    header_info = { "Authorization": 'Bearer ' + token }
    response = requests.get(url, headers=header_info)
    print(json.loads(response.text))
#get_user_info()
def get_assets():
    url = 'http://ip/api/v1/assets/assets/'
    token = get_token()
    header_info = { "Authorization": 'Bearer ' + token }
    response = requests.get(url, headers=header_info)
    print(json.loads(response.text))
#get_assets()
def post_assets(ipaddr, hostname):
    url = 'http://ip/api/v1/assets/assets/'
    print(ipaddr,hostname)
#    data = {"hostname": "scripttest","ip":"1.1.1.1": "string","protocols": ["ssh/22"],"platform": "Linux","nodes": ['fa3ee95f-b0b0-423a-b198-43b98331dbaa'],"nodes_display": ['/Default']}
    data = {
  "ip": ipaddr,
  "hostname": hostname,
  "protocol": "ssh",
  "port": 0,
  "is_active": "true",
  "admin_user": "8873efe7-61b0-4617-865b-e77e4c1b7fd5",
  "platform": "Linux",
  "nodes": [
    "46ec2e92-aa53-411e-986a-7492c6a6b115"
  ],
  "nodes_display": [
    "/Default/Linux"
  ],
}
    token = get_token()
    header_info = { "Authorization": 'Bearer ' + token }
    response = requests.post(url, data, headers=header_info)
    print(json.loads(response.text))
#get_assets()
if __name__ == "__main__":
    ak = "ak"
    sk = "sk"

    credentials = BasicCredentials(ak, sk) \

    client = EcsClient.new_builder() \
        .with_credentials(credentials) \
        .with_region(EcsRegion.value_of("cn-east-2")) \
        .build()

    try:
        getIp = subprocess.Popen("hostname -I | awk -F' ' '{print $1}'", shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, universal_newlines=True)
        ipaddr = getIp.stdout.read().strip()
        request = ListServersDetailsRequest()
        request.ip = "172.16.33.156"
        response = client.list_servers_details(request)
        result = eval(repr(response))
        hostname = result["servers"][0]["name"]
    except exceptions.ClientRequestException as e:
        print(e.status_code)
        print(e.request_id)
        print(e.error_code)
        print(e.error_msg)
    post_assets(ipaddr,hostname)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值