如何使用纯python / boto访问EMR主私有IP地址

问题描述 投票:1回答:2

我在这个网站和谷歌上搜索过但未能得到答案。

我从EC2实例运行代码,该实例使用boto创建和管理EMR集群。我可以使用这个框架来获取flow_id(或cluster_id,不确定哪个是正确的名称),它以“j-”开头并具有固定数量的字符来标识集群。

使用框架我可以建立一个emr或ec2连接,但对于我的生活,我不能使用boto执行以下操作:

    aws emr --list-clusters --cluster-id=j-ASDFGHJKL | json '["instances"].[0].["privateipaddress"]

**以上是有点捏造,我不记得json格式和json命令是什么或它想要什么args,但cli仍然。

我用pprint.pprint()编辑并检查了inspect.getmembers()连接,得到了conn到特定的cluster_id,但我还没有看到这个字段/ var /属性有或没有方法调用。

我一直在亚马逊和博托上下,他们怎么做像here

在里面

    def test_list_instances(self): #line 317
        ...
        self.assertEqual(response.instances[0].privateipaddress , '10.0.0.60')
        ...

附:我已经尝试过this但是python抱怨“实例”属性不可迭代,数组可访问(我忘记了“var [0]”命名),还有我试过的其他东西,包括检查。顺便说一句,我可以从这里访问publicDNSaddress,还有很多其他的东西,只是不是privateIP ...

请告诉我,如果我搞砸了某个地方,我可以在哪里找到答案,我正在使用子流程进行丑陋的修复!

python amazon-web-services amazon-ec2 boto emr
2个回答
1
投票

如果您要求使用emr的主ip,则以下命令将起作用:

list_intance_resp =  boto3.client('emr',region_name='us-east-1').list_instances(ClusterId ='j-XXXXXXX')
print list_intance_resp['Instances'][len(list_intance_resp['Instances'])-1]['PrivateIpAddress']

0
投票

使用pip show boto检查你的boto版本我的猜测是你使用2.24或更早版本,因为这个版本没有解析实例信息,请参阅https://github.com/boto/boto/blob/2.24.0/tests/unit/emr/test_connection.py#L117https://github.com/boto/boto/blob/2.25.0/tests/unit/emr/test_connection.py#L313相比较

如果您将boto版本升级到2.25或更高版本,您将能够执行以下操作

from boto.emr.connection import EmrConnection

conn = EmrConnection(<your aws key>, <your aws secret key>)
jobid = 'j-XXXXXXXXXXXXXX' # your job id
response = conn.list_instances(jobid)

for instance in response.instances:
    print instance.privateipaddress 
© www.soinside.com 2019 - 2024. All rights reserved.