本文整理汇总了Python中pyroute2.IPRoute.flush_addr方法的典型用法代码示例。如果您正苦于以下问题:Python IPRoute.flush_addr方法的具体用法?Python IPRoute.flush_addr怎么用?Python IPRoute.flush_addr使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pyroute2.IPRoute
的用法示例。
在下文中一共展示了IPRoute.flush_addr方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __createNetns
# 需要导入模块: from pyroute2 import IPRoute [as 别名]
# 或者: from pyroute2.IPRoute import flush_addr [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_addr [as 别名]
#.........这里部分代码省略.........
require_user('root')
ifaddr1 = self.ifaddr()
ifaddr2 = self.ifaddr()
self.ip.addr('add', self.ifaces[0],
address=ifaddr1,
local=ifaddr2,
mask=24)
link = self.ip.get_addr(index=self.ifaces[0])[0]
address = link.get_attr('IFA_ADDRESS')
local = link.get_attr('IFA_LOCAL')
assert address == ifaddr1
assert local == ifaddr2
def test_addr_broadcast(self):
require_user('root')
ifaddr1 = self.ifaddr()
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
示例3: TestIPRoute
# 需要导入模块: from pyroute2 import IPRoute [as 别名]
# 或者: from pyroute2.IPRoute import flush_addr [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: network_module
# 需要导入模块: from pyroute2 import IPRoute [as 别名]
# 或者: from pyroute2.IPRoute import flush_addr [as 别名]
class network_module(PtyshModule):
IP = "IP"
IP_MASK = "IP/MASK"
STRING = "STRING"
def __init__(self):
super(network_module, self).__init__()
super(network_module, self).init_node("network", "network node")
super(network_module, self).add_command("link up", "set link state up", self.cmd_link_up, "link up [link name]")
super(network_module, self).add_command("link down", "set link state down", self.cmd_link_down, "link down [link name]")
super(network_module, self).add_command("ip add", "add ip address", self.cmd_ip_add, "ip add [link name] [ip] [mask] ( [broadcast] [primary] )")
super(network_module, self).add_command("route add", "add route address", self.cmd_route_add, "route add [default|ip|ip/mask] [ip|none] [interface]")
super(network_module, self).add_command("show link", "show link state", self.cmd_show_link, "show link [link name|none]")
super(network_module, self).add_command("show link all", "show all link statue", self.cmd_show_link, "show link all")
super(network_module, self).add_command("show route", "show route state", self.cmd_show_route, "show route")
self.iproute = IPRoute()
self.boarder_len = 60
def get_link_index(self, link_name):
index = self.iproute.link_lookup(ifname=link_name)
if len(index) != 0:
return index[0]
else:
raise ValueError("There is no interface.")
def check_ip_address(self, ip):
try:
ret = ip_address(ip)
except Exception:
try:
ret = ip_network(ip)
except Exception:
return self.STRING
return self.IP_MASK
return self.IP
def change_link_state(self, state):
index = self.get_link_index(args[0])
ret = self.iproute.link("set", index=index, state=state)
if ret[0]["header"]["error"] is not None:
raise Exception(ret[0]["header"]["error"])
def print_output_boarder(self, inner):
print ("=" * self.boarder_len) if inner else print ("-" * self.boarder_len)
def print_output_subject(self, subject):
subject_len = len(subject)
harf_boarder_len = int((self.boarder_len - (subject_len + 2)) / 2)
harf_boarder = "-" * harf_boarder_len
print ("%s %s %s" % (harf_boarder, subject, harf_boarder))
def print_key_value(self, key, value):
print ("%s : %s" % (key.ljust(25), value))
def print_ip_brief(self, idx, interface, state, ip):
print ("%s%s%s%s" % (idx.ljust(8), interface.ljust(15), state.ljust(10), ip.ljust(27)))
def print_route_table(self, index, dest, gateway, mask, interface):
print ("%s%s%s%s%s" % (index.ljust(8), dest.ljust(17), gateway.ljust(17), mask.ljust(8), interface))
##### cmd function. #####
def cmd_link_up(self, args):
self.change_link_state("up")
def cmd_link_down(self, args):
self.change_link_state("down")
def cmd_ip_add(self, args):
if len(args) < 3:
raise TypeError()
index = self.get_link_index(args[0])
ip = str(args[1])
mask = int(args[2])
broadcast = str(args[3]) if len(args) >= 4 else ""
primary = args[4] if len(args) == 5 else True
if primary:
self.iproute.flush_addr(index=index)
if broadcast:
ret = self.iproute.addr("add", index=index, address=ip, mask=mask, broadcast=broadcast)
else:
ret = self.iproute.addr("add", index=index, address=ip, mask=mask)
if len(ret):
raise Exception(ret)
def cmd_route_add(self, args):
if len(args) < 3:
raise TypeError()
dest = args[0]
#.........这里部分代码省略.........
示例5: TestIPRoute
# 需要导入模块: from pyroute2 import IPRoute [as 别名]
# 或者: from pyroute2.IPRoute import flush_addr [as 别名]
#.........这里部分代码省略.........
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)
link = self.ip.get_addr(index=self.ifaces[0])[0]
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))
示例6: main
# 需要导入模块: from pyroute2 import IPRoute [as 别名]
# 或者: from pyroute2.IPRoute import flush_addr [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):
#.........这里部分代码省略.........