阿里雲獲取所有例項資訊 API 使用

2022-08-10 01:51:20 字數 4786 閱讀 8328

示例**:

#

! /usr/bin/env python

#-*- coding: utf-8 -*-

#__author__ = "shuke"

#date: 2017/9/26

import

json, yaml

try:

from aliyunsdkcore.client import

acsclient

from aliyunsdkcore import

client

from aliyunsdkecs.request.v20140526 import

describeinstancesrequest

except

importerror as e:

print("

error:

", e)

exit()

class

alicloudapi(object):

"""阿里雲api獲取資料

"""#

例項編號

hid = 1

#所有region的主機

all_instances_dic ={}

def__init__(self, key, secret, zone="

cn-beijing"):

self.key =key

self.secret =secret

self.zone =zone

self.client =acsclient(self.key, self.secret, self.zone)

def instances_count(self, page_size=10):

"""返回頁碼數量

:param page_size:

:return:

"""#

建立 request,並設定引數

request =describeinstancesrequest.describeinstancesrequest()

request.set_pagesize(page_size)

#發起 api 請求並列印返回

response =self.client.do_action_with_exception(request)

ret = json.loads(response, encoding='

utf-8')

#頁碼total_page, b = divmod(ret['

totalcount

'], page_size)

ifb:

total_page = total_page + 1

return

total_page

defqueryinstances(self):

"""獲取各region的例項資訊

:return: region下所有的例項資訊

"""per_region_instances =

try:

total_page =self.instances_count()

for page_num in range(1, total_page + 1):

num = "

第%d頁

" %page_num

print(num.center(100, "*"

))

#建立 request,並設定引數

request =describeinstancesrequest.describeinstancesrequest()

request.set_pagenumber(page_num)

#發起 api 請求並列印返回

response =self.client.do_action_with_exception(request)

ret = json.loads(response, encoding='

utf-8')

#print(json.dumps(ret), indent=4, ensure_ascii=false))

instances = ret['

instances

']['

instance']

for item in

instances:

ele ={}

ele[

'zoneid

'] = item['

zoneid']

ele[

'instanceid

'] = item['

instanceid']

ele[

'instancetype

'] = item['

instancetype']

ele[

'hostname

'] = item['

hostname']

ele[

'instancename

'] = item['

instancename']

#公網ip

if item['

publicipaddress

']['

ipaddress']:

ele[

'publicipaddress

'] = item['

publicipaddress

']['

ipaddress

'][0]

else

: ele[

'publicipaddress

'] = ""

#私網ip

if item['

inneripaddress

']['

ipaddress']:

ele[

'inneripaddress

'] = item['

inneripaddress

']['

ipaddress

'][0]

else

: ele[

'inneripaddress

'] = item['

vpcattributes

']['

privateipaddress

']['

ipaddress

'][0]

print(alicloudapi.hid, ele['

zoneid

'], ele['

instanceid

'], ele['

instancetype

'], ele['

hostname'],

ele[

'instancename

'], ele['

publicipaddress

'], ele['

inneripaddress'])

alicloudapi.hid += 1

#每個例項放入列表中

#print(ele)

page_num += 1

except

exception as e:

print("

error:

", e)

return

per_region_instances

defget_all_instances(self, region_li):

"""獲得所有region例項

:param region_li: region列表

:return:

"""for zone in

region_li:

self.zone =zone

print("

\033[0;32mnow,start query %s region\033[0m

" %zone)

obj =alicloudapi(self.key, self.secret, zone)

per_region_instances =obj.queryinstances()

alicloudapi.all_instances_dic[zone] =per_region_instances

return

alicloudapi.all_instances_dic

if__name__ == '

__main__':

#載入阿里雲訪問key

key_pair = yaml.load(open('

aliyun.yml'))

key = key_pair['

key'

] secret = key_pair['

secret']

zones = ["

cn-qingdao

", "

cn-hangzhou

", "

cn-beijing"]

#for zone in zones:

#print("\033[1;32mnow,start query %s region\033[0m" % zone)

#obj = alicloudapi(key, secret, zone)

#per_region_instances = obj.queryinstances()

#all_instances_li[zone] = per_region_instances

obj =alicloudapi(key, secret)

ret = obj.get_all_instances(zones)

阿里雲獲取所有例項資訊 API 使用

示例 import json,yaml try from aliyunsdkcore.client import acsclient from aliyunsdkcore import client from aliyunsdkecs.request.v20140526 import describ...

android獲取周圍AP資訊(上)

1 增加許可權 1 2 uses permission 3 uses permission 4 uses permission 2 獲取wifimanager物件 1 wifimanager wm wifimanager getsystemservice wifi service 獲取wifi服務 ...

阿里雲例項配置

阿里雲伺服器例項設定 1.購買或領取免費的阿里雲,購買時都會選擇系統和配置,當前個人可以領取1個月免費的阿里雲伺服器 2.在控制台的雲伺服器選例項,找到自己購買區 華南1 下面的例項,重置密碼,這個密碼就是用來給putty等ssh工具進行訪問伺服器 搭建環境用的。3.使用putty等工具通過ip位址...