本文整理汇总了Python中pyroute2.IPRoute.close方法的典型用法代码示例。如果您正苦于以下问题:Python IPRoute.close方法的具体用法?Python IPRoute.close怎么用?Python IPRoute.close使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pyroute2.IPRoute
的用法示例。
在下文中一共展示了IPRoute.close方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: BasicTest
# 需要导入模块: from pyroute2 import IPRoute [as 别名]
# 或者: from pyroute2.IPRoute import close [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.close()
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
示例2: scan_netdevs
# 需要导入模块: from pyroute2 import IPRoute [as 别名]
# 或者: from pyroute2.IPRoute import close [as 别名]
def scan_netdevs():
scan = []
ipr = IPRoute()
try:
for part in ipr.get_links():
new_link = {}
new_link["netlink_msg"] = part
new_link["index"] = part["index"]
new_link["name"] = part.get_attr("IFLA_IFNAME")
#
# FIXME:
#
# nlmsg.get_attr() returns None if there is no
# such attribute in the NLA chain; if hwaddr is None,
# normalize_hwaddr(hwaddr) will raise AttributeError(),
# since None has no upper(). The issue is that the
# AttributeError() will be a bit unrelated to the
# root cause, and since that it will be confusing.
#
hwaddr = part.get_attr("IFLA_ADDRESS")
new_link["hwaddr"] = normalize_hwaddr(hwaddr)
scan.append(new_link)
except:
raise
finally:
ipr.close()
return scan
示例3: scan_netdevs
# 需要导入模块: from pyroute2 import IPRoute [as 别名]
# 或者: from pyroute2.IPRoute import close [as 别名]
def scan_netdevs():
scan = []
ipr = IPRoute()
try:
for part in ipr.get_links():
new_link = {}
new_link["netlink_msg"] = part
new_link["index"] = part["index"]
new_link["name"] = part.get_attr("IFLA_IFNAME")
hwaddr = part.get_attr("IFLA_ADDRESS")
if hwaddr:
new_link["hwaddr"] = normalize_hwaddr(hwaddr)
else:
new_link["hwaddr"] = None
addrs = ipr.get_addr(index=new_link["index"])
new_link["ip_addrs"] = addrs
scan.append(new_link)
except:
raise
finally:
ipr.close()
return scan
示例4: set_net
# 需要导入模块: from pyroute2 import IPRoute [as 别名]
# 或者: from pyroute2.IPRoute import close [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)
示例5: del_addr
# 需要导入模块: from pyroute2 import IPRoute [as 别名]
# 或者: from pyroute2.IPRoute import close [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
示例6: test_close
# 需要导入模块: from pyroute2 import IPRoute [as 别名]
# 或者: from pyroute2.IPRoute import close [as 别名]
def test_close(self):
ip = IPRoute()
ip.get_links()
ip.close()
# Shouldn't be able to use the socket after closing
with assert_raises(socket.error):
ip.get_links()
示例7: test_iproute
# 需要导入模块: from pyroute2 import IPRoute [as 别名]
# 或者: from pyroute2.IPRoute import close [as 别名]
def test_iproute(self):
ip = IPRoute()
try:
assert len(ip.get_links()) > 1
except:
raise
finally:
ip.close()
示例8: LoopBackAddress
# 需要导入模块: from pyroute2 import IPRoute [as 别名]
# 或者: from pyroute2.IPRoute import close [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)
示例9: getInterfaceState
# 需要导入模块: from pyroute2 import IPRoute [as 别名]
# 或者: from pyroute2.IPRoute import close [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
示例10: __init__
# 需要导入模块: from pyroute2 import IPRoute [as 别名]
# 或者: from pyroute2.IPRoute import close [as 别名]
def __init__(self):
ip = IPRoute()
pppLinksIds = ip.link_lookup(ifname=INTERFACE)
if len(pppLinksIds) > 0:
state = ip.get_links(pppLinksIds)[0].get_attr('IFLA_OPERSTATE')
if state == 'UP':
self.connect = True
self.details = INTERFACE + " interface UP"
else:
self.connect = False
self.details = INTERFACE + " exist but no UP"
ip.close()
else:
self.connect = False
self.details = INTERFACE + " interface no exist"
super(StatusSchema, self).__init__()
示例11: Server
# 需要导入模块: from pyroute2 import IPRoute [as 别名]
# 或者: from pyroute2.IPRoute import close [as 别名]
def Server(cmdch, brdch):
try:
ipr = IPRoute()
lock = ipr._sproxy.lock
ipr._s_channel = brdch
poll = select.poll()
poll.register(ipr, select.POLLIN | select.POLLPRI)
poll.register(cmdch, select.POLLIN | select.POLLPRI)
except Exception as e:
cmdch.send({'stage': 'init',
'error': e})
return 255
# all is OK so far
cmdch.send({'stage': 'init',
'error': None})
# 8<-------------------------------------------------------------
while True:
events = poll.poll()
for (fd, event) in events:
if fd == ipr.fileno():
bufsize = ipr.getsockopt(SOL_SOCKET, SO_RCVBUF) // 2
with lock:
brdch.send({'stage': 'broadcast',
'data': ipr.recv(bufsize)})
elif fd == cmdch.fileno():
cmd = cmdch.recv()
if cmd['stage'] == 'shutdown':
poll.unregister(ipr)
poll.unregister(cmdch)
ipr.close()
return
elif cmd['stage'] == 'command':
error = None
try:
ret = getattr(ipr, cmd['name'])(*cmd['argv'],
**cmd['kwarg'])
except Exception as e:
error = e
error.tb = traceback.format_exc()
cmdch.send({'stage': 'command',
'error': error,
'return': ret,
'cookie': cmd['cookie']})
示例12: TestMisc
# 需要导入模块: from pyroute2 import IPRoute [as 别名]
# 或者: from pyroute2.IPRoute import close [as 别名]
class TestMisc(object):
def setup(self):
self.ip = IPRoute()
def teardown(self):
self.ip.close()
def test_get_policy_map(self):
assert isinstance(self.ip.get_policy_map(), dict)
def test_register_policy(self):
self.ip.register_policy(100, nlmsg)
self.ip.register_policy({101: nlmsg})
self.ip.register_policy(102, nlmsg)
assert self.ip.get_policy_map()[100] == nlmsg
assert self.ip.get_policy_map(101)[101] == nlmsg
assert self.ip.get_policy_map([102])[102] == nlmsg
self.ip.unregister_policy(100)
self.ip.unregister_policy([101])
self.ip.unregister_policy({102: nlmsg})
assert 100 not in self.ip.get_policy_map()
assert 101 not in self.ip.get_policy_map()
assert 102 not in self.ip.get_policy_map()
def test_addrpool_expand(self):
# see coverage
for i in range(100):
self.ip.get_addr()
def test_nla_compare(self):
lvalue = self.ip.get_links()
rvalue = self.ip.get_links()
assert lvalue is not rvalue
if lvalue == rvalue:
pass
if lvalue != rvalue:
pass
assert lvalue != 42
示例13: test_isinstance
# 需要导入模块: from pyroute2 import IPRoute [as 别名]
# 或者: from pyroute2.IPRoute import close [as 别名]
def test_isinstance(self):
from pyroute2 import IPRSocket
from pyroute2 import IPRoute
from pyroute2.iproute import IPRoute as IPRoute_real
from pyroute2.netlink.rtnl.iprsocket import IPRSocket as IPRSocket_real
ipr1 = IPRoute()
ipr2 = IPRoute_real()
ips1 = IPRSocket()
ips2 = IPRSocket_real()
# positive
assert isinstance(ips1, IPRSocket)
assert isinstance(ips2, IPRSocket)
assert isinstance(ips1, IPRSocket_real)
assert isinstance(ips2, IPRSocket_real)
assert isinstance(ipr1, IPRoute)
assert isinstance(ipr2, IPRoute)
assert isinstance(ipr1, IPRoute_real)
assert isinstance(ipr2, IPRoute_real)
# negative
assert not isinstance(ips1, IPRoute)
assert not isinstance(ips2, IPRoute)
assert not isinstance(ips1, IPRoute_real)
assert not isinstance(ips2, IPRoute_real)
# this must succeed -- IPRoute is a subclass of IPRSocket
assert isinstance(ipr1, IPRSocket)
assert isinstance(ipr2, IPRSocket)
assert isinstance(ipr1, IPRSocket_real)
assert isinstance(ipr2, IPRSocket_real)
ips1.close()
ips2.close()
ipr1.close()
ipr2.close()
示例14: TestMisc
# 需要导入模块: from pyroute2 import IPRoute [as 别名]
# 或者: from pyroute2.IPRoute import close [as 别名]
class TestMisc(object):
def setup(self):
self.ip = IPRoute()
def teardown(self):
self.ip.close()
def test_addrpool_expand(self):
# see coverage
for i in range(100):
self.ip.get_addr()
def test_nla_compare(self):
lvalue = self.ip.get_links()
rvalue = self.ip.get_links()
assert lvalue is not rvalue
if lvalue == rvalue:
pass
if lvalue != rvalue:
pass
assert lvalue != 42
示例15: BasicTest
# 需要导入模块: from pyroute2 import IPRoute [as 别名]
# 或者: from pyroute2.IPRoute import close [as 别名]
class BasicTest(object):
def setup(self):
require_user('root')
self.ip = IPRoute()
self.ifname = uifname()
self.ip.link_create(ifname=self.ifname, kind='dummy')
self.interface = self.ip.link_lookup(ifname=self.ifname)[0]
def teardown(self):
self.ip.link('delete', index=self.interface)
self.ip.close()
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