0

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 ターミナルに移動する必要があるため、これは私にとってはまったく役に立ちません。誰でもこれで私を助けてもらえますか? 前もって感謝します

4

1 に答える 1

1

文字列コマンドを引数として に渡すことができますvtysh -c "<command>"。シェルでは、これは次のようになります。

# 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)
于 2021-07-29T15:08:00.460 に答える