我有以下代码:
import paramiko
import re
router_ip = "10.10.10.10"
router_username = "admin"
router_password = "admin"
ssh = paramiko.SSHClient()
# Load SSH host keys.
ssh.load_system_host_keys()
# Add SSH host key automatically if needed.
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
# Connect to router using username/password authentication.
ssh.connect(router_ip,
username=router_username,
password=router_password,
look_for_keys=False)
# Run command.
ssh_stdin, ssh_stdout, ssh_stderr = ssh.exec_command(
"show l2route evpn mac-ip all | i HMM")
output = ssh_stdout.readlines()
output1 = " ".join(output)
ip_list = re.findall(r'[0-9]+(?:\.[0-9]+){3}', output1)
print(output1)
print(ip_list)
#ssh.close()我必须运行命令"show bgp l2vpn evpn vrf Tenant-1“,其中"”是列表"ip_list“中的每一项(即ip),如果有任何输出包含字符串"VNI”而不是exit,则停止运行任何命令,并打印"match found,stop here“。该怎么做呢?看起来我不得不使用for循环,但我不清楚该怎么做。
请帮帮忙。我是python的新手。
发布于 2020-10-28 03:05:20
当您在这里获得IP列表时
ip_list = re.findall(r'[0-9]+(?:\.[0-9]+){3}', output1)运行此命令
for ip in ip_list:
ssh_stdin, ssh_stdout, ssh_stderr = ssh.exec_command(f"show bgp l2vpn evpn {ip} vrf Tenant-1")
output = ssh_stdout.readlines()
output1 = " ".join(output)
if "VNI" in output1:
print("match found, stop here")
exit(0)https://stackoverflow.com/questions/64565826
复制相似问题