项目作者: natenka

项目描述 :
Python Q&A for Network Engineers
高级语言: Python
项目地址: git://github.com/natenka/q_and_a.git
创建时间: 2021-07-12T10:25:43Z
项目社区:https://github.com/natenka/q_and_a

开源协议:MIT License

下载


Q & A

I am often asked questions about how to solve this or that problem, and I decided to post
these questions and solutions here, in case it is also useful to someone.

The description of the question can be used as a task and you can first solve it yourself, and then see the solution.

QA Description Topics/modules used in solutions
1 Split the interface configuration into two parts regex, format, jinja2
2 Network Topology Discovery Using CDP/LLDP scrapli, regex, queue, click, rich
3 Filter JSON data by key recursion, generator, regex, click
4 Collect Port Status Information scrapli, concurrent.futures, regex, rich

Q&A 1 Split the interface configuration into two parts

English translation

Надо разбить настройку интерфейса на две части.
Есть конфигурация интерфейса такого вида:

  1. set interfaces ae0 unit 1001 description "EXAMPLE_1001"
  2. set interfaces ae0 unit 1001 vlan-tags outer 18
  3. set interfaces ae0 unit 1001 vlan-tags inner 10
  4. set interfaces ae0 unit 1001 family inet policer input P-IN-L2
  5. set interfaces ae0 unit 1001 family inet policer output P-OUT-L2
  6. set interfaces ae0 unit 1001 family inet address 60.1.1.1/30

Эту конфигурацию надо разбить на две части:

  1. set interfaces ae9 unit 1001 description "EXAMPLE_1001"
  2. set interfaces ae9 unit 1001 encapsulation vlan-bridge
  3. set interfaces ae9 unit 1001 vlan-tags outer 18
  4. set interfaces ae9 unit 1001 vlan-tags inner 10
  5. set interfaces ae9 unit 1001 family bridge policer input P-IN-L2
  6. set interfaces ae9 unit 1001 family bridge policer output P-OUT-L2

и

  1. set interfaces irb unit 1001 description "EXAMPLE_1001"
  2. set interfaces irb unit 1001 family inet address 60.1.1.1/30
  3. set interfaces irb unit 1001 mac 00:ff:3c:01:01:01

Подробнее

Q&A 2 Network Topology Discovery Using CDP/LLDP

English translation

Надо обнаружить топологию сети через вывод CDP (считаем что CDP есть на всех устройствах).
Для старта должен быть известен IP-адрес одного устройства и параметры подключения
по SSH ко всем устройствам в сети.

Надо подключиться к первому устройству, дать команду sh cdp neighbors detail, получить
всех соседей и их IP-адреса и подключаться к каждому соседу.
На каждом соседе опять дать команду sh cdp neighbors detail и получить соседей этого устройства.
Так надо пройтись по всей сети и собрать информацию об устройствах и топологии.

Подробнее

Q&A 3 Filter JSON data by key

English translation

Задача отфильтровать данные из JSON файла по указанному ключу. Технически речь
об отборе данных из словаря/списка, так как после чтения данных в Python это уже будет
Python list/dict.
JSON упоминается потому что именно в этом формате часто очень большая вложенность.

  1. $ python solution_2a.py json_files/cfg.json name
  2. ['ae1.185', 'v185', 'ae47.128', 'v128', 'ae1.139', 'v139', 'ae1.140', 'v140', 'User1', 'User2', 'User3', 'ge-0/0/0', '192.168.1.1/29', 11, '10.1.1.1/29', 'ge-0/0/1', '192.168.199.1/30']
  3. $ python solution_2a.py json_files/cfg.json user
  4. [
  5. [
  6. {'authentication': {'encrypted-password': 'password'}, 'class': 'super-user', 'name': 'User1', 'uid': 1000},
  7. {'authentication': {'encrypted-password': 'password'}, 'class': 'super-user', 'name': 'User2', 'uid': 2001},
  8. {'authentication': {'encrypted-password': 'password'}, 'class': 'super-user', 'name': 'User3', 'uid': 2002}
  9. ]
  10. ]
  11. $ python solution_2a.py json_files/cfg.json user name
  12. ['User1', 'User2', 'User3']

Q&A 4 Collect Port Status Information

English translation

Задача собрать информацию о статусе портов на оборудовании (up/down/admin down для Cisco IOS).
На первом этапе собирается информация о всех портах (Loopback/физические/Tunnel/…) и их статусе.
На втором этапе из собраной информации надо отобрать только физические порты. И на третьем сохранять
информацию о статусе портов и добавить возможность сравнивать изменения
в статусе портов (текущий статус с последним записанным).

solution_1

solution_1_stats