首页 > 解决方案 > 使用 python 添加 bfd 对等体

问题描述

我正在尝试使用 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() 运行 shell 命令。但在跑步时

run_command(command = "vtysh", wait=True)

我被重定向到 vtysh 终端,我无法运行下一个命令。我们也可以使用

vtysh -c 

但这对我没有帮助,因为我必须进一步去 bfd 终端。谁能帮我解决这个问题?提前致谢

标签: pythonshellpeerbfd

解决方案


您可以将字符串命令作为参数传递给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)

推荐阅读