【Azure Developer】使用Python代码获取VM的IP地址 (Public IP + Private IP)【未解决问题标签】
记录使用以下的代码获取Azure VM中的IP地址
"""Create and manage virtual machines. This script expects that the following environment vars are set: AZURE_TENANT_ID: your Azure Active Directory tenant id or domain
AZURE_CLIENT_ID: your Azure Active Directory Application Client ID
AZURE_CLIENT_SECRET: your Azure Active Directory Application Secret
AZURE_SUBSCRIPTION_ID: your Azure Subscription Id
"""
import os
import traceback from azure.common.credentials import ServicePrincipalCredentials
from azure.mgmt.resource import ResourceManagementClient
from azure.mgmt.network import NetworkManagementClient
from azure.mgmt.compute import ComputeManagementClient
from azure.mgmt.compute.models import DiskCreateOption from msrestazure.azure_exceptions import CloudError
from msrestazure.azure_cloud import AZURE_CHINA_CLOUD # from azure.identity import DefaultAzureCredential
# from azure.identity import ClientSecretCredential
# credentials = ClientSecretCredential(client_id='xxxxxxxx-xxxx-xxxx-xxxx-76f50363af33', client_secret='.~V9ij1.5Y_F8rL_k8DNpj~RSLFf~H56nH', tenant_id='xxxxxxxx-xxxx-xxxx-xxxx-1316152d9587',authority=AzureAuthorityHosts.AZURE_CHINA)
# token =credentials.get_token("https://microsoftgraph.chinacloudapi.cn/.default")
# print(token) def get_credentials():
subscription_id = os.environ['AZURE_SUBSCRIPTION_ID']
print(subscription_id)
credentials = ServicePrincipalCredentials(
client_id=os.environ['AZURE_CLIENT_ID'],
secret=os.environ['AZURE_CLIENT_SECRET'],
tenant=os.environ['AZURE_TENANT_ID'],
resource="https://management.chinacloudapi.cn/",
authority=AZURE_CHINA_CLOUD.endpoints.active_directory,
scopes="https://management.chinacloudapi.cn/.default",
china=True
) # credentials = ClientSecretCredential(
# client_id=os.environ['AZURE_CLIENT_ID'],
# client_secret=os.environ['AZURE_CLIENT_SECRET'],
# tenant_id=os.environ['AZURE_TENANT_ID'],
# resource="https://management.chinacloudapi.cn/",
# authority=AZURE_CHINA_CLOUD.endpoints.active_directory,
# scopes="https://management.chinacloudapi.cn/.default",
# china=True
# ) return credentials, subscription_id def run_example():
"""Virtual Machine management example."""
#
# Create all clients with an Application (service principal) token provider
#
credentials, subscription_id = get_credentials()
#access_token = credentials.token['access_token']
#print(access_token) # client2 = MonitorManagementClient(
# credential,
# subscription_id,
# base_url=CLOUD.endpoints.resource_manager,
# credential_scopes=[CLOUD.endpoints.resource_manager + "/.default"]
# ) resource_client = ResourceManagementClient(credentials, subscription_id,base_url="https://management.chinacloudapi.cn/",scopes="https://management.chinacloudapi.cn/.default")
compute_client = ComputeManagementClient(credentials, subscription_id,base_url="https://management.chinacloudapi.cn/",scopes="https://management.chinacloudapi.cn/.default")
network_client = NetworkManagementClient(credentials, subscription_id,base_url="https://management.chinacloudapi.cn/",scopes="https://management.chinacloudapi.cn/.default")
print(AZURE_CHINA_CLOUD.endpoints.active_directory_resource_id) for public_ip in network_client.public_ip_addresses.list_all():
print("Public IP: {}:{}".format(public_ip.id, public_ip.ip_address)) for network_interface in network_client.network_interfaces.list_all():
if (network_interface.virtual_machine != None):
print("VM ID: " + network_interface.virtual_machine.id)
for ip_configuration in network_interface.ip_configurations:
if (ip_configuration.primary):
print("VM Used Public IP: " + ip_configuration.public_ip_address.id) try:
# List VMs in subscription
print('\nList VMs in subscription')
for vm in compute_client.virtual_machines.list_all():
print("\tVM: {}".format(vm)) except CloudError:
print('A VM operation failed:\n{}'.format(traceback.format_exc()))
else:
print('All example operations completed successfully!') if __name__ == "__main__":
run_example()
但是,结果却没有能成功。
遇见问题:
https://management.chinacloudapi.cn/.default
Traceback (most recent call last):
File "C:\Users\AppData\Local\Programs\Python\Python310\lib\runpy.py", line 196, in _run_module_as_main
return _run_code(code, main_globals, None,
File "C:\Users\AppData\Local\Programs\Python\Python310\lib\runpy.py", line 86, in _run_code
exec(code, run_globals)
File "c:\Users\.vscode\extensions\ms-python.python-2022.14.0\pythonFiles\lib\python\debugpy\__main__.py", line 39, in <module>
cli.main()
File "c:\Users\.vscode\extensions\ms-python.python-2022.14.0\pythonFiles\lib\python\debugpy/..\debugpy\server\cli.py", line 430, in main
run()
File "c:\Users\.vscode\extensions\ms-python.python-2022.14.0\pythonFiles\lib\python\debugpy/..\debugpy\server\cli.py", line 284, in run_file
runpy.run_path(target, run_name="__main__")
File "c:\Users\.vscode\extensions\ms-python.python-2022.14.0\pythonFiles\lib\python\debugpy\_vendored\pydevd\_pydevd_bundle\pydevd_runpy.py", line 321, in run_path
return _run_module_code(code, init_globals, run_name,
File "c:\Users\.vscode\extensions\ms-python.python-2022.14.0\pythonFiles\lib\python\debugpy\_vendored\pydevd\_pydevd_bundle\pydevd_runpy.py", line 135, in _run_module_code
_run_code(code, mod_globals, init_globals,
File "c:\Users\.vscode\extensions\ms-python.python-2022.14.0\pythonFiles\lib\python\debugpy\_vendored\pydevd\_pydevd_bundle\pydevd_runpy.py", line 124, in _run_code
exec(code, run_globals)
File "c:\LBWorkSpace\MyCode\61-VM-Python\getIPaddress\getip.py", line 97, in <module>
run_example()
File "c:\LBWorkSpace\MyCode\61-VM-Python\getIPaddress\getip.py", line 75, in run_example
for public_ip in network_client.public_ip_addresses.list_all():
File "C:\Users\AppData\Local\Programs\Python\Python310\lib\site-packages\azure\core\paging.py", line 128, in __next__
return next(self._page_iterator)
File "C:\Users\AppData\Local\Programs\Python\Python310\lib\site-packages\azure\core\paging.py", line 76, in __next__
self._response = self._get_next(self.continuation_token)
File "C:\Users\AppData\Local\Programs\Python\Python310\lib\site-packages\azure\mgmt\network\v2022_01_01\operations\_operations.py", line 32711, in get_next
pipeline_response = self._client._pipeline.run( # pylint: disable=protected-access
File "C:\Users\AppData\Local\Programs\Python\Python310\lib\site-packages\azure\core\pipeline\_base.py", line 211, in run
return first_node.send(pipeline_request) # type: ignore
File "C:\Users\AppData\Local\Programs\Python\Python310\lib\site-packages\azure\core\pipeline\_base.py", line 71, in send
response = self.next.send(request)
File "C:\Users\AppData\Local\Programs\Python\Python310\lib\site-packages\azure\core\pipeline\_base.py", line 71, in send
response = self.next.send(request)
File "C:\Users\AppData\Local\Programs\Python\Python310\lib\site-packages\azure\core\pipeline\_base.py", line 71, in send
response = self.next.send(request)
[Previous line repeated 2 more times]
File "C:\Users\AppData\Local\Programs\Python\Python310\lib\site-packages\azure\mgmt\core\policies\_base.py", line 47, in send
response = self.next.send(request)
File "C:\Users\AppData\Local\Programs\Python\Python310\lib\site-packages\azure\core\pipeline\policies\_redirect.py", line 158, in send
response = self.next.send(request)
File "C:\Users\AppData\Local\Programs\Python\Python310\lib\site-packages\azure\core\pipeline\policies\_retry.py", line 446, in send
response = self.next.send(request)
File "C:\Users\AppData\Local\Programs\Python\Python310\lib\site-packages\azure\core\pipeline\policies\_authentication.py", line 119, in send
self.on_request(request)
File "C:\Users\AppData\Local\Programs\Python\Python310\lib\site-packages\azure\core\pipeline\policies\_authentication.py", line 96, in on_request
self._token = self._credential.get_token(*self._scopes)
AttributeError: 'ServicePrincipalCredentials' object has no attribute 'get_token'. Did you mean: 'set_token'?
通过Bing搜索查找,怀疑是packages中版本冲突引起。参考链接:
https://github.com/Azure/azure-sdk-for-python/issues/16908
https://github.com/Azure/azure-sdk-for-python/issues/14059
@2022-10-01, 还没有解决它。
【Azure Developer】使用Python代码获取VM的IP地址 (Public IP + Private IP)【未解决问题标签】的更多相关文章
- 【Azure Developer】Python代码通过AAD认证访问微软Azure密钥保管库(Azure Key Vault)中机密信息(Secret)
关键字说明 什么是 Azure Active Directory?Azure Active Directory(Azure AD, AAD) 是 Microsoft 的基于云的标识和访问管理服务,可帮 ...
- 获取Mac、CPUID、硬盘序列号、本地IP地址、外网IP地址OCX控件
提供获取Mac.CPUID.硬盘序列号.本地IP地址.外网IP地址OCX控件 开发语言:vc++ 可应用与WEB程序开发应用 <HTML><HEAD><TITLE> ...
- 【Azure Redis 缓存】使用Python代码获取Azure Redis的监控指标值 (含Powershell脚本方式)
问题描述 通过Metrics监控页面,我们能得知当前资源(如Redis)的运行情况与各种指标.如果我们需要把指标下载到本地或者生成JSON数据导入到第三方的监控平台呢?Azure是否可以通过Pytho ...
- 【Azure Developer】Python 获取Micrisoft Graph API资源的Access Token, 并调用Microsoft Graph API servicePrincipals接口获取应用ID
问题描述 在Azure开发中,我们时常面临获取Authorization问题,需要使用代码获取到Access Token后,在调用对应的API,如servicePrincipals接口. 如果是直接调 ...
- 获取ip地址,并根据ip获取当前省份
<script src="http://pv.sohu.com/cityjson?ie=utf-8"></script> //methods里面 // 获取 ...
- 【Azure 环境】【Azure Developer】使用Python代码获取Azure 中的资源的Metrics定义及数据
问题描述 使用Python SDK来获取Azure上的各种资源的Metrics的名称以及Metrics Data的示例 问题解答 通过 azure-monitor-query ,可以创建一个 metr ...
- 编写python代码获取4k高清壁纸
Huskiesir最近在研究python爬虫大约俩周了吧,由于比较懒,也没把具体研究的过程与经验写下来,实在是一大憾事.这次直接上干货,代码送给大家: import re import request ...
- 【Azure Developer】使用Postman获取Azure AD中注册应用程序的授权Token,及为Azure REST API设置Authorization
Azure Active Directory (Azure AD) is Microsoft's cloud-based identity and access management service, ...
- 利用python代码获取文件特定的内容,并保存为文档
说明:有段时间需要读取上百个文件的单点能(sp),就写了下面的代码(计算化学狗努力转行中^-^) import os.path import re # 1 遍历指定目录,显示目录下的所有文件名 def ...
- 【Azure Developer】使用 CURL 获取 Key Vault 中 Secrets 中的值
问题描述 在使用CURL通过REST API获取Azure Key Vaualt的Secrets值,提示Missing Token, 问如何来生成正确的Token呢? # curl 命令 curl - ...
随机推荐
- Docker容器基础入门认知-Cgroup
在上一篇说完 namespace 给容器技术提供了隔离之后,我们在介绍一下容器的"限制"问题 也许你会好奇,我们不是已经通过 Linux Namespace 给容器创建了一个容器了 ...
- Liunx网络配置
1.安装精简版:CentOS-7-x86_64-Minimal-2009.iso 2.进入配置文件: vi /etc/sysconfig/network-scripts/ifcfg-ens33 3. ...
- 解决Chrome翻译无法使用
截止2022年11月3日自己ping出的ip不可用了 可以用以下ip 172.217.215.90 172.253.115.90 142.250.126.90 142.250.10.90 142.25 ...
- python中--try except 异常捕获以及正则化、替换异常值
1.异常处理过程 异常名称 描述 BaseException 所有异常的基类 SystemExit 解释器请求退出 KeyboardInterrupt 用户中断执行(通常是输入^C) Exceptio ...
- 基于 hugging face 预训练模型的实体识别智能标注方案:生成doccano要求json格式
强烈推荐:数据标注平台doccano----简介.安装.使用.踩坑记录_汀.的博客-CSDN博客_doccano huggingface官网 参考:数据标注平台doccano----简介.安装.使用. ...
- C/C++ 反汇编:数据类型与常量
反汇编即把目标二进制机器码转为汇编代码的过程,该技术常用于软件破解.外挂技术.病毒分析.逆向工程.软件汉化等领域,学习和理解反汇编对软件调试.系统漏洞挖掘.内核原理及理解高级语言代码都有相当大的帮助, ...
- Vue3学习笔记 —— 状态管理、Vuex、Pinia (未完结)
优秀文章分享:vue中使用vuex(超详细) - 掘金 (juejin.cn) 一.状态管理 1.1.什么是状态管理? 理论上来说,每一个 Vue 组件实例都已经在"管理"它自己的 ...
- ”动态“修改MAC地址
一:获取MAC地址 1.自定义的MAC地址 这里是例程中存放自定义MAC地址的位置,如果想修改MAC地址可以在此处修改.一般例程这里是灰色的需要在工程预编译处配置. 可以看到MCU.c文件中此处代码生 ...
- USACO 2022 Cu 题解
USACO 2022 Cu 题解 AK用时:$ 3 $ 小时 $ 30 $ 分钟. A - Cow College 原题 Farmer John 计划为奶牛们新开办一所大学! 有 $ N $($ 1 ...
- Mysql 创建外键、索引的问题
总结: 创建外键的列,要求必须创建索引,通常我们只需要创建外键就可,索引他会自动创建.若是索引那里已经存在了组合索引,那么组合索引前面的第一列已经有了索引,所以创建外键的时候不会自动创建,但是后面的列 ...