Есть ли способ записать несколько строк одновременно с TextFSM? - PullRequest
0 голосов
/ 12 февраля 2019

Я хочу проанализировать межсетевой экран Checkpoint cphaprob -a if, выполненный через Netmiko с использованием TextFSM.Окончательно сформированный список не отформатирован.

Я уже перепробовал много комбинаций команд TextFSM, но, возможно, я просто не понимаю, как он работает должным образом.

Исходный вывод команды

Ниже приведен исходный вывод cphaprob -a if.Я хочу проанализировать виртуальный контекст (например, 'vcont 0'), имена интерфейсов (например, 'bond0'), виртуальные интерфейсы (например, 'bond0.2121') и их имена хостов (например, '10 .105.0.42 ').

vcont 0:
------
Required interfaces: 2
Required secured interfaces: 1

eth0       UP                    non sync(non secured), multicast
eth1       UP                    sync(secured), broadcast

Virtual cluster interfaces: 1

eth0            10.105.0.42        


vcont 1:
------
Required interfaces: 3
Required secured interfaces: 1

eth1       UP                    sync(secured), broadcast
bond0      UP                    non sync(non secured), multicast, bond Load Sharing  (bond0.2101)
bond1      UP                    non sync(non secured), multicast, bond Load Sharing  (bond1.2126)

Virtual cluster interfaces: 3

bond0.2121      10.65.29.21         
bond1.2122      10.65.29.22        
bond1.2123      10.65.29.23        


vcont 2:
------
Required interfaces: 3
Required secured interfaces: 1

eth1       UP                    sync(secured), broadcast
bond1      UP                    non sync(non secured), multicast, bond Load Sharing  (bond1.2127)
bond0      UP                    non sync(non secured), multicast, bond Load Sharing  (bond0.2102)

Virtual cluster interfaces: 2

bond1.4242      10.65.29.42        
bond0.4243      10.65.29.43         

Шаблон TextFSM

# template for ```cphaprob -a if``` command.
Value Context (\S+\s\d+)
Value List Interface (\S+)
Value List VirtualInterface (\S+)
Value List IPv4 (\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3})

Start
  ^${Context}:
  ^${Interface}.*(UP|DOWN|Disconnected)
  ^Virtual cluster interfaces: \d+ -> Cluster

Cluster
  ^${VirtualInterface}\s+${IPv4} -> Record Start

Ожидаемые результаты

$ python tests/test_checkpoint_functions.py 
[['vcont 0', ['eth0', 'eth1'], ['eth0'], ['10.105.0.42']],
 ['vcont 1', ['eth1', 'bond0', 'bond1'], ['bond0.2121', 'bond1.2122', 'bond1.2123'], ['10.65.29.21', '10.65.29.22', '10.65.29.23']],
 ['vcont 2', ['eth1', 'bond1', 'bond0'], ['bond1.4242', 'bond0.4243'], ['10.65.29.42', '10.65.29.43']]]

Фактические результаты

$ python tests/test_checkpoint_functions.py 
[['vcont 0', ['eth0', 'eth1'], ['eth0'], ['10.105.0.42']],
 ['vcont 1', ['eth1', 'bond0', 'bond1'], ['bond0.2121'], ['10.65.29.21']],
 ['vcont 2', ['eth1', 'bond1', 'bond0'], ['bond1.4242'], ['10.65.29.42']]]

Как видите, только яполучить 1-е появление виртуальных интерфейсов и их соответствующих IP-адресов.Причина может заключаться в том, что в моем шаблоне в состоянии кластера я записываю сразу после ^${VirtualInterface}\s+${IPv4} -> Record Start.Я просто не могу понять, как получить все виртуальные интерфейсы и IP-адреса в соответствующих списках.

1 Ответ

0 голосов
/ 25 мая 2019
Value Context (\s+\s\d+)
Value List Interface (\S+)
Value List VirtualInterface (\S+)
# Add escaping for "."
Value List IPv4 (\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})

Start
  ^${Context}:
  ^${Interface}.*(UP|DOWN|Disconnected)
  ^Virtual cluster interfaces: \d+ -> Cluster

Cluster
  ^${VirtualInterface}\s+${IPv4}
  # The reason for the multiple VirtualInterface and IPv4 entries
  # is because you Record after each match.
  # Instead, you can Record after you have matched all entries.
  # I look for what I know will be the start of the next entry in table,
  # and use Continue.Record.
  # Continue will continue looking for matches for the current line that is being parsed,
  # but will move onto the next regex line, instead of starting back at the top of State.
  # Another thing about Continue, is that you cannot do that and have a State Change,
  # which is why I change to the Start state afterwards
  ^.+: -> Continue.Record
  ^${Context}: -> Start
...