#!/usr/bin/env python #coding=utf-8 from pprint import pprint import json from config import Config from word_generater import decodestr from aliyunsdkcore.client import AcsClient from aliyunsdkcore.acs_exception.exceptions import ClientException from aliyunsdkcore.acs_exception.exceptions import ServerException from aliyunsdkecs.request.v20140526.DescribeInstancesRequest import DescribeInstancesRequest def privateIP2InstanceID(ip,region): d = {} client = AcsClient(decodestr(Config.accessKeyId), decodestr(Config.accessSecret), region) request = DescribeInstancesRequest() request.set_accept_format('json') request.set_PrivateIpAddresses([ip]) response = client.do_action_with_exception(request) result = json.loads(str(response, encoding='utf-8')) eipinfo = result['Instances']["Instance"][0].get('EipAddress') if eipinfo: d['eipid'] = eipinfo['AllocationId'] d['instanceid'] = result['Instances']["Instance"][0]["InstanceId"] return d if __name__ == "__main__": #print(privateIP2InstanceID("172.18.135.107")) print(privateIP2InstanceID("172.16.181.135","cn-hangzhou"))