示例**:
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)
if b:
total\_page = total\_page + 1
return total\_page
def queryinstances(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
def get\_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 使用
示例 usr bin env python coding utf 8 author shuke date 2017 9 26 import json,yaml try from aliyunsdkcore.client import acsclient from aliyunsdkcore impo...
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位址...