简体   繁体   English

如果数字 output 显示为字符串,如何匹配 IF 语句中的数字范围?

[英]How do I match number ranges in an IF statement if the output of numbers are presented as a string?

I am a.network engineer by day learning python to automate tasks, please go easy as I am a python newbie.我是一名网络工程师,每天学习 python 来自动执行任务,请 go 简单一点,因为我是 python 新手。 My goal is to iterate through a range of switchport interfaces and identify down switchport interfaces, then apply a new VLAN ID to the port.我的目标是遍历一系列交换端口接口并识别关闭的交换端口接口,然后将新的 VLAN ID 应用到该端口。

The first stage of my script is below, which presents me with a list of down ports.下面是我脚本的第一阶段,它向我提供了一个关闭端口列表。

The issue I am facing is that I am wanting to over iterate over port numbers 3-6, 38-52 and that are down.我面临的问题是,我想过度迭代端口号 3-6、38-52 并且它们已关闭。

At present I am iterating through the entire list of ports identified on the switch.目前,我正在遍历交换机上标识的整个端口列表。

import netmiko

from netmiko import ConnectHandler
from getpass4 import getpass

user = 'example_user'
password = getpass('Password: ')

net_connect = ConnectHandler(
    device_type="hp_procurve",
    host="10.0.0.1",
    username= user,
    password= password,
)

print('*** Sending command ***')

show_int_brief = net_connect.send_command("show int brief", use_textfsm=True)

net_connect.disconnect()

int_down = []
for item in show_int_brief:
    if item['status'] == 'Down':
        int_down.append(item['port'])

print('*** Port status known as down ***\n', int_down)


Example output prior to being added to the list int_down.示例 output 在被添加到列表 int_down 之前。


[{'port': '1', 'type': '100/1000T', 'intrusion_alert': 'No', 'enabled': 'Yes', 'status': 'Up', 'mode': '1000FDx', 'mdi_mode': 'MDI', 'flow_ctrl': 'off', 'bcast_limit': '0'},
 {'port': '2', 'type': '100/1000T', 'intrusion_alert': 'No', 'enabled': 'Yes', 'status': 'Up', 'mode': '1000FDx', 'mdi_mode': 'MDIX', 'flow_ctrl': 'off', 'bcast_limit': '0'},
{'port': '3', 'type': '100/1000T', 'intrusion_alert': 'No', 'enabled': 'Yes', 'status': 'Down', 'mode': '1000FDx', 'mdi_mode': 'Auto', 'flow_ctrl': 'off', 'bcast_limit': '0'}, 
{'port': '4', 'type': '100/1000T', 'intrusion_alert': 'No', 'enabled': 'Yes', 'status': 'Down', 'mode': '1000FDx', 'mdi_mode': 'Auto', 'flow_ctrl': 'off', 'bcast_limit': '0'}, 
{'port': '5', 'type': '100/1000T', 'intrusion_alert': 'No', 'enabled': 'Yes', 'status': 'Down', 'mode': '1000FDx', 'mdi_mode': 'Auto', 'flow_ctrl': 'off', 'bcast_limit': '0'}, 
{'port': '6', 'type': '100/1000T', 'intrusion_alert': 'No', 'enabled': 'Yes', 'status': 'Down', 'mode': '1000FDx', 'mdi_mode': 'Auto', 'flow_ctrl': 'off', 'bcast_limit': '0'}]

And so on..


Example output after being placed in 'int_down' and printed.示例 output 放入“int_down”并打印后。

Numbers identified are expected, as these are in a down state.确定的数字是预期的,因为这些数字在 state 中。

['3', '4', '5', '6', '7', '8', '9', '30', '31', '32', '33', '34', '35', '36', '37', '38', '39', '40', '41', '42', '43', '44', '45', '47', '49', '50', '51', '52']

The idea is to then use this list in another command that will proceed with applying VLAN configuration only to these ports, though will tackle this as I get past this hurdle.我的想法是在另一个命令中使用此列表,该命令将继续仅将 VLAN 配置应用于这些端口,但在我越过这个障碍时会解决这个问题。

Cheers,干杯,

Luppa卢帕

I don't think it is worth to limit to a range a priory, since ensuring the port data is in the proper shape and organization would be similar or even more computational effort with respect to looping through all ports directly.我认为将修道院限制在一个范围内是不值得的,因为确保端口数据处于正确的形状和组织将与直接遍历所有端口相似甚至更多的计算工作。 Instead I suggest to filter ad-hoc:相反,我建议临时过滤:

I use the boild down list of ports-dicts:我使用 ports-dicts 的简化列表:

ports = [
    {'port': '1', 'status': 'Down'},
    {'port': '7', 'status': 'Down'},
    {'port': '2', 'status': 'Up'},
    {'port': '8', 'status': 'Up'},
    {'port': '4', 'status': 'Down'},
    {'port': '5', 'status': 'Up'},
    {'port': '9', 'status': 'Up'},
    {'port': '6', 'status': 'Down'}
]

Please note that they are not in order and have missing entries to simulate real world data more closely.请注意,它们没有按顺序排列并且缺少条目以更接近地模拟真实世界的数据。

Then first I implement a helper function, where I can specify the ranges of interest.然后我首先实现一个助手 function,我可以在其中指定感兴趣的范围。 If these are more than two consider to use any() and a list of ranges for readability.如果这些超过两个,请考虑使用any()和范围列表以提高可读性。

def of_interest(port_num: int) -> bool:
    return port_num in range(3, 7) or port_num in range(38, 53)

Now your nested for and if structure can be expressed as list comprehension :现在你的嵌套forif结构可以表示为list理解

down_ports = [e['port'] for e in ports if e['status'] == 'Down' and of_interest(int(e['port']))]

Of course, depending on what you want to do in later steps it might make sense to copy all values from the port list of that entry.当然,根据您要在后续步骤中执行的操作,从该条目的端口列表中复制所有值可能是有意义的。

Does this help?这有帮助吗?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从具有数字范围的字符串中获取数字列表 - Get list of numbers from string with number ranges 我有一个包含字符串和浮点数字的列表,如何按降序对数字进行排序并将列表打印到标准输出 - I have a list contains string and float numbers, how to sort number by descending order and print the list to standard output 如何计算字符串中数字的数量 - How can I count the number of numbers in a string 如何仅从字符串列表中提取数字并根据输出在数据框中创建列? - How do i extract numbers only from a list of string and create a columns in dataframe based on output? if语句与bs4输出中的String匹配 - If statement to match String in bs4 output 如何在Python 2.7.2中获取一串数字并使每个数字成为列表中自己的元素? - How do I take a string of numbers in Python 2.7.2 and make each number its own element in a list? 如果每个数字旁边都有一个字符串,我如何使用此代码查找文件中的前 5 个最大数字? - how do i use this code to find the top 5 max numbers in a file if each number has a string next to it? 如何将要求的整数/数字的数字加在一起,或者应该是字符串? - How do I add together the numbers of a requested integer/number or should it be a string? 如何在多字符串变量中的特定文本后使用正则表达式匹配随机数? - How do i match a random number using regex after a particular text in a multi string variable? 如何使用正则表达式将字符串与python中的数字匹配? - How do I match a string up to a number in python using regular expressions?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM