本文整理汇总了Python中pyroute2.IPRoute.flush_rules方法的典型用法代码示例。如果您正苦于以下问题:Python IPRoute.flush_rules方法的具体用法?Python IPRoute.flush_rules怎么用?Python IPRoute.flush_rules使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pyroute2.IPRoute
的用法示例。
在下文中一共展示了IPRoute.flush_rules方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __createNetns
# 需要导入模块: from pyroute2 import IPRoute [as 别名]
# 或者: from pyroute2.IPRoute import flush_rules [as 别名]
def __createNetns(self, phyIfaceIndex):
netnsName = self.__getNetnsName()
(pvdIfaceName, pvdIfaceIndex) = self.__getPvdIfaceParams()
netns.create(netnsName)
LOG.debug('network namespace {0} created'.format(netnsName))
# create a virtual interface where PvD parameters are going to be configured, then move the interface to the new network namespace
self.ipRoot.link_create(ifname=pvdIfaceName, index=pvdIfaceIndex, kind=self.__PVD_IFACE_TYPE, link=phyIfaceIndex)
LOG.debug('macvlan {0} created in default network namespace'.format(pvdIfaceName))
pvdIfaceIndex = self.ipRoot.link_lookup(ifname=pvdIfaceName)
self.ipRoot.link('set', index=pvdIfaceIndex[0], net_ns_fd=netnsName)
LOG.debug('macvlan {0} moved to network namespace {1}'.format(pvdIfaceName, netnsName))
# change the namespace and get new NETLINK handles to operate in new namespace
netns.setns(netnsName)
LOG.debug('network namespace switched to {0}'.format(netnsName))
ip = IPRoute()
ipdb = IPDB()
ipdb.register_callback(self.__onIfaceStateChange)
# disable kernel to auto-configure the interface associated with the PvD, let the pvdman to solely control interface configuration
acceptRaConfFile = self.__ACCEPT_RA_CONF_FILE.replace(self.__IFACENAME_REPLACE_PATTERN, pvdIfaceName)
acceptRaConfFile = open(acceptRaConfFile, 'w')
acceptRaConfFile.write('0')
LOG.debug('processing of RAs by kernel disabled in {0}'.format(acceptRaConfFile.name))
# return to a default network namespace to not cause a colision with other modules
# ip and ipdb handles continue to work in the target network namespace
netns.setns(self.__NETNS_DEFAULT_NAME)
LOG.debug('network namespace switched to default')
# get new index since interface has been moved to a different namespace
loIfaceIndex = ip.link_lookup(ifname=self.__LOOPBACK_IFACE_NAME)
if (len(loIfaceIndex) > 0):
loIfaceIndex = loIfaceIndex[0]
pvdIfaceIndex = ip.link_lookup(ifname=pvdIfaceName)
if (len(pvdIfaceIndex) > 0):
pvdIfaceIndex = pvdIfaceIndex[0]
# start interfaces
ip.link_up(loIfaceIndex)
ip.link_up(pvdIfaceIndex)
# clear network configuration if exists
ip.flush_addr(index=pvdIfaceIndex)
ip.flush_routes(index=pvdIfaceIndex)
ip.flush_rules(index=pvdIfaceIndex)
LOG.debug('macvlan {0} in network namespace {1} initialized'.format(pvdIfaceName, netnsName))
return (netnsName, pvdIfaceName, ip)
示例2: TestIPRoute
# 需要导入模块: from pyroute2 import IPRoute [as 别名]
# 或者: from pyroute2.IPRoute import flush_rules [as 别名]
#.........这里部分代码省略.........
ifaddr2 = self.ifaddr()
self.ip.addr('add', self.ifaces[0],
address=ifaddr1,
mask=24,
broadcast=ifaddr2)
assert ifaddr2 in get_ip_brd()
def test_addr_broadcast_default(self):
require_user('root')
ifaddr1 = self.ifaddr() # -> 255
ifaddr2 = self.ifaddr() # -> 254
self.ip.addr('add', self.ifaces[0],
address=ifaddr2,
mask=24,
broadcast=True)
assert ifaddr1 in get_ip_brd()
def test_flush_addr(self):
require_user('root')
ifaddr1 = self.ifaddr(0)
ifaddr2 = self.ifaddr(0)
ifaddr3 = self.ifaddr(1)
ifaddr4 = self.ifaddr(1)
self.ip.addr('add', self.ifaces[0], address=ifaddr1, mask=24)
self.ip.addr('add', self.ifaces[0], address=ifaddr2, mask=24)
self.ip.addr('add', self.ifaces[0], address=ifaddr3, mask=24)
self.ip.addr('add', self.ifaces[0], address=ifaddr4, mask=24)
assert len(self.ip.get_addr(index=self.ifaces[0],
family=socket.AF_INET)) == 4
self.ip.flush_addr(index=self.ifaces[0])
assert len(self.ip.get_addr(index=self.ifaces[0],
family=socket.AF_INET)) == 0
def test_flush_rules(self):
require_user('root')
ifaddr1 = self.ifaddr(0)
ifaddr2 = self.ifaddr(1)
init = len(self.ip.get_rules(family=socket.AF_INET))
assert len(self.ip.get_rules(priority=lambda x: 100 < x < 500)) == 0
self.ip.rule('add', table=10, priority=110)
self.ip.rule('add', table=15, priority=150, action='FR_ACT_PROHIBIT')
self.ip.rule('add', table=20, priority=200, src=ifaddr1)
self.ip.rule('add', table=25, priority=250, dst=ifaddr2)
assert len(self.ip.get_rules(priority=lambda x: 100 < x < 500)) == 4
assert len(self.ip.get_rules(src=ifaddr1)) == 1
assert len(self.ip.get_rules(dst=ifaddr2)) == 1
self.ip.flush_rules(family=socket.AF_INET,
priority=lambda x: 100 < x < 500)
assert len(self.ip.get_rules(priority=lambda x: 100 < x < 500)) == 0
assert len(self.ip.get_rules(src=ifaddr1)) == 0
assert len(self.ip.get_rules(dst=ifaddr2)) == 0
assert len(self.ip.get_rules(family=socket.AF_INET)) == init
def test_rules_deprecated(self):
require_user('root')
init = len(self.ip.get_rules(family=socket.AF_INET))
assert len(self.ip.get_rules(priority=lambda x: 100 < x < 500)) == 0
self.ip.rule('add', 10, 110)
self.ip.rule('add', 15, 150, 'FR_ACT_PROHIBIT')
assert len(self.ip.get_rules(priority=lambda x: 100 < x < 500)) == 2
self.ip.flush_rules(family=socket.AF_INET,
priority=lambda x: 100 < x < 500)
assert len(self.ip.get_rules(priority=lambda x: 100 < x < 500)) == 0
assert len(self.ip.get_rules(family=socket.AF_INET)) == init
def test_match_callable(self):
示例3: TestIPRoute
# 需要导入模块: from pyroute2 import IPRoute [as 别名]
# 或者: from pyroute2.IPRoute import flush_rules [as 别名]
class TestIPRoute(object):
def setup(self):
self.ip = IPRoute()
try:
self.dev, idx = self.create()
self.ifaces = [idx]
except IndexError:
pass
def create(self, kind='dummy'):
name = uifname()
create_link(name, kind=kind)
idx = self.ip.link_lookup(ifname=name)[0]
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_flush_addr(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)
self.ip.addr('add', self.ifaces[0], address='172.16.1.1', mask=24)
self.ip.addr('add', self.ifaces[0], address='172.16.1.2', mask=24)
assert len(self.ip.get_addr(index=self.ifaces[0],
family=socket.AF_INET)) == 4
self.ip.flush_addr(index=self.ifaces[0])
assert len(self.ip.get_addr(index=self.ifaces[0],
family=socket.AF_INET)) == 0
def test_flush_rules(self):
require_user('root')
init = len(self.ip.get_rules(family=socket.AF_INET))
assert len(self.ip.get_rules(priority=lambda x: 100 < x < 500)) == 0
self.ip.rule('add', table=10, priority=110)
self.ip.rule('add', table=15, priority=150, action='FR_ACT_PROHIBIT')
self.ip.rule('add', table=20, priority=200, src='172.16.200.1')
self.ip.rule('add', table=25, priority=250, dst='172.16.250.1')
assert len(self.ip.get_rules(priority=lambda x: 100 < x < 500)) == 4
assert len(self.ip.get_rules(src='172.16.200.1')) == 1
assert len(self.ip.get_rules(dst='172.16.250.1')) == 1
self.ip.flush_rules(family=socket.AF_INET,
priority=lambda x: 100 < x < 500)
assert len(self.ip.get_rules(priority=lambda x: 100 < x < 500)) == 0
assert len(self.ip.get_rules(src='172.16.200.1')) == 0
assert len(self.ip.get_rules(dst='172.16.250.1')) == 0
assert len(self.ip.get_rules(family=socket.AF_INET)) == init
def test_rules_deprecated(self):
require_user('root')
init = len(self.ip.get_rules(family=socket.AF_INET))
assert len(self.ip.get_rules(priority=lambda x: 100 < x < 500)) == 0
self.ip.rule('add', 10, 110)
self.ip.rule('add', 15, 150, 'FR_ACT_PROHIBIT')
assert len(self.ip.get_rules(priority=lambda x: 100 < x < 500)) == 2
self.ip.flush_rules(family=socket.AF_INET,
priority=lambda x: 100 < x < 500)
assert len(self.ip.get_rules(priority=lambda x: 100 < x < 500)) == 0
assert len(self.ip.get_rules(family=socket.AF_INET)) == init
def test_addr_filter(self):
require_user('root')
self.ip.addr('add',
index=self.ifaces[0],
address='172.16.0.1',
prefixlen=24,
broadcast='172.16.0.255')
self.ip.addr('add',
index=self.ifaces[0],
address='172.16.0.2',
#.........这里部分代码省略.........
示例4: TestIPRoute
# 需要导入模块: from pyroute2 import IPRoute [as 别名]
# 或者: from pyroute2.IPRoute import flush_rules [as 别名]
#.........这里部分代码省略.........
address = link.get_attr('IFA_ADDRESS')
local = link.get_attr('IFA_LOCAL')
assert address == '172.16.0.2'
assert local == '172.16.0.1'
def test_addr_broadcast(self):
require_user('root')
self.ip.addr('add', self.ifaces[0],
address='172.16.0.1',
mask=24,
broadcast='172.16.0.250')
assert '172.16.0.250' in get_ip_brd()
def test_addr_broadcast_default(self):
require_user('root')
self.ip.addr('add', self.ifaces[0],
address='172.16.0.1',
mask=24,
broadcast=True)
assert '172.16.0.255' in get_ip_brd()
def test_flush_addr(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)
self.ip.addr('add', self.ifaces[0], address='172.16.1.1', mask=24)
self.ip.addr('add', self.ifaces[0], address='172.16.1.2', mask=24)
assert len(self.ip.get_addr(index=self.ifaces[0],
family=socket.AF_INET)) == 4
self.ip.flush_addr(index=self.ifaces[0])
assert len(self.ip.get_addr(index=self.ifaces[0],
family=socket.AF_INET)) == 0
def test_flush_rules(self):
require_user('root')
init = len(self.ip.get_rules(family=socket.AF_INET))
assert len(self.ip.get_rules(priority=lambda x: 100 < x < 500)) == 0
self.ip.rule('add', table=10, priority=110)
self.ip.rule('add', table=15, priority=150, action='FR_ACT_PROHIBIT')
self.ip.rule('add', table=20, priority=200, src='172.16.200.1')
self.ip.rule('add', table=25, priority=250, dst='172.16.250.1')
assert len(self.ip.get_rules(priority=lambda x: 100 < x < 500)) == 4
assert len(self.ip.get_rules(src='172.16.200.1')) == 1
assert len(self.ip.get_rules(dst='172.16.250.1')) == 1
self.ip.flush_rules(family=socket.AF_INET,
priority=lambda x: 100 < x < 500)
assert len(self.ip.get_rules(priority=lambda x: 100 < x < 500)) == 0
assert len(self.ip.get_rules(src='172.16.200.1')) == 0
assert len(self.ip.get_rules(dst='172.16.250.1')) == 0
assert len(self.ip.get_rules(family=socket.AF_INET)) == init
def test_rules_deprecated(self):
require_user('root')
init = len(self.ip.get_rules(family=socket.AF_INET))
assert len(self.ip.get_rules(priority=lambda x: 100 < x < 500)) == 0
self.ip.rule('add', 10, 110)
self.ip.rule('add', 15, 150, 'FR_ACT_PROHIBIT')
assert len(self.ip.get_rules(priority=lambda x: 100 < x < 500)) == 2
self.ip.flush_rules(family=socket.AF_INET,
priority=lambda x: 100 < x < 500)
assert len(self.ip.get_rules(priority=lambda x: 100 < x < 500)) == 0
assert len(self.ip.get_rules(family=socket.AF_INET)) == init
def test_addr_filter(self):
require_user('root')
self.ip.addr('add',