Adding bfd peers using python

499 Views Asked by At

I am trying to add bfd peers in FRR using python. The process goes like this:

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

But I am unable to perform the same in my python script. I know we can run shell commands using run_command(). But on running

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

I am redirected to the vtysh terminal, and I am unable to run the next commands. Also we can use

vtysh -c 

but this is no way helpful to me because I have to further go to the bfd terminal. Can anyone please help me with this? Thanks in advance

1

There are 1 best solutions below

0
On

You can pass string command as an argument to vtysh -c "<command>". In shell this will look like:

# vtysh -c 'show version'
Quagga 0.99.22.4 ().
Copyright 1996-2005 Kunihiro Ishiguro, et al.

So, to configure bfd parameters you should run:

# vtysh -c '
conf t
 bfd
  peer 10.6.5.8'

Note, that indentation here is done for visual purpose, technically you don't indentation here in command.

In python using subprocess I do like this:

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}.')

I'm not familiar with your implementation of run_command(), but I'd try something like this:

command = "vtysh -c 'conf t
 bfd
  peer 10.6.5.8
'"

run_command(command=command, wait=True)