我正在尝试构建一个Python脚本,它首先显示界面的描述,然后检查界面是否有(向上向上)或(向上向下)不需要任何操作,否则如果界面是(向下向下)然后打印界面名字。
这是应用脚本之前的输出
Switch# show int description | section ABC
Hu1/0/14 up up ABC
Hu1/0/30 up down ABC
Hu1/0/31 down down ABC
应用脚本后的输出应该是
Hu1/0/31
有多种方法可以与网络设备通信,例如 telnet、ssh、netconf 等,并且每种方法都有可用的模块。
应该满足您需要的示例代码
import re
from netmiko import ConnectHandler
username = "test"
password = "test"
enablePwd = "test"
verification_cmd = 'show int description | section ABC'
net_connect = ConnectHandler(device_type="cisco_ios",ip=host,username=userName,password=passWord,secret=enablePwd)
net_connect.enable()
sh_command_output = net_connect.send_command(verification_cmd)
for line in sh_command_output.split('\n'):
if re.findall(r'down.*down', line):
print(line.split(' ')[0])