Skip to content

Instantly share code, notes, and snippets.

@svinota
Last active August 29, 2015 14:12
Show Gist options
  • Save svinota/776d7add72a224e12111 to your computer and use it in GitHub Desktop.
Save svinota/776d7add72a224e12111 to your computer and use it in GitHub Desktop.
pyroute2 IPDB + network namespaces
$ sudo python
Python 2.7.5 (default, Nov 3 2014, 14:26:24)
[GCC 4.8.3 20140911 (Red Hat 4.8.3-7)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from pyroute2 import IPDB
>>> from pyroute2 import NetNS
>>> ip1 = IPDB()
>>> ip2 = IPDB(nl=NetNS('test'))
>>> ip1.by_name.keys()
['bond0', 'lo', 'vnet0', 'em1', 'wlo1', 'dummy0', 'v13p0', 'virbr0-nic', 'virbr0']
>>> ip2.by_name.keys()
['lo', 'v13p1']
>>> with ip1.create(ifname='v14p0', kind='veth', peer='v14p1') as veth:
... veth.add_ip('172.16.200.1/24')
...
{'index': 0, 'kind': 'veth', 'ipaddr': [], 'peer': 'v14p1', 'ifname': 'v14p0', 'ports': []}
>>> with ip1.interfaces.v14p1 as veth:
... veth.net_ns_fd = 'test'
...
{'neighbors': [], 'family': 0, 'txqlen': 1000, 'index': 8587, 'operstate': 'UP', 'group': 0, 'carrier_changes': 2, 'ipaddr': [('fe80::7842:e6ff:fe33:aa74', 64)], 'ifname': 'v14p1', 'promiscuity': 0, 'linkmode': 0, 'broadcast': 'ff:ff:ff:ff:ff:ff', 'address': '7a:42:e6:33:aa:74', 'num_tx_queues': 1, 'change': 0, 'kind': 'veth', 'qdisc': 'pfifo_fast', '__align': 0, 'mtu': 1500, 'num_rx_queues': 1, 'carrier': 1, 'flags': 69699, 'ifi_type': 1, 'ports': []}
>>> with ip2.interfaces.v14p1 as veth:
... veth.add_ip('172.16.200.2/24')
...
{'neighbors': [], 'family': 0, 'txqlen': 1000, 'index': 8587, 'operstate': 'DOWN', 'group': 0, 'carrier_changes': 3, 'ipaddr': [], 'ifname': 'v14p1', 'promiscuity': 0, 'linkmode': 0, 'broadcast': 'ff:ff:ff:ff:ff:ff', 'address': '7a:42:e6:33:aa:74', 'num_tx_queues': 1, 'change': 4294967295, 'kind': 'veth', 'qdisc': 'noop', '__align': 0, 'mtu': 1500, 'num_rx_queues': 1, 'carrier': 0, 'flags': 4098, 'ifi_type': 1, 'ports': []}
>>> ip1.by_name.keys()
['v14p0', 'bond0', 'lo', 'vnet0', 'em1', 'wlo1', 'dummy0', 'v13p0', 'virbr0-nic', 'virbr0']
>>> ip2.by_name.keys()
['lo', 'v13p1', 'v14p1']
>>>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment