本文整理汇总了Python中pyroute2.IPRoute.get_default_routes方法的典型用法代码示例。如果您正苦于以下问题:Python IPRoute.get_default_routes方法的具体用法?Python IPRoute.get_default_routes怎么用?Python IPRoute.get_default_routes使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pyroute2.IPRoute
的用法示例。
在下文中一共展示了IPRoute.get_default_routes方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: TestIPRoute
# 需要导入模块: from pyroute2 import IPRoute [as 别名]
# 或者: from pyroute2.IPRoute import get_default_routes [as 别名]
#.........这里部分代码省略.........
def test_fail_not_permitted(self):
try:
self.ip.addr('add', 1, address='172.16.0.1', mask=24)
except NetlinkError as e:
if e.code != 1: # Operation not permitted
raise
finally:
try:
self.ip.addr('delete', 1, address='172.16.0.1', mask=24)
except:
pass
def test_fail_no_such_device(self):
require_user('root')
dev = sorted([i['index'] for i in self.ip.get_links()])[-1] + 10
try:
self.ip.addr('add',
dev,
address='172.16.0.1',
mask=24)
except NetlinkError as e:
if e.code != 19: # No such device
raise
def test_remove_link(self):
require_user('root')
try:
self.ip.link_remove(self.ifaces[0])
except NetlinkError:
pass
assert len(self.ip.link_lookup(ifname=self.dev)) == 0
def test_get_route(self):
if not self.ip.get_default_routes(table=254):
return
rts = self.ip.get_routes(family=socket.AF_INET,
dst='8.8.8.8',
table=254)
assert len(rts) > 0
def test_flush_routes(self):
require_user('root')
self.ip.link('set', index=self.ifaces[0], state='up')
self.ip.addr('add', self.ifaces[0], address='172.16.0.2', mask=24)
self.ip.route('add',
prefix='172.16.1.0',
mask=24,
gateway='172.16.0.1',
table=100)
self.ip.route('add',
prefix='172.16.2.0',
mask=24,
gateway='172.16.0.1',
table=100)
assert grep('ip route show table 100',
pattern='172.16.1.0/24.*172.16.0.1')
assert grep('ip route show table 100',
pattern='172.16.2.0/24.*172.16.0.1')
self.ip.flush_routes(table=100)
assert not grep('ip route show table 100',
pattern='172.16.1.0/24.*172.16.0.1')
assert not grep('ip route show table 100',
pattern='172.16.2.0/24.*172.16.0.1')
示例2: TestIPRoute
# 需要导入模块: from pyroute2 import IPRoute [as 别名]
# 或者: from pyroute2.IPRoute import get_default_routes [as 别名]
#.........这里部分代码省略.........
pass
self.ip.route('del',
dst='%s/24' % naddr,
gateway='127.0.0.1',
proto=proto)
assert not grep('ip ro', pattern='%s/24.*127.0.0.1' % naddr)
def test_route_proto_static(self):
return self._test_route_proto('static', 'boot', 'proto static')
def test_route_proto_static_num(self):
return self._test_route_proto(4, 3, 'proto static')
def test_route_proto_boot(self):
return self._test_route_proto('boot', 4)
def test_route_proto_boot_num(self):
return self._test_route_proto(3, 'static')
def test_route_oif_as_iterable(self):
require_user('root')
naddr = str(self.ipnets[1].network)
spec = {'dst': naddr,
'dst_len': 24,
'oif': (1, )}
self.ip.route('add', **spec)
rts = self.ip.get_routes(family=socket.AF_INET,
dst=naddr)
self.ip.route('del', **spec)
assert len(rts) == 1
assert rts[0].get_attr('RTA_OIF') == 1
def test_route_get_target(self):
if not self.ip.get_default_routes(table=254):
raise SkipTest('no default IPv4 routes')
rts = self.ip.get_routes(family=socket.AF_INET,
dst='8.8.8.8',
table=254)
assert len(rts) > 0
def test_route_get_target_default_ipv4(self):
rts = self.ip.get_routes(dst='127.0.0.1')
assert len(rts) > 0
def test_route_get_target_default_ipv6(self):
rts = self.ip.get_routes(dst='::1')
assert len(rts) > 0
def test_route_get_by_spec(self):
require_user('root')
ifaddr1 = self.ifaddr(0)
ifaddr2 = self.ifaddr(1)
self.ip.link('set', index=self.ifaces[0], state='up')
self.ip.addr('add', index=self.ifaces[0],
address=ifaddr1, mask=24)
self.ip.addr('add', index=self.ifaces[0],
address=ifaddr2, mask=24)
rts = self.ip.get_routes(family=socket.AF_INET,
dst=lambda x: x in (ifaddr1,
ifaddr2))
assert len(rts) == 4
@skip_if_not_supported
def _test_route_mpls_via_ipv(self, family, address, label):
require_kernel(4, 4)
require_user('root')
示例3: TestData
# 需要导入模块: from pyroute2 import IPRoute [as 别名]
# 或者: from pyroute2.IPRoute import get_default_routes [as 别名]
class TestData(object):
def setup(self):
create_link('dummyX', 'dummy')
self.ip = IPRoute()
self.dev = self.ip.link_lookup(ifname='dummyX')
def teardown(self):
self.ip.release()
remove_link('dummyX')
remove_link('bala')
def test_nla_operators(self):
require_user('root')
dev = self.dev[0]
self.ip.addr('add', dev, address='172.16.0.1', mask=24)
self.ip.addr('add', dev, address='172.16.0.2', mask=24)
r = [x for x in self.ip.get_addr() if x['index'] == dev]
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') == 'dummyX'
assert intersection['prefixlen'] == 24
assert intersection['index'] == dev
def test_add_addr(self):
require_user('root')
dev = self.dev[0]
self.ip.addr('add', dev, address='172.16.0.1', mask=24)
assert '172.16.0.1/24' in get_ip_addr()
def test_remove_link(self):
require_user('root')
create_link('bala', 'dummy')
dev = self.ip.link_lookup(ifname='bala')[0]
try:
self.ip.link_remove(dev)
except NetlinkError:
pass
assert len(self.ip.link_lookup(ifname='bala')) == 0
def test_get_route(self):
if not self.ip.get_default_routes(table=254):
return
rts = self.ip.get_routes(family=socket.AF_INET,
dst='8.8.8.8',
table=254)
assert len(rts) > 0
def test_route_table_2048(self):
require_user('root')
create_link('bala', 'dummy')
dev = self.ip.link_lookup(ifname='bala')[0]
self.ip.link('set', index=dev, state='up')
self.ip.addr('add', dev, address='172.16.0.2', mask=24)
self.ip.route('add',
prefix='172.16.1.0',
mask=24,
gateway='172.16.0.1',
table=2048)
assert grep('ip route show table 2048',
pattern='172.16.1.0/24.*172.16.0.1')
remove_link('bala')
def test_updown_link(self):
require_user('root')
dev = self.dev[0]
assert not (self.ip.get_links(dev)[0]['flags'] & 1)
try:
self.ip.link_up(dev)
except NetlinkError:
pass
assert self.ip.get_links(dev)[0]['flags'] & 1
try:
self.ip.link_down(dev)
except NetlinkError:
pass
assert not (self.ip.get_links(dev)[0]['flags'] & 1)
def test_callbacks_positive(self):
require_user('root')
dev = self.dev[0]
self.cb_counter = 0
self.ip.register_callback(_callback,
lambda e, x: x.get('index', None) == dev,
(self, ))
self.test_updown_link()
assert self.cb_counter > 0
self.ip.unregister_callback(_callback)
def test_callbacks_negative(self):
require_user('root')
#.........这里部分代码省略.........
示例4: TestIPRoute
# 需要导入模块: from pyroute2 import IPRoute [as 别名]
# 或者: from pyroute2.IPRoute import get_default_routes [as 别名]
#.........这里部分代码省略.........
def test_fail_not_permitted(self):
try:
self.ip.addr('add', 1, address='172.16.0.1', mask=24)
except NetlinkError as e:
if e.code != errno.EPERM: # Operation not permitted
raise
finally:
try:
self.ip.addr('delete', 1, address='172.16.0.1', mask=24)
except:
pass
def test_fail_no_such_device(self):
require_user('root')
dev = sorted([i['index'] for i in self.ip.get_links()])[-1] + 10
try:
self.ip.addr('add',
dev,
address='172.16.0.1',
mask=24)
except NetlinkError as e:
if e.code != errno.ENODEV: # No such device
raise
def test_remove_link(self):
require_user('root')
try:
self.ip.link_remove(self.ifaces[0])
except NetlinkError:
pass
assert len(self.ip.link_lookup(ifname=self.dev)) == 0
def test_route_get_target(self):
if not self.ip.get_default_routes(table=254):
return
rts = self.ip.get_routes(family=socket.AF_INET,
dst='8.8.8.8',
table=254)
assert len(rts) > 0
def test_route_get_by_spec(self):
self.ip.link('set', index=self.ifaces[0], state='up')
self.ip.addr('add', index=self.ifaces[0],
address='172.16.60.1', mask=24)
self.ip.addr('add', index=self.ifaces[0],
address='172.16.61.1', mask=24)
rts = self.ip.get_routes(family=socket.AF_INET,
dst=lambda x: x in ('172.16.60.0',
'172.16.61.0'))
assert len(rts) == 4
def test_route_multipath(self):
require_user('root')
self.ip.route('add',
dst='172.16.241.0',
mask=24,
multipath=[{'hops': 20,
'ifindex': 1,
'attrs': [['RTA_GATEWAY', '127.0.0.2']]},
{'hops': 30,
'ifindex': 1,
'attrs': [['RTA_GATEWAY', '127.0.0.3']]}])
assert grep('ip route show', pattern='172.16.241.0/24')
assert grep('ip route show', pattern='nexthop.*127.0.0.2.*weight 21')
assert grep('ip route show', pattern='nexthop.*127.0.0.3.*weight 31')
self.ip.route('del', dst='172.16.241.0', mask=24)
示例5: TestIPRoute
# 需要导入模块: from pyroute2 import IPRoute [as 别名]
# 或者: from pyroute2.IPRoute import get_default_routes [as 别名]
#.........这里部分代码省略.........
except NetlinkError:
pass
self.ip.route('del',
dst='172.16.3.0/24',
gateway='127.0.0.1',
proto=proto)
assert not grep('ip ro', pattern='172.16.3.0/24.*127.0.0.1')
def test_route_proto_static(self):
return self._test_route_proto('static', 'boot', 'proto static')
def test_route_proto_static_num(self):
return self._test_route_proto(4, 3, 'proto static')
def test_route_proto_boot(self):
return self._test_route_proto('boot', 4)
def test_route_proto_boot_num(self):
return self._test_route_proto(3, 'static')
def test_route_oif_as_iterable(self):
require_user('root')
spec = {'dst': '172.16.0.0',
'dst_len': 24,
'oif': (1, )}
self.ip.route('add', **spec)
rts = self.ip.get_routes(family=socket.AF_INET,
dst='172.16.0.0')
self.ip.route('del', **spec)
assert len(rts) == 1
assert rts[0].get_attr('RTA_OIF') == 1
def test_route_get_target(self):
if not self.ip.get_default_routes(table=254):
raise SkipTest('no default IPv4 routes')
rts = self.ip.get_routes(family=socket.AF_INET,
dst='8.8.8.8',
table=254)
assert len(rts) > 0
def test_route_get_target_default_ipv4(self):
rts = self.ip.get_routes(dst='127.0.0.1')
assert len(rts) > 0
def test_route_get_target_default_ipv6(self):
rts = self.ip.get_routes(dst='::1')
assert len(rts) > 0
def test_route_get_by_spec(self):
require_user('root')
self.ip.link('set', index=self.ifaces[0], state='up')
self.ip.addr('add', index=self.ifaces[0],
address='172.16.60.1', mask=24)
self.ip.addr('add', index=self.ifaces[0],
address='172.16.61.1', mask=24)
rts = self.ip.get_routes(family=socket.AF_INET,
dst=lambda x: x in ('172.16.60.0',
'172.16.61.0'))
assert len(rts) == 4
@skip_if_not_supported
def _test_route_mpls_via_ipv(self, family, address, label):
require_kernel(4, 4)
require_user('root')
self.ip.route('add', **{'family': AF_MPLS,
'oif': self.ifaces[0],