Skip to content

Instantly share code, notes, and snippets.

@svinota
Created April 22, 2016 04:46
Show Gist options
  • Save svinota/6b0e5bebf50c67bd404d811cc3aa2535 to your computer and use it in GitHub Desktop.
Save svinota/6b0e5bebf50c67bd404d811cc3aa2535 to your computer and use it in GitHub Desktop.
create a bridge, create 20 veth interfaces, put veth peers to namespaces (and rename as eth0), add veth interfaces as ports to the bridge
from pyroute2 import IPDB
from pyroute2 import netns
with IPDB() as ip:
ip.create(ifname='v0', kind='bridge').commit()
map(lambda x: ip.create(ifname='v0p%02i' % x,
peer='p0p%02i' % x,
kind='veth').commit(), range(20))
map(lambda x: netns.create('host-%02i' % x), range(20))
map(lambda x: ip.interfaces['p0p%02i' % x]
.set('net_ns_fd', 'host-%02i' % x)
.set('ifname', 'eth0')
.commit(), range(20))
reduce(lambda x, y: x.add_port(y),
map(lambda x: ip.interfaces[x],
sorted(filter(lambda x: x.startswith('v0'),
ip.by_name.keys())))).commit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment