本文整理汇总了Python中pyroute2.IPRoute.flush_routes方法的典型用法代码示例。如果您正苦于以下问题:Python IPRoute.flush_routes方法的具体用法?Python IPRoute.flush_routes怎么用?Python IPRoute.flush_routes使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pyroute2.IPRoute
的用法示例。
在下文中一共展示了IPRoute.flush_routes方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __createNetns
# 需要导入模块: from pyroute2 import IPRoute [as 别名]
# 或者: from pyroute2.IPRoute import flush_routes [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_routes [as 别名]
#.........这里部分代码省略.........
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')
def test_route_table_2048(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',
示例3: TestIPRoute
# 需要导入模块: from pyroute2 import IPRoute [as 别名]
# 或者: from pyroute2.IPRoute import flush_routes [as 别名]
#.........这里部分代码省略.........
assert route.get_attr('RTA_ENCAP_TYPE') == 1
assert route.get_attr('RTA_OIF') == 1
labels = route.get_attr('RTA_ENCAP').get_attr('MPLS_IPTUNNEL_DST')
assert len(labels) == 1
assert labels[0]['bos'] == 1
assert labels[0]['label'] == 245
self.ip.route('del', dst='%s/24' % naddr)
def test_route_change_existing(self):
# route('replace', ...) should succeed, if route exists
require_user('root')
naddr = str(self.ipnets[1].network)
ifaddr1 = self.ifaddr()
ifaddr2 = self.ifaddr()
ifaddr3 = self.ifaddr()
self.ip.link('set', index=self.ifaces[0], state='up')
self.ip.addr('add', self.ifaces[0], address=ifaddr1, mask=24)
self.ip.route('add',
dst=naddr,
mask=24,
gateway=ifaddr2,
table=100)
assert grep('ip route show table 100',
pattern='%s/24.*%s' % (naddr, ifaddr2))
self.ip.route('change',
dst=naddr,
mask=24,
gateway=ifaddr3,
table=100)
assert not grep('ip route show table 100',
pattern='%s/24.*%s' % (naddr, ifaddr2))
assert grep('ip route show table 100',
pattern='%s/24.*%s' % (naddr, ifaddr3))
self.ip.flush_routes(table=100)
assert not grep('ip route show table 100',
pattern='%s/24.*%s' % (naddr, ifaddr3))
def test_route_change_not_existing_fail(self):
# route('change', ...) should fail, if no route exists
require_user('root')
naddr = str(self.ipnets[1].network)
ifaddr1 = self.ifaddr()
ifaddr2 = self.ifaddr()
self.ip.link('set', index=self.ifaces[0], state='up')
self.ip.addr('add', self.ifaces[0], address=ifaddr1, mask=24)
assert not grep('ip route show table 100',
pattern='%s.*%s' % (naddr, ifaddr2))
try:
self.ip.route('change',
dst=naddr,
mask=24,
gateway=ifaddr2,
table=100)
except NetlinkError as e:
if e.code != errno.ENOENT:
raise
def test_route_replace_existing(self):
# route('replace', ...) should succeed, if route exists
require_user('root')
naddr = str(self.ipnets[1].network)
ifaddr1 = self.ifaddr()
ifaddr2 = self.ifaddr()
ifaddr3 = self.ifaddr()
self.ip.link('set', index=self.ifaces[0], state='up')
self.ip.addr('add', self.ifaces[0], address=ifaddr1, mask=24)
示例4: TestIPRoute
# 需要导入模块: from pyroute2 import IPRoute [as 别名]
# 或者: from pyroute2.IPRoute import flush_routes [as 别名]
#.........这里部分代码省略.........
'multipath': [{'hops': 20,
'ifindex': 1,
'gateway': '127.0.0.2'},
{'hops': 30,
'ifindex': 1,
'gateway': '127.0.0.3'}]})
self.ip.route('add', **req)
assert grep('ip route show', pattern='172.16.242.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.242.0', mask=24)
def test_route_change_existing(self):
# route('replace', ...) should succeed, if route exists
require_user('root')
self.ip.link('set', index=self.ifaces[0], state='up')
self.ip.addr('add', self.ifaces[0], address='172.16.0.50', mask=24)
self.ip.route('add',
dst='172.16.1.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')
self.ip.route('change',
dst='172.16.1.0',
mask=24,
gateway='172.16.0.2',
table=100)
assert not 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.1.0/24.*172.16.0.2')
self.ip.flush_routes(table=100)
assert not grep('ip route show table 100',
pattern='172.16.1.0/24.*172.16.0.2')
def test_route_change_not_existing_fail(self):
# route('change', ...) should fail, if no route exists
require_user('root')
self.ip.link('set', index=self.ifaces[0], state='up')
self.ip.addr('add', self.ifaces[0], address='172.16.0.50', mask=24)
assert not grep('ip route show table 100',
pattern='172.16.1.0/24.*172.16.0.1')
try:
self.ip.route('change',
dst='172.16.1.0',
mask=24,
gateway='172.16.0.1',
table=100)
except NetlinkError as e:
if e.code != errno.ENOENT:
raise
def test_route_replace_existing(self):
# route('replace', ...) should succeed, if route exists
require_user('root')
self.ip.link('set', index=self.ifaces[0], state='up')
self.ip.addr('add', self.ifaces[0], address='172.16.0.50', mask=24)
self.ip.route('replace',
dst='172.16.1.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')
示例5: TestIPRoute
# 需要导入模块: from pyroute2 import IPRoute [as 别名]
# 或者: from pyroute2.IPRoute import flush_routes [as 别名]
#.........这里部分代码省略.........
oif=1)
routes = self.ip.route('dump', dst='172.16.245.0/24')
assert len(routes) == 1
route = routes[0]
assert route.get_attr('RTA_ENCAP_TYPE') == 1
assert route.get_attr('RTA_OIF') == 1
labels = route.get_attr('RTA_ENCAP').get_attr('MPLS_IPTUNNEL_DST')
assert len(labels) == 1
assert labels[0]['bos'] == 1
assert labels[0]['label'] == 245
self.ip.route('del', dst='172.16.245.0/24')
def test_route_change_existing(self):
# route('replace', ...) should succeed, if route exists
require_user('root')
self.ip.link('set', index=self.ifaces[0], state='up')
self.ip.addr('add', self.ifaces[0], address='172.16.0.50', mask=24)
self.ip.route('add',
dst='172.16.1.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')
self.ip.route('change',
dst='172.16.1.0',
mask=24,
gateway='172.16.0.2',
table=100)
assert not 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.1.0/24.*172.16.0.2')
self.ip.flush_routes(table=100)
assert not grep('ip route show table 100',
pattern='172.16.1.0/24.*172.16.0.2')
def test_route_change_not_existing_fail(self):
# route('change', ...) should fail, if no route exists
require_user('root')
self.ip.link('set', index=self.ifaces[0], state='up')
self.ip.addr('add', self.ifaces[0], address='172.16.0.50', mask=24)
assert not grep('ip route show table 100',
pattern='172.16.1.0/24.*172.16.0.1')
try:
self.ip.route('change',
dst='172.16.1.0',
mask=24,
gateway='172.16.0.1',
table=100)
except NetlinkError as e:
if e.code != errno.ENOENT:
raise
def test_route_replace_existing(self):
# route('replace', ...) should succeed, if route exists
require_user('root')
self.ip.link('set', index=self.ifaces[0], state='up')
self.ip.addr('add', self.ifaces[0], address='172.16.0.50', mask=24)
self.ip.route('replace',
dst='172.16.1.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')
示例6: main
# 需要导入模块: from pyroute2 import IPRoute [as 别名]
# 或者: from pyroute2.IPRoute import flush_routes [as 别名]
def main(argv):
if (len(argv) != 2):
print('Usage: python pvdtest_veth.py PVD_ID')
print('PVD_ID := name of the interface that contains PvD-related network configuration')
sys.exit()
# Get user-entered PVD ID
pvdId = sys.argv[1]
# Create IPRoute object for manipulation with a default network namespace
ipMain = IPRoute()
# Create a PvD-related network namespace
pvdNetnsName = getPvdNetnsName(pvdId);
if (pvdNetnsName in netns.listnetns()):
netns.remove(pvdNetnsName)
netns.create(pvdNetnsName)
# Create IPRoute object for manipulation with a PvD-related network namespace
netns.setns(pvdNetnsName)
ipPvd = IPRoute()
# Activate loopback interface in a PvD-related network namespace
loIndex = ipPvd.link_lookup(ifname='lo')[0]
ipPvd.link_up(loIndex)
# Get addresses from a PvD-related network interface
pvdIfIndex = ipMain.link_lookup(ifname=getPvdIfName(pvdId))[0]
pvdAddresses = ipMain.get_addr(index=pvdIfIndex)
# Get current routes
pvdRoutes = ipMain.get_routes()
# Create bridge
bridge = getPvdBridgeName(pvdId)
ipMain.link_create(ifname=bridge, kind='bridge')
# Create veth interface
(veth0, veth1) = getPvdVethNames(pvdId)
ipMain.link_create(ifname=veth0, kind='veth', peer=veth1)
# Move one end of the veth interafce to a PvD-related network namespace
veth1Index = ipMain.link_lookup(ifname=veth1)[0]
ipMain.link('set', index=veth1Index, net_ns_fd=pvdNetnsName)
# Shut down and remove addresses from the PvD interface before adding it to the bridge
ipMain.link_down(pvdIfIndex)
ipMain.flush_addr(pvdIfIndex)
# Make a bridge between PvD interface and one end of veth interface
veth0Index = ipMain.link_lookup(ifname=veth0)[0]
bridgeIndex = ipMain.link_lookup(ifname=bridge)[0]
ipMain.link('set', index=veth0Index, master=bridgeIndex)
ipMain.link('set', index=pvdIfIndex, master=bridgeIndex)
# Activate bridge and connected interfaces
ipMain.link_up(pvdIfIndex)
ipMain.link_up(veth0Index)
ipMain.link_up(bridgeIndex)
ipPvd.link_up(veth1Index)
# Configure bridge and another end of the veth interface with PvD-related network parameters + add routes
ipMain.flush_routes()
ipPvd.flush_routes()
for address in pvdAddresses:
ipAddress = broadcastAddress = netmask = addrFamily = None
for attr in address['attrs']:
if attr[0] == 'IFA_ADDRESS':
ipAddress = attr[1]
if attr[0] == 'IFA_BROADCAST':
broadcastAddress = attr[1]
netmask = address['prefixlen']
addrFamily = address['family']
# Configure bridge
try:
ipMain.addr('add', index=bridgeIndex, family=addrFamily, address=ipAddress, broadcast=broadcastAddress, mask=netmask)
except:
pass
# Configure veth
try:
ipPvd.addr('add', index=veth1Index, family=addrFamily, address=ipAddress, broadcast=broadcastAddress, mask=netmask)
except:
pass
# Configure routes
# Some routes are added during interface IP address/netmask configuration, skip them if already there
ipNetwork = IPNetwork(ipAddress + '/' + str(netmask))
try:
ipMain.route('add', dst=str(ipNetwork.network), mask=netmask, oif=bridgeIndex, src=ipAddress, rtproto='RTPROT_STATIC', rtscope='RT_SCOPE_LINK')
except:
pass
try:
ipPvd.route('add', dst=str(ipNetwork.network), mask=netmask, oif=veth1Index, src=ipAddress, rtproto='RTPROT_STATIC', rtscope='RT_SCOPE_LINK')
except:
pass
# Fing gateway(s) and add default routes
defGateways = []
for route in pvdRoutes:
for attr in route['attrs']:
if (attr[0] == 'RTA_GATEWAY' and attr[1] not in defGateways):
#.........这里部分代码省略.........