如何通过 python sdk 从 public 虚拟机的 ip 地址获取实例 id/name
How do I get instance id/name from public ip address of VM in azure via python sdk
def get_instance_id_from_pip(self, pip):
subscription_id="69ff3a41-a66a-4d31-8c7d-9a1ef44595c3"
compute_client = ComputeManagementClient(self.credentials, subscription_id)
network_client = NetworkManagementClient(self.credentials, subscription_id)
print("Get all public IP")
for public_ip in network_client.public_ip_addresses.list_all():
if public_ip.ip_address == pip:
print(public_ip)
# Get id
pip_id= public_ip.id.split('/')
print("pip id : {}".format(pip_id))
rg_from_pip = pip_id[4].lower()
print("RG : {}".format(rg_from_pip))
pip_name = pip_id[-1]
print("pip name : {}".format(pip_name))
for vm in compute_client.virtual_machines.list_all():
vm_id = vm.id.split('/')
#print("vm ref id: {}".format(vm_id))
rg_from_vm = vm_id[4].lower()
if rg_from_pip == rg_from_vm:
## this is the VM in the same rg as pip
for ni_reference in vm.network_profile.network_interfaces:
ni_reference = ni_reference.id.split('/')
ni_name = ni_reference[8]
print("ni reference: {}".format(ni_reference))
net_interface = network_client.network_interfaces.get(rg_from_pip, ni_name)
print("net interface ref {}".format(net_interface))
public_ip_reference = net_interface.ip_configurations[0].public_ip_address
if public_ip_reference:
public_ip_reference = public_ip_reference.id.split('/')
ip_group = public_ip_reference[4]
ip_name = public_ip_reference[8]
print("IP group {}, IP name {}".format(ip_group, ip_name))
if ip_name == pip_name:
print("Thank god. Finallly !!!!")
print("VM ID :-> {}".format(vm.id))
return vm.id
我有上面的代码从 Public ip 获取 VM 实例 ID,但它不起作用。真正令人惊讶的是,对于所有实例,我得到 x.public_ip_address.ip_address 作为 None 值。
我对 Azure 的 python SDK 有多个 readthedocs 参考。但是,有些链接无法正常工作。干得好 Azure :)
其中一些:
https://azure-sdk-for-python.readthedocs.io/en/v1.0.3/resourcemanagementcomputenetwork.html
https://azure-storage.readthedocs.io/en/latest/ref/azure.storage.file.fileservice.html
第二次编辑:
我得到了这个和上面代码的答案 return vm id 给定 public ip 地址。虽然,您可以看到它并不是绝对优化的答案。因此,欢迎更好的答案。谢谢!
文档已移至此处:
https://docs.microsoft.com/python/azure/
我们进行了一些重定向,但遗憾的是无法在 RTD 上进行全局重定向,因此某些页面为 404 :/
关于你的麻烦,我会尝试直接使用publicip操作组:
https://docs.microsoft.com/en-us/python/api/azure.mgmt.network.v2017_11_01.operations.publicipaddressesoperations?view=azure-python
您在网络客户端中获得了这个,因为 client.public_ip_addresses
def get_instance_id_from_pip(self, pip):
subscription_id="69ff3a41-a66a-4d31-8c7d-9a1ef44595c3"
compute_client = ComputeManagementClient(self.credentials, subscription_id)
network_client = NetworkManagementClient(self.credentials, subscription_id)
print("Get all public IP")
for public_ip in network_client.public_ip_addresses.list_all():
if public_ip.ip_address == pip:
print(public_ip)
# Get id
pip_id= public_ip.id.split('/')
print("pip id : {}".format(pip_id))
rg_from_pip = pip_id[4].lower()
print("RG : {}".format(rg_from_pip))
pip_name = pip_id[-1]
print("pip name : {}".format(pip_name))
for vm in compute_client.virtual_machines.list_all():
vm_id = vm.id.split('/')
#print("vm ref id: {}".format(vm_id))
rg_from_vm = vm_id[4].lower()
if rg_from_pip == rg_from_vm:
## this is the VM in the same rg as pip
for ni_reference in vm.network_profile.network_interfaces:
ni_reference = ni_reference.id.split('/')
ni_name = ni_reference[8]
print("ni reference: {}".format(ni_reference))
net_interface = network_client.network_interfaces.get(rg_from_pip, ni_name)
print("net interface ref {}".format(net_interface))
public_ip_reference = net_interface.ip_configurations[0].public_ip_address
if public_ip_reference:
public_ip_reference = public_ip_reference.id.split('/')
ip_group = public_ip_reference[4]
ip_name = public_ip_reference[8]
print("IP group {}, IP name {}".format(ip_group, ip_name))
if ip_name == pip_name:
print("Thank god. Finallly !!!!")
print("VM ID :-> {}".format(vm.id))
return vm.id
我有上面的代码从 Public ip 获取 VM 实例 ID,但它不起作用。真正令人惊讶的是,对于所有实例,我得到 x.public_ip_address.ip_address 作为 None 值。
我对 Azure 的 python SDK 有多个 readthedocs 参考。但是,有些链接无法正常工作。干得好 Azure :)
其中一些: https://azure-sdk-for-python.readthedocs.io/en/v1.0.3/resourcemanagementcomputenetwork.html https://azure-storage.readthedocs.io/en/latest/ref/azure.storage.file.fileservice.html
第二次编辑: 我得到了这个和上面代码的答案 return vm id 给定 public ip 地址。虽然,您可以看到它并不是绝对优化的答案。因此,欢迎更好的答案。谢谢!
文档已移至此处: https://docs.microsoft.com/python/azure/
我们进行了一些重定向,但遗憾的是无法在 RTD 上进行全局重定向,因此某些页面为 404 :/
关于你的麻烦,我会尝试直接使用publicip操作组: https://docs.microsoft.com/en-us/python/api/azure.mgmt.network.v2017_11_01.operations.publicipaddressesoperations?view=azure-python
您在网络客户端中获得了这个,因为 client.public_ip_addresses