Bird CFG Creator

Learn more about Bird

Using this API the user can create his own custom bird configuration file, adding/removing routes and routing protocols. When the final config file is read to send, you can use PyBirdClient to do so.

The working flow is simple as:
  • Create BirdCFGCreator object, passing current bird configuration as a string.

  • Manipulate the future config: add/remove routes and routing protocols.

  • Create the final bird.conf content as a string.

The following snippet create bird configuration with 2 bgp protocols and 2 routes

bird_cfg = BirdCFGCreator()

# defining protocols data as strings
bgp_data1 = """
    local 1.1.1.3 as 65000;
    neighbor 1.1.1.1 as 65000;
    ipv4 {
            import all;
            export all;
    };
"""
bgp_data2 = """
    local 1.1.2.3 as 65000;
    neighbor 1.1.2.1 as 65000;
    ipv4 {
            import all;
            export all;
    };
"""

# adding protocols
bird_cfg.add_protocol(protocol = "bgp", name = "my_bgp1", data = bgp_data1)
bird_cfg.add_protocol(protocol = "bgp", name = "my_bgp2", data = bgp_data2)

# adding routes
bird_cfg.add_route(dst_cidr = "42.42.42.42/32", next_hop = "1.1.1.3")
bird_cfg.add_route(dst_cidr = "42.42.42.43/32", next_hop = "1.1.2.3")

# build combined configuration
cfg = bird_cfg.build_config()

BirdCFGCreator Class

class trex.pybird.bird_cfg_creator.BirdCFGCreator(cfg_string='\nrouter id 100.100.100.100;\nprotocol device {\n    scan time 1;\n}\n')[source]

This class is used to create bird.conf file. Able to read a given config or using a default one, add/remove routes/routes protocols. And finally create the wanted config file.

Construct BirdCFGCreator object with a given cfg_string.

Parameters
cfg_string: string

The given bird.conf with all the protocols as string. In case cfg_string was not supply default cfg will be filled instead

add_protocol(protocol, name, data, from_conf=False)[source]

Add protocol to our future cfg.

Parameters
protocol: string
The protocol we are about to add

i.e: bgp, rip..

name: string
The name of out protocol in bird

i.e: bgp1, my_rip Must be unique in the cfg.

data: string
The data inside the protocol as a string

i.e: “ipv4 { import all; export all;}”

from_conf: bool

Internal usage, True/False if that protocol was given by the self.cfg. False by deafult

Raises
  • Exception - in case trying to add an existing protocol or static one with BirdCFGCreator name.

remove_protocol(protocol, name)[source]

Remove the protocol from our future cfg.

Parameters
protocol: string
The protocol to be removed

i.e: bgp, rip. Not to be confused with the name as it in bird i.e bgp1, my_rip..

name: string
Protocol name (as it in bird) to be removed

i.e: bgp1, my_rip. Not to be confused with the protocol itself like bgp, rip..

Raises
  • Exception - in case protocol was not added before or it is part of the original bird.cfg.

add_route(dst_cidr, next_hop)[source]
Adding simple route to our future cfg. Simple route is any route where after “via” there are no brackets and there is only 1 term

i.e: route 1.1.1.0/24 via “eth0”

Parameters
dst_cidr: string
Destination ip and subnetmask in cidr notation

i.e: 1.1.1.0/24

next_hop: string

Next hop to get the dst_cidr.

add_extended_route(dst_cidr, next_hop)[source]
Adding more complex route to our future cfg. Extended route is any route where after “via” there are more than 1 term

i.e: route 10.1.1.0/24 via 198.51.100.3 { rip_metric = 3; };

Parameters
dst_cidr: string

Destination ip and subnetmask in cidr notation i.e: 1.1.1.0/24

next_hop: string
Next hop to get the dst_cidr. In extended route next_hop is more informative

i.e: 198.51.100.3 { rip_metric = 3; };

add_many_routes(start_ip, total_routes, next_hop, jump=1)[source]

Adding many simple routes to our future cfg. The function iterates from “start_ip” incrementing by “jump” with “total_routes”

Parameters
start_ip: string

First ip to start to start counting from i.e: 1.1.1.2

total_routes: string

Total number of routes to add

next_hop: string

The next hop that will be in each route

jump: string

The amount of ip addresses to jump from each route

remove_route(dst_cidr, next_hop=None)[source]

Remove route from our future cfg by his dst_cidr and next_hop. If next_hop was not provided it will remove every route with dst_cidr.

Parameters
dst_cidr: string
Destination ip and subnetmask in cidr notation

i.e: 1.1.1.0/24

next_hop: string

Next hop to get the dst_cidr. This is an optional argument, in case it was not provided it will remove every route with dst_cidr.

build_config()[source]

Create our final bird.conf content. Merge the given bird.cfg from constructor with the routes & protocols the user added.

add_simple_rip()[source]

Add simple rip config

add_simple_bgp()[source]

Add simple bgp config

add_simple_ospf()[source]

Add simple ospf config