本文整理汇总了Python中pyroute2.IPRoute.link_lookup方法的典型用法代码示例。如果您正苦于以下问题:Python IPRoute.link_lookup方法的具体用法?Python IPRoute.link_lookup怎么用?Python IPRoute.link_lookup使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pyroute2.IPRoute
的用法示例。
在下文中一共展示了IPRoute.link_lookup方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: connect_to_br
# 需要导入模块: from pyroute2 import IPRoute [as 别名]
# 或者: from pyroute2.IPRoute import link_lookup [as 别名]
def connect_to_br(self):
index = 0
with docker_netns(self.name) as pid:
for quagga_config in self.quagga_config:
ip = IPRoute()
br = ip.link_lookup(ifname=quagga_config['bridge'])
if len(br) == 0:
ip.link_create(ifname=quagga_config['bridge'], kind='bridge')
br = ip.link_lookup(ifname=quagga_config['bridge'])
br = br[0]
ip.link('set', index=br, state='up')
ifname = '{0}-{1}'.format(self.name, index)
ifs = ip.link_lookup(ifname=ifname)
if len(ifs) > 0:
ip.link_remove(ifs[0])
peer_ifname = '{0}-{1}'.format(pid, index)
ip.link_create(ifname=ifname, kind='veth', peer=peer_ifname)
host = ip.link_lookup(ifname=ifname)[0]
ip.link('set', index=host, master=br)
ip.link('set', index=host, state='up')
guest = ip.link_lookup(ifname=peer_ifname)[0]
ip.link('set', index=guest, net_ns_fd=pid)
with Namespace(pid, 'net'):
ip = IPRoute()
ip.link('set', index=guest, ifname='eth{}'.format(index+1))
ip.addr('add', index=guest, address=quagga_config['ip'], mask=quagga_config['mask'])
ip.link('set', index=guest, state='up')
index += 1
示例2: create_interface
# 需要导入模块: from pyroute2 import IPRoute [as 别名]
# 或者: from pyroute2.IPRoute import link_lookup [as 别名]
def create_interface(self):
'''
Create MACVlan interface
Creates VLAN interface first based on VLAN tag for sub-interface
then create macvlan interface above the vlan interface
'''
iproute = IPRoute()
# Ensure the host interface is present
host_if = iproute.link_lookup(ifname=self.host_ifname)
if len(host_if) == 0:
raise CniError(CNI_ERROR_ADD_VLAN_INTF,
'Error creating vlan interface' + ' host interface' +
self.host_ifname + ' not found')
return
# Create vlan interface if not present
vlan_if = iproute.link_lookup(ifname=self.vlan_ifname)
if len(vlan_if) == 0:
try:
iproute.link("add", ifname=self.vlan_ifname, kind='vlan',
vlan_id=self.vlan_tag, link=host_if[0])
except NetlinkError as e:
if e.code != errno.EEXIST:
raise CniError(CNI_ERROR_ADD_VETH,
'Error creating vlan interface. ' +
'Host interface ' + self.host_ifname +
'vlan id ' + str(self.vlan_tag) +
'vlan ifname ' + self.vlan_ifname +
' code ' + str(e.code) +
' message ' + e.message)
vlan_if = iproute.link_lookup(ifname=self.vlan_ifname)
# Create MACVlan interface if not present
cn_ifname = self.vlan_ifname + '-ns'
cn_if = iproute.link_lookup(ifname=cn_ifname)
if len(cn_if) == 0:
try:
iproute.link("add", ifname=cn_ifname, kind='macvlan',
link=vlan_if[0], macvlan_mode="vepa")
except NetlinkError as e:
if e.code != errno.EEXIST:
raise CniError(CNI_ERROR_ADD_VETH,
'Error creating macvlan interface ' +
' vlan iterface ' + self.vlan_ifname +
' macvlan interface ' + cn_ifname +
' code ' + str(e.code) +
' message ' + e.message)
cn_if = iproute.link_lookup(ifname=self.vlan_ifname)
# Move one end of pair inside container
self.move_link(cn_ifname)
return
示例3: __createNetns
# 需要导入模块: from pyroute2 import IPRoute [as 别名]
# 或者: from pyroute2.IPRoute import link_lookup [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)
示例4: TestProxyData
# 需要导入模块: from pyroute2 import IPRoute [as 别名]
# 或者: from pyroute2.IPRoute import link_lookup [as 别名]
class TestProxyData(TestData):
def setup(self):
create_link('dummyX', 'dummy')
t_url = 'unix://\0%s' % (uuid.uuid4())
p_url = 'unix://\0%s' % (uuid.uuid4())
self.connect = Event()
self.release = Event()
target = Process(target=_run_remote_uplink,
args=(t_url, self.connect, self.release))
target.daemon = True
target.start()
self.connect.wait()
self.connect.clear()
proxy = Process(target=_run_remote_uplink,
args=(p_url, self.connect, self.release))
proxy.daemon = True
proxy.start()
self.connect.wait()
self.connect.clear()
self.ip = IPRoute(do_connect=False)
link, proxy = self.ip.connect(p_url)
self.ip.register('bala', proxy)
link, host = self.ip.connect(t_url, addr=proxy)
service = self.ip.discover(self.ip.default_target, addr=host)
self.ip.default_peer = host
self.ip.default_dport = service
self.dev = self.ip.link_lookup(ifname='dummyX')
示例5: delete_veth_pair
# 需要导入模块: from pyroute2 import IPRoute [as 别名]
# 或者: from pyroute2.IPRoute import link_lookup [as 别名]
def delete_veth_pair(name):
ip = IPRoute()
peers = ('{}0'.format(name), '{}1'.format(name))
LOG.info('deleting veth pair {}'.format(peers))
link_down(peers[0])
link_down(peers[1])
ip.link('del', index=ip.link_lookup(ifname=peers[0])[0])
示例6: TestProxyData
# 需要导入模块: from pyroute2 import IPRoute [as 别名]
# 或者: from pyroute2.IPRoute import link_lookup [as 别名]
class TestProxyData(TestData):
def setup(self):
create_link('dummyX', 'dummy')
t_url = 'unix://\0%s' % (uuid.uuid4())
p_url = 'unix://\0%s' % (uuid.uuid4())
self.uplink = IPRoute()
self.uplink.serve(t_url)
self.proxy = IPRoute(host=t_url)
self.proxy.serve(p_url)
self.ip = IPRoute(host=p_url)
service = self.ip.discover(self.ip.default_target,
addr=self.proxy.default_peer)
self.ip.default_peer = self.proxy.default_peer
self.ip.default_dport = service
self.dev = self.ip.link_lookup(ifname='dummyX')
def teardown(self):
TestData.teardown(self)
self.proxy.release()
self.uplink.release()
示例7: BasicTest
# 需要导入模块: from pyroute2 import IPRoute [as 别名]
# 或者: from pyroute2.IPRoute import link_lookup [as 别名]
class BasicTest(object):
def setup(self):
require_user('root')
self.ip = IPRoute()
self.ip.link('add',
index=0,
ifname='dummyX',
linkinfo={'attrs': [['IFLA_INFO_KIND', 'dummy']]})
self.interface = self.ip.link_lookup(ifname='dummyX')[0]
def teardown(self):
self.ip.link('delete', index=self.interface)
self.ip.release()
def get_qdiscs(self):
return [x for x in self.ip.get_qdiscs() if
x['index'] == self.interface]
def get_qdisc(self):
# get qdiscs list and filter out our interface
qds = self.get_qdiscs()
if qds:
return qds[0]
else:
return None
示例8: set_net
# 需要导入模块: from pyroute2 import IPRoute [as 别名]
# 或者: from pyroute2.IPRoute import link_lookup [as 别名]
def set_net(lease):
ipr = IPRoute()
try:
index = ipr.link_lookup(ifname=lease.interface)[0]
except IndexError as e:
logger.error('Interface %s not found, can not set IP.',
lease.interface)
try:
ipr.addr('add', index, address=lease.address,
mask=int(lease.subnet_mask_cidr))
except NetlinkError as e:
if ipr.get_addr(index=index)[0].\
get_attrs('IFA_ADDRESS')[0] == lease.address:
logger.debug('Interface %s is already set to IP %s' %
(lease.interface, lease.address))
else:
logger.error(e)
else:
logger.debug('Interface %s set to IP %s' %
(lease.interface, lease.address))
try:
ipr.route('add', dst='0.0.0.0', gateway=lease.router, oif=index)
except NetlinkError as e:
if ipr.get_routes(table=254)[0].\
get_attrs('RTA_GATEWAY')[0] == lease.router:
logger.debug('Default gateway is already set to %s' %
(lease.router))
else:
logger.error(e)
else:
logger.debug('Default gateway set to %s', lease.router)
ipr.close()
set_dns(lease)
示例9: start_ue
# 需要导入模块: from pyroute2 import IPRoute [as 别名]
# 或者: from pyroute2.IPRoute import link_lookup [as 别名]
def start_ue () :
#print 'Enter your commands below.\r\nInsert "exit" to leave the application.'
timeout=60 #timeout in seconds
send_command('AT', 'OK' , timeout)
send_command('AT+CFUN=1' , 'OK' , timeout)
#send_command('AT+CGATT=0' , 'OK' , timeout)
send_command('AT+CGATT=1','OK', 300)
#os.system('wvdial -C ' + bandrich_ppd_config + ' &' )
thread_ppp = pppThread(1, "ppp_thread", 1)
thread_ppp.start()
iface='ppp0'
while 1:
time.sleep ( 2)
#Now we check if ppp0 interface is up and running
try:
if exit_flag == 1:
break
ip = IPRoute()
idx = ip.link_lookup(ifname=iface)[0]
os.system ('route add 192.172.0.1 ppp0')
os.system ('ping -c 5 192.172.0.1')
break
except Exception, e:
error = ' Interface ' + iface + 'does not exist...'
error = error + ' In function: ' + sys._getframe().f_code.co_name + ': *** Caught exception: ' + str(e.__class__) + " : " + str( e)
error = error + traceback.format_exc()
print error
示例10: set_dns_systemd_resolved
# 需要导入模块: from pyroute2 import IPRoute [as 别名]
# 或者: from pyroute2.IPRoute import link_lookup [as 别名]
def set_dns_systemd_resolved(lease):
# NOTE: if systemd-resolved is not already running, we might not want to
# run it in case there's specific system configuration for other resolvers
ipr = IPRoute()
index = ipr.link_lookup(ifname=lease.interface)[0]
# Construct the argument to pass to DBUS.
# the equivalent argument for:
# busctl call org.freedesktop.resolve1 /org/freedesktop/resolve1 \
# org.freedesktop.resolve1.Manager SetLinkDNS 'ia(iay)' 2 1 2 4 1 2 3 4
# is SetLinkDNS(2, [(2, [8, 8, 8, 8])]_
iay = [(2, [int(b) for b in ns.split('.')])
for ns in lease.name_server.split()]
# if '.' in ns
# else (10, [ord(x) for x in
# socket.inet_pton(socket.AF_INET6, ns)])
bus = SystemBus()
resolved = bus.get_object('org.freedesktop.resolve1',
'/org/freedesktop/resolve1')
manager = Interface(resolved,
dbus_interface='org.freedesktop.resolve1.Manager')
try:
manager.SetLinkDNS(index, iay)
return True
except DBusException as e:
logger.error(e)
return False
示例11: del_addr
# 需要导入模块: from pyroute2 import IPRoute [as 别名]
# 或者: from pyroute2.IPRoute import link_lookup [as 别名]
def del_addr(self, ip):
iproute= IPRoute()
br = iproute.link_lookup(ifname=BRIDGE_INTERFACE_NAME)[0]
if not self.check_exists(ip, iproute, br):
iproute.addr('delete', br, address=ip, mask=30)
iproute.close()
iproute = None
示例12: LoopBackAddress
# 需要导入模块: from pyroute2 import IPRoute [as 别名]
# 或者: from pyroute2.IPRoute import link_lookup [as 别名]
class LoopBackAddress(object):
def __init__(self, logger, config):
self.config = config
self._label = self.config.get("balancer.agent.plugins.loopback", "label")
self._prefix = self.config.getint("balancer.agent.plugins.loopback", "prefix")
self._ip = IPRoute()
self._idx = self._ip.link_lookup(ifname="lo")[0]
self.logger = logger
@property
def configured_ips(self):
ips = set()
for link in self._ip.get_addr(index=self._idx):
if not "attrs" in link:
continue
interface = None
ip = None
for key, value in link["attrs"]:
if key == "IFA_ADDRESS":
ip = value
elif key == "IFA_LABEL":
interface = value
elif interface == self._label and ip not in ["127.0.0.1", None]:
ips.add(ip)
return ips
def close(self):
if self._ip:
self._ip.close()
def __disable__(self, ip):
self.logger("Disabling {}/{} on {}".format(ip, self._prefix, self._label))
self._ip.addr("del", index=self._idx, address=ip, prefixlen=self._prefix, label=self._label)
def __enable__(self, ip):
self.logger("Enabling {}/{} on {}".format(ip, self._prefix, self._label))
self._ip.addr("add", index=self._idx, address=ip, prefixlen=self._prefix, label=self._label)
def apply(self, frontends):
configured_ips = self.configured_ips
ips = set()
for frontend, ips_and_ports in frontends.items():
for ip, port in ips_and_ports:
ips.add(ip)
to_disabled = configured_ips.difference(ips)
to_enabled = ips.difference(configured_ips)
for ip in to_disabled:
self.__disable__(ip)
for ip in to_enabled:
self.__enable__(ip)
示例13: getInterfaceState
# 需要导入模块: from pyroute2 import IPRoute [as 别名]
# 或者: from pyroute2.IPRoute import link_lookup [as 别名]
def getInterfaceState(ifname):
try:
ip = IPRoute()
state = ip.get_links(ip.link_lookup(ifname=ifname))[0].get_attr('IFLA_OPERSTATE')
ip.close()
except Exception as e:
raise Exception("getInterfaceState: Collecting interface status for %s failed: %s" % (ifname,str(e)))
else:
if state == "UP":
return True
return False
示例14: create_interface
# 需要导入模块: from pyroute2 import IPRoute [as 别名]
# 或者: from pyroute2.IPRoute import link_lookup [as 别名]
def create_interface(self):
'''
Create veth-pair
Creates veth-pair in the container-namespace first and then moves
one end of the pair to host-os namespace
'''
# If the host end of veth is already present in host-os, it means
# interface create was already done. There is nothing to do
iproute = IPRoute()
iface = iproute.link_lookup(ifname=self.host_ifname)
if len(iface) != 0:
return
host_ifindex = None
with CniNamespace(self.cni.container_netns):
# Create veth pairs if not already created inside namespace
# One end of pair is named host_ifname and the other end of pair
# is set a container_ifname
ns_iproute = IPRoute()
ns_iface = ns_iproute.link_lookup(ifname=self.cni.container_ifname)
if len(ns_iface) == 0:
try:
ns_iproute.link_create(ifname=self.cni.container_ifname,
peer=self.host_ifname, kind='veth',
address=self.container_mac)
except NetlinkError as e:
if e.code != errno.EEXIST:
raise Error(CNI_ERROR_VETH_ADD,
'Error creating veth device ' +
self.host_ifname + ' code ' +
str(e.code) + ' message ' + e.message)
# We must move the host part of veth pair to host-namespace
# Get the interface-index. We will move to host namespace once
# we exit container-name space and go to host-namespace
host_ifindex = ns_iproute.link_lookup(ifname=self.host_ifname)[0]
if host_ifindex is not None:
ns_iproute.link('set', index=host_ifindex, net_ns_pid=self.pid)
return
示例15: get_link
# 需要导入模块: from pyroute2 import IPRoute [as 别名]
# 或者: from pyroute2.IPRoute import link_lookup [as 别名]
def get_link(self):
'''
Get link information for the interface inside the container
'''
link = None
with CniNamespace(self.container_netns, logger):
iproute = IPRoute()
iface = iproute.link_lookup(ifname=self.container_ifname)
if len(iface) != 0:
idx = iface[0]
link = iproute.link("get", index=idx)
return link