本文整理汇总了Python中pyroute2.IPRoute.get_vlans方法的典型用法代码示例。如果您正苦于以下问题:Python IPRoute.get_vlans方法的具体用法?Python IPRoute.get_vlans怎么用?Python IPRoute.get_vlans使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pyroute2.IPRoute
的用法示例。
在下文中一共展示了IPRoute.get_vlans方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: TestIPRoute
# 需要导入模块: from pyroute2 import IPRoute [as 别名]
# 或者: from pyroute2.IPRoute import get_vlans [as 别名]
class TestIPRoute(object):
ipnets = []
ipranges = []
ifnames = []
def setup(self):
self.ip = IPRoute()
self.ipnets = [allocate_network() for _ in range(3)]
self.ipranges = [[str(x) for x in net] for net in self.ipnets]
self.ifaces = []
self.ifnames = []
try:
self.dev, idx = self.create()
except IndexError:
pass
def create(self, kind='dummy'):
require_user('root')
name = uifname()
self.ip.link('add', ifname=name, kind=kind)
idx = None
while not idx:
idx = self.ip.link_lookup(ifname=name)
idx = idx[0]
self.ifaces.append(idx)
return (name, idx)
def uifname(self):
ifname = uifname()
self.ifnames.append(ifname)
return ifname
def teardown(self):
for net in self.ipnets:
free_network(net)
if hasattr(self, 'ifaces'):
for dev in reversed(self.ifaces):
try:
self.ip.link('delete', index=dev)
except:
pass
for name in reversed(self.ifnames):
try:
(self
.ip
.link('del', index=(self
.ip
.link_lookup(ifname=name)[0])))
except:
pass
self.ip.close()
def ifaddr(self, r=0):
return str(self.ipranges[r].pop())
def _test_nla_operators(self):
require_user('root')
self.ip.addr('add', self.ifaces[0], address='172.16.0.1', mask=24)
self.ip.addr('add', self.ifaces[0], address='172.16.0.2', mask=24)
r = [x for x in self.ip.get_addr() if x['index'] == self.ifaces[0]]
complement = r[0] - r[1]
intersection = r[0] & r[1]
assert complement.get_attr('IFA_ADDRESS') == '172.16.0.1'
assert complement.get_attr('IFA_LABEL') is None
assert complement['prefixlen'] == 0
assert complement['index'] == 0
assert intersection.get_attr('IFA_ADDRESS') is None
assert intersection.get_attr('IFA_LABEL') == self.dev
assert intersection['prefixlen'] == 24
assert intersection['index'] == self.ifaces[0]
def test_addr_add(self):
require_user('root')
ifaddr = self.ifaddr()
self.ip.addr('add', self.ifaces[0], address=ifaddr, mask=24)
assert '{0}/24'.format(ifaddr) in get_ip_addr()
def test_vlan_filter_dump(self):
require_user('root')
(an, ax) = self.create('bridge')
(bn, bx) = self.create('bridge')
self.ip.link('set', index=ax, state='up')
self.ip.link('set', index=bx, state='up')
assert len(self.ip.get_vlans()) >= 2
for name in (an, bn):
assert len(self.ip.get_vlans(ifname=name)) == 1
assert (self
.ip
.get_vlans(ifname=name)[0]
.get_attr('IFLA_IFNAME')) == name
assert (self
.ip
.get_vlans(ifname=name)[0]
.get_nested('IFLA_AF_SPEC',
'IFLA_BRIDGE_VLAN_INFO'))['vid'] == 1
def test_vlan_filter_add(self):
#.........这里部分代码省略.........
示例2: TestIPRoute
# 需要导入模块: from pyroute2 import IPRoute [as 别名]
# 或者: from pyroute2.IPRoute import get_vlans [as 别名]
class TestIPRoute(object):
def setup(self):
self.ip = IPRoute()
try:
self.ifaces = []
self.dev, idx = self.create()
except IndexError:
pass
def create(self, kind='dummy'):
name = uifname()
create_link(name, kind=kind)
idx = self.ip.link_lookup(ifname=name)[0]
self.ifaces.append(idx)
return (name, idx)
def teardown(self):
if hasattr(self, 'ifaces'):
for dev in self.ifaces:
try:
self.ip.link('delete', index=dev)
except:
pass
self.ip.close()
def _test_nla_operators(self):
require_user('root')
self.ip.addr('add', self.ifaces[0], address='172.16.0.1', mask=24)
self.ip.addr('add', self.ifaces[0], address='172.16.0.2', mask=24)
r = [x for x in self.ip.get_addr() if x['index'] == self.ifaces[0]]
complement = r[0] - r[1]
intersection = r[0] & r[1]
assert complement.get_attr('IFA_ADDRESS') == '172.16.0.1'
assert complement.get_attr('IFA_LABEL') is None
assert complement['prefixlen'] == 0
assert complement['index'] == 0
assert intersection.get_attr('IFA_ADDRESS') is None
assert intersection.get_attr('IFA_LABEL') == self.dev
assert intersection['prefixlen'] == 24
assert intersection['index'] == self.ifaces[0]
def test_addr_add(self):
require_user('root')
self.ip.addr('add', self.ifaces[0], address='172.16.0.1', mask=24)
assert '172.16.0.1/24' in get_ip_addr()
def test_vlan_filter_dump(self):
require_user('root')
(an, ax) = self.create('bridge')
(bn, bx) = self.create('bridge')
self.ip.link('set', index=ax, state='up')
self.ip.link('set', index=bx, state='up')
assert len(self.ip.get_vlans()) >= 2
for name in (an, bn):
assert len(self.ip.get_vlans(ifname=name)) == 1
assert (self
.ip
.get_vlans(ifname=name)[0]
.get_attr('IFLA_IFNAME')) == name
assert (self
.ip
.get_vlans(ifname=name)[0]
.get_nested('IFLA_AF_SPEC',
'IFLA_BRIDGE_VLAN_INFO'))['vid'] == 1
def test_vlan_filter_add(self):
require_user('root')
(bn, bx) = self.create('bridge')
(sn, sx) = self.create('dummy')
self.ip.link('set', index=sx, master=bx)
assert not grep('bridge vlan show', pattern='567')
self.ip.vlan_filter('add', index=sx, vlan_info={'vid': 567})
assert grep('bridge vlan show', pattern='567')
self.ip.vlan_filter('del', index=sx, vlan_info={'vid': 567})
assert not grep('bridge vlan show', pattern='567')
def test_vlan_filter_add_raw(self):
require_user('root')
(bn, bx) = self.create('bridge')
(sn, sx) = self.create('dummy')
self.ip.link('set', index=sx, master=bx)
assert not grep('bridge vlan show', pattern='567')
self.ip.vlan_filter('add', index=sx,
af_spec={'attrs': [['IFLA_BRIDGE_VLAN_INFO',
{'vid': 567}]]})
assert grep('bridge vlan show', pattern='567')
self.ip.vlan_filter('del', index=sx,
af_spec={'attrs': [['IFLA_BRIDGE_VLAN_INFO',
{'vid': 567}]]})
assert not grep('bridge vlan show', pattern='567')
def test_local_add(self):
require_user('root')
self.ip.addr('add', self.ifaces[0],
address='172.16.0.2',
local='172.16.0.1',
mask=24)
#.........这里部分代码省略.........