我正在尝试使用python在FRR中添加bfd对等点。过程是这样的:
root@10:~# vtysh
Hello, this is FRRouting (version 7.6-dev-MyOwnFRRVersion-g9c28522e1).
Copyright 1996-2005 Kunihiro Ishiguro, et al.
This is a git build of frr-7.4-dev-1313-g9c28522e1
Associated branch(es):
local:master
github/frrouting/frr.git/master
10.108.161.64# configure
10.108.161.64(config)# bfd
10.108.161.64(config-bfd)# peer 10.6.5.8
10.108.161.64(config-bfd-peer)# do show bfd peers
BFD Peers:
peer 10.6.5.8 vrf default
ID: 467896786
Remote ID: 0
Active mode
Status: down
Downtime: 9 second(s)
Diagnostics: ok
Remote diagnostics: ok
Peer Type: configured
Local timers:
Detect-multiplier: 3
Receive interval: 300ms
Transmission interval: 300ms
Echo transmission interval: 50ms
Remote timers:
Detect-multiplier: 3
Receive interval: 1000ms
Transmission interval: 1000ms
Echo transmission interval: 0ms但是我无法在我的python脚本中执行相同的操作。我知道我们可以使用run_command()运行外壳命令。但在跑步时
run_command(command = "vtysh", wait=True)我被重定向到vtysh终端,并且我无法运行下一个命令。我们还可以使用
vtysh -c 但这对我没有任何帮助,因为我必须进一步到bfd终端。有谁能帮我一下吗?提前感谢
发布于 2021-07-29 15:08:00
您可以将字符串命令作为参数传递给vtysh -c "<command>"。在shell中,这将如下所示:
# vtysh -c 'show version'
Quagga 0.99.22.4 ().
Copyright 1996-2005 Kunihiro Ishiguro, et al.因此,要配置bfd参数,您应该运行:
# vtysh -c '
conf t
bfd
peer 10.6.5.8'请注意,这里的缩进是为了视觉目的,从技术上讲,您在命令中不需要缩进。
在python中使用子进程,我这样做:
import subprocess
vtysh_command = '''
conf t
bfd
peer 10.6.5.8
'''
try:
subprocess.run(['vtysh', '-c', vtysh_command],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
check=True)
except subprocess.CalledProcessError as e:
print(f'Stdout: {e.stdout.decode()}, '
f'Stderr: {e.stderr.decode()}, '
f'Exc: {e}.')我不熟悉您的run_command()实现,但我会尝试如下所示:
command = "vtysh -c 'conf t
bfd
peer 10.6.5.8
'"
run_command(command=command, wait=True)https://stackoverflow.com/questions/65793687
复制相似问题