Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Azure SDK Python,我想为NSG安全规则分配多个端口_Python_Azure_Azure Sdk Python - Fatal编程技术网

Azure SDK Python,我想为NSG安全规则分配多个端口

Azure SDK Python,我想为NSG安全规则分配多个端口,python,azure,azure-sdk-python,Python,Azure,Azure Sdk Python,我试图用Python操作Azure安全组。 大局在起作用 但是,尝试将规则添加到现有安全组 network_client.security_rules.create_or_update('resourcesname',"nsg-name","secure-name",SecurityRule( protocol='Tcp', source_address_prefix='*', destination_a

我试图用Python操作Azure安全组。 大局在起作用

但是,尝试将规则添加到现有安全组

network_client.security_rules.create_or_update('resourcesname',"nsg-name","secure-name",SecurityRule(
        protocol='Tcp', 
        source_address_prefix='*', 
        destination_address_prefix='*', 
        access='Allow', 
        direction='Inbound', description=name+' use rules',source_port_range='*', 
        #destination_port_range="1000,2000",
        #destination_port_range=["1000","2000"],
        destination_port_range=[1000,2000],
        priority=100, name="secure-name"))
当我指定多个端口时,出现以下错误

msrestazure.azure_exceptions.CloudError: Azure Error: SecurityRuleInvalidPortRange
Message: Security rule has invalid Port range. Value provided: [1000,2000]. Value should be an integer OR integer range with '-' delimiter. Valid range 0-65535.
我也尝试过字符串数组和简单字符串。 但它失败了。
有人能解决这个问题吗?

事实上,这很简单

如果要添加端口范围,应使用属性
destination\u port\u ranges
,而不是
destination\u port\u range
注意两个属性末尾的“s”)

这是我的密码:

from azure.common.credentials import ServicePrincipalCredentials
from azure.mgmt.network import NetworkManagementClient
from azure.mgmt.network.v2020_04_01.models import NetworkSecurityGroup, SecurityRule

subscription_id = 'xxx'
credentials = ServicePrincipalCredentials(
    client_id = 'xxx',
    secret = 'xxx',
    tenant = 'xxx'
)

network_client = NetworkManagementClient(
    credentials,
    subscription_id
)

network_client.security_rules.create_or_update('xxx',"yysecurityGroup","my_Port_8080",SecurityRule(
        protocol='Tcp', 
        source_address_prefix='*', 
        destination_address_prefix='*', 
        access='Allow', 
        direction='Inbound', description='my_Port_8080 use rules',source_port_range='*', 
        #destination_port_range="1000,2000",
        destination_port_ranges=["1000","1005","2005","2020"],     
        priority=100, name="my_Port_8080"))


print("**complete**")
测试结果:


根据错误中的建议,您是否尝试按照以下方法定义目标端口范围<代码>目的地\端口\范围=[1000-2000]如果执行上述操作,会发生什么?如果您在上述语法中遇到错误,能否与他人分享?请尝试。“1000-2000”或1000将成功。但是目标端口不是连续的。@user14020029,请查看下面的答案,它可以工作。谢谢您的帮助。但我不能向上压,因为我不够强壮…我会在我能的时候按它。太棒了。你救了我的命!!你很安全。