[英]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 :现在你的嵌套
for
和if
结构可以表示为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.