简体   繁体   中英

How to list MAC Address of an AWS EC2 instance with Python and boto3

如何使用Python boto3库查找有关Amazon EC2实例的MAC地址的信息。

If you are on the instance itself, the MAC address can be retrieve via the instance metadata service :

$ curl http://169.254.169.254/latest/meta-data/mac/ 

02:72:f3:75:2f:83

Assuming you have one network interface attached to your instances. If you have more than one network interface attached to your instances, tweak the code to your needs.

import boto3

ec2 = boto3.resource('ec2')
insts = list(ec2.instances.all())
for inst in insts:
  for iface in inst.network_interfaces:
    print inst.instance_id, iface.mac_address
    ips = []
    macs = []
    print self.nodes
    vpc = self.resource.Vpc(self.config['vpc_id'])

    for node in self.nodes:

        node_name = node.name
        if re.search(re.escape(instance_name) + r"-*[0-9]*", node_name) and node.state == 'running':
            ips.append(node.private_ips)

            for n in node.network_interfaces:
                if n.private_ip_address == node.private_ip_address:
                    macs.append(n.mac_address)
                    break
    return ips, macs

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM