本文整理汇总了Python中pyroute2.IPDB类的典型用法代码示例。如果您正苦于以下问题:Python IPDB类的具体用法?Python IPDB怎么用?Python IPDB使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了IPDB类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_fail_released
def test_fail_released(self):
ip = IPDB()
ip.release()
try:
ip.interfaces.lo.up()
except RuntimeError:
pass
示例2: test_commit_barrier
def test_commit_barrier(self):
require_user("root")
ifname = uifname()
# barrier 0
try:
ip = IPDB()
config.commit_barrier = 0
ts1 = time.time()
ip.create(ifname=ifname, kind="dummy").commit()
ts2 = time.time()
assert 0 < (ts2 - ts1) < 1
except:
raise
finally:
config.commit_barrier = 0.2
ip.interfaces[ifname].remove().commit()
ip.release()
# barrier 5
try:
ip = IPDB()
config.commit_barrier = 5
ts1 = time.time()
ip.create(ifname=ifname, kind="dummy").commit()
ts2 = time.time()
assert 5 < (ts2 - ts1) < 6
except:
raise
finally:
config.commit_barrier = 0.2
ip.interfaces[ifname].remove().commit()
ip.release()
示例3: test_create_gretap
def test_create_gretap(self):
require_user('root')
ifL = self.get_ifname()
ifV = self.get_ifname()
with self.ip.create(kind='dummy', ifname=ifL) as i:
i.add_ip('172.16.0.1/24')
i.up()
self.ip.create(kind='gretap',
ifname=ifV,
gre_local='172.16.0.1',
gre_ikey=1,
gre_okey=2,
gre_iflags=0x0020,
gre_oflags=0x0020,
gre_collect_metadata=True,
gre_ttl=16).commit()
ip2 = IPDB()
ifdb = ip2.interfaces
try:
assert ifdb[ifV].gre_local == '172.16.0.1'
assert ifdb[ifV].gre_ikey == 1
assert ifdb[ifV].gre_okey == 2
assert ifdb[ifV].gre_iflags == 0x0020
assert ifdb[ifV].gre_oflags == 0x0020
if kernel_version_ge(4, 3):
assert ifdb[ifV].gre_collect_metadata
assert ifdb[ifV].gre_ttl == 16
except Exception:
raise
finally:
ip2.release()
示例4: __ovs_setup
def __ovs_setup(self):
if not self.ovs_client.bridgeExists('cygnet0'):
self.ovs_client.addBridge('cygnet0')
self.ovs_client.addPort('cygnet0', self.external_iface)
elif not self.ovs_client.portExists(self.external_iface):
self.ovs_client.addPort('cygnet0', self.external_iface)
ip = IPDB()
ifaces = ip.interfaces
ifaces.cygnet0.begin()
addrs= ip.interfaces[self.external_iface].ipaddr.raw
addr = None
for address, attrs in addrs.items():
if __getIPv4Addr__([address]) == None:
continue
addr = address
ifaces.cygnet0.add_ip(addr[0], int(addr[1]))
ifaces.cygnet0.up()
ifaces.cygnet0.commit()
ifaces[self.external_iface].begin()
ifaces[self.external_iface].down()
ifaces[self.external_iface].commit()
ifaces[self.external_iface].begin()
ifaces[self.external_iface].up()
ifaces[self.external_iface].commit()
ip.release()
示例5: test_create_gre
def test_create_gre(self):
require_user('root')
ifL = self.get_ifname()
ifV = self.get_ifname()
with self.ip.create(kind='dummy', ifname=ifL) as i:
i.add_ip('172.16.0.1/24')
i.up()
self.ip.create(kind='gre',
ifname=ifV,
gre_local='172.16.0.1',
gre_remote='172.16.0.2',
gre_ttl=16).commit()
ip2 = IPDB()
ifdb = ip2.interfaces
try:
assert ifdb[ifV].gre_local == '172.16.0.1'
assert ifdb[ifV].gre_remote == '172.16.0.2'
assert ifdb[ifV].gre_ttl == 16
except Exception:
raise
finally:
ip2.release()
示例6: initContainerNetwork
def initContainerNetwork(self, network=None):
if not network:
try:
network = Network(None)
network.name = 'cygnet_internal'
network.address = self['internal_ip']
if not self.ovs_client.bridgeExists(network.name):
self.ovs_client.addBridge(network.name)
self.ovs_client.setBridgeProperty(network.name,
'stp_enable',
True)
except KeyError as e:
print("OpenvSwitch: CYGNET_INTERNAL_IP \
environment variable not found")
raise e
else:
network.name = "cygnet_" + network.id[:8]
if not self.ovs_client.bridgeExists(network.name):
self.ovs_client.addBridge(network.name)
self.ovs_client.setBridgeProperty(network.name,
'stp_enable',
True)
ip = IPDB()
ifaces = ip.interfaces
ifaces[network.name].begin()
ifaces[network.name].add_ip(network.address, network.mask)
ifaces[network.name].up()
ifaces[network.name].commit()
ip.release()
self.interfaces.append(network)
return network
示例7: test_create_vxlan
def test_create_vxlan(self):
require_user('root')
ifL = self.get_ifname()
ifV = self.get_ifname()
ifdb = self.ip.interfaces
self.ip.create(kind='dummy',
ifname=ifL).commit()
self.ip.create(kind='vxlan',
ifname=ifV,
vxlan_link=ifdb[ifL],
vxlan_id=101,
vxlan_group='239.1.1.1').commit()
ip2 = IPDB()
ifdb = ip2.interfaces
try:
assert ifdb[ifV].vxlan_link == ifdb[ifL].index
assert ifdb[ifV].vxlan_group == '239.1.1.1'
assert ifdb[ifV].vxlan_id == 101
except Exception:
raise
finally:
ip2.release()
示例8: test_ipdb
def test_ipdb(self):
require_user('root')
ip = IPDB()
try:
assert ip._nl_async is False
assert len(ip.interfaces.keys()) > 1
except:
raise
finally:
ip.release()
示例9: destroyInterface
def destroyInterface(self):
ipdb = IPDB()
with ipdb.interfaces[self.bridge_iface] as i:
i.del_port(ipdb.interfaces[self.veth0])
with ipdb.interfaces[self.veth0] as i:
i.remove()
ipdb.release()
示例10: deleteService
def deleteService(name, terminalName):
if_svc_name = name
if_terminal_name = name + '_' + terminalName
ip_host = IPDB()
with ip_host.interfaces[if_terminal_name] as veth:
veth.remove()
ip_host.release()
subprocess.call(["ovs-vsctl", "del-port", "vs-" + name, if_svc_name])
subprocess.call(["ovs-vsctl", "del-port", "br0", if_terminal_name])
subprocess.call(["ovs-vsctl", "del-br", "vs-" + name])
return json.dumps({ 'status' : 'deleted service'})
示例11: _TestDhcpClient
class _TestDhcpClient(object):
def setup(self):
require_user('root')
require_executable('busybox')
self.ip = IPDB()
# create internal network
self.if1 = uifname()
self.if2 = uifname()
self.ip.create(kind='veth', ifname=self.if1, peer=self.if2).commit()
# set interfaces up
with self.ip.interfaces[self.if1] as i:
i.add_ip('172.16.101.1/24')
i.up()
with self.ip.interfaces[self.if2] as i:
i.up()
# prepare configuration for udhcpd
with open('udhcpd.conf.in', 'r') as conf_in:
with open('udhcpd.conf', 'w') as conf_out:
conf_out.write('interface %s\n' % self.if1)
conf_out.write(conf_in.read())
# run busybox dhcp server on $if1
with open(os.devnull, 'w') as fnull:
subprocess.check_call(['busybox', 'udhcpd', 'udhcpd.conf'],
stdout=fnull,
stderr=fnull)
def teardown(self):
# read pid from file and kill the server
with open('udhcpd.pid', 'r') as pid_file:
pid = int(pid_file.read())
os.kill(pid, 15)
# teardown interfaces (enough to remove only master)
self.ip.interfaces[self.if1].remove().commit()
# release IPDB
self.ip.release()
# remove configuration file
os.unlink('udhcpd.conf')
# collect garbage
gc.collect()
def test_defaults(self):
msg = dhclient.action(self.if2)
assert msg['yiaddr'].startswith('172.16.101.')
assert msg['op'] == BOOTREPLY
assert msg['options']['message_type'] == DHCPACK
assert msg['options']['router'] == ['172.16.101.1']
assert msg['options']['server_id'] == '172.16.101.1'
assert msg['options']['subnet_mask'] == '255.255.255.0'
assert set(msg['options']['name_server']) ==\
set(('172.16.101.1', '172.16.101.2'))
示例12: initalize
def initalize(self):
ip = IPDB()
try:
# Check if public interface is up
self.addr = __getIPv4Addr__(list(ip.interfaces.br1.ipaddr))
self.addr = self.addr[0], str(self.addr[1])
self.interfaces.append(('br1', self.addr))
except Exception as e:
print(e)
finally:
ip.release()
self.range_buckets[int(self.addr[0].split(".")[-1])] = 1
return self.addr
示例13: test_move_ns_pid
def test_move_ns_pid(self):
foo = str(uuid4())
bar = str(uuid4())
ifA = uifname()
netnsmod.create(foo)
netnsmod.create(bar)
ns_foo = IPDB(nl=NetNS(foo))
ns_bar = IPDB(nl=NetNS(bar))
try:
ns_foo.create(ifname=ifA, kind='dummy').commit()
with ns_foo.interfaces[ifA] as iface:
iface.net_ns_pid = ns_bar.nl.server.pid
assert ifA in ns_bar.interfaces.keys()
assert ifA not in ns_foo.interfaces.keys()
with ns_bar.interfaces[ifA] as iface:
iface.net_ns_pid = ns_foo.nl.server.pid
assert ifA not in ns_bar.interfaces.keys()
assert ifA in ns_foo.interfaces.keys()
finally:
ns_foo.release()
ns_bar.release()
netnsmod.remove(foo)
netnsmod.remove(bar)
示例14: __createNetns
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)
示例15: initalize
def initalize(self):
# check if our setup already exists
self.__ovs_setup()
ip = IPDB()
try:
# Check if public interface is up
self.addr = __getIPv4Addr__(list(ip.interfaces.cygnet0.ipaddr))
self.addr = self.addr[0], str(self.addr[1])
#self.interfaces.append(('cygnet0', self.addr))
except Exception as e:
raise e
finally:
ip.release()
self.range_buckets[int(self.addr[0].split(".")[-1])] = 1
return self.addr