# 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)
华为云新实例自动添加到jms
最新推荐文章于 2024-04-13 13:18:12 发布