To get started with Pybatfish, you will need a network snapshot. An example snapshot is packaged with Pybatfish (link) and can be used to step through the example below. Alternatively, you can package a snapshot of your own network as described here.
The following instructions show how to upload and query a network snapshot using Pybatfish in an interactive python shell like IPython. In these instructions, we assumed that Batfish is running on the same machine as Pybatfish, and the example snapshot included with Pybatfish is being analyzed.
- Import Pybatfish:
>>> from pybatfish.client.commands import * >>> from pybatfish.question.question import load_questions, list_questions >>> from pybatfish.question import bfq
- Load the question templates from the Batfish service into Pybatfish:
4. Upload a network snapshot (you’ll see some log messages followed by the
name of initialized snapshot (prefixed by
>>> bf_init_snapshot('jupyter_notebooks/networks/example') # doctest: +ELLIPSIS 'ss_...'
Here, the example network is being uploaded, but this location could also be a folder or a zip containing a custom network snapshot.
5. Ask a question about the snapshot, using one of the loaded templates (
bfq holds the questions currently loaded in Pybatfish).
For example here, the question
IPOwners fetches the mapping between IP address, interface, node and VRF for all devices in the network. :
>>> ip_owners_ans = bfq.ipOwners().answer()
answer() runs the question and returns the answer in a JSON format. See the Batfish
for the set of questions that can be asked and their parameters.
>>> ip_owners_ans.frame().head() Node VRF Interface IP Mask Active 0 as2dist2 default Loopback0 126.96.36.199 32 True 1 as2dist1 default Loopback0 188.8.131.52 32 True 2 as2dept1 default GigabitEthernet1/0 184.108.40.206 24 True 3 as2dept1 default Loopback0 220.127.116.11 32 True 4 as3border2 default GigabitEthernet1/0 18.104.22.168 24 True
7. Next, let’s ask a question about interfaces. For example, to see all prefixes present on the interface
GigabitEthernet0/0 of the node
as1border1 we can use the
interfaceProperties question like below:
>>> iface_ans = bfq.interfaceProperties(nodes='as1border1', interfaces='GigabitEthernet0/0', properties='all-prefixes').answer() >>> iface_ans Interface 0 as1border1:GigabitEthernet0/0
For additional and more in-depth examples, check out the Jupyter Notebooks.