本文整理汇总了Python中pyroute2.IPRoute.link_down方法的典型用法代码示例。如果您正苦于以下问题:Python IPRoute.link_down方法的具体用法?Python IPRoute.link_down怎么用?Python IPRoute.link_down使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pyroute2.IPRoute
的用法示例。
在下文中一共展示了IPRoute.link_down方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: TestIPRoute
# 需要导入模块: from pyroute2 import IPRoute [as 别名]
# 或者: from pyroute2.IPRoute import link_down [as 别名]
#.........这里部分代码省略.........
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=2048)
assert grep('ip route show table 2048',
pattern='172.16.1.0/24.*172.16.0.1')
remove_link('bala')
def test_symbolic_flags_ifaddrmsg(self):
require_user('root')
self.ip.link('set', index=self.ifaces[0], state='up')
self.ip.addr('add', self.ifaces[0], '172.16.1.1', 24)
addr = [x for x in self.ip.get_addr()
if x.get_attr('IFA_LOCAL') == '172.16.1.1'][0]
assert 'IFA_F_PERMANENT' in addr.flags2names(addr['flags'])
def test_symbolic_flags_ifinfmsg(self):
require_user('root')
self.ip.link('set', index=self.ifaces[0], flags=['IFF_UP'])
iface = self.ip.get_links(self.ifaces[0])[0]
assert iface['flags'] & 1
assert 'IFF_UP' in iface.flags2names(iface['flags'])
self.ip.link('set', index=self.ifaces[0], flags=['!IFF_UP'])
assert not (self.ip.get_links(self.ifaces[0])[0]['flags'] & 1)
def test_updown_link(self):
require_user('root')
try:
self.ip.link_up(*self.ifaces)
except NetlinkError:
pass
assert self.ip.get_links(*self.ifaces)[0]['flags'] & 1
try:
self.ip.link_down(*self.ifaces)
except NetlinkError:
pass
assert not (self.ip.get_links(*self.ifaces)[0]['flags'] & 1)
def test_callbacks_positive(self):
require_user('root')
dev = self.ifaces[0]
self.cb_counter = 0
self.ip.register_callback(_callback,
lambda x: x.get('index', None) == dev,
(self, ))
self.test_updown_link()
assert self.cb_counter > 0
self.ip.unregister_callback(_callback)
def test_callbacks_negative(self):
require_user('root')
self.cb_counter = 0
self.ip.register_callback(_callback,
lambda x: x.get('index', None) == -1,
(self, ))
self.test_updown_link()
assert self.cb_counter == 0
self.ip.unregister_callback(_callback)
def test_rename_link(self):
require_user('root')
dev = self.ifaces[0]
try:
self.ip.link_rename(dev, 'bala')
except NetlinkError:
pass
assert len(self.ip.link_lookup(ifname='bala')) == 1
try:
self.ip.link_rename(dev, self.dev)
except NetlinkError:
pass
assert len(self.ip.link_lookup(ifname=self.dev)) == 1
def test_rules(self):
assert len(get_ip_rules('-4')) == \
len(self.ip.get_rules(socket.AF_INET))
assert len(get_ip_rules('-6')) == \
len(self.ip.get_rules(socket.AF_INET6))
def test_addr(self):
assert len(get_ip_addr()) == len(self.ip.get_addr())
def test_links(self):
assert len(get_ip_link()) == len(self.ip.get_links())
def test_one_link(self):
lo = self.ip.get_links(1)[0]
assert lo.get_attr('IFLA_IFNAME') == 'lo'
def test_default_routes(self):
assert len(get_ip_default_routes()) == \
len(self.ip.get_default_routes(family=socket.AF_INET, table=254))
def test_routes(self):
assert len(get_ip_route()) == \
len(self.ip.get_routes(family=socket.AF_INET, table=255))
示例2: TestIPRoute
# 需要导入模块: from pyroute2 import IPRoute [as 别名]
# 或者: from pyroute2.IPRoute import link_down [as 别名]
#.........这里部分代码省略.........
self.ip.addr('add', self.ifaces[0], address='172.16.0.2', mask=24)
self.ip.route('add',
dst='172.16.1.0',
mask=24,
gateway='172.16.0.1',
table=2048)
assert grep('ip route show table 2048',
pattern='172.16.1.0/24.*172.16.0.1')
remove_link('bala')
def test_symbolic_flags_ifaddrmsg(self):
require_user('root')
self.ip.link('set', index=self.ifaces[0], state='up')
self.ip.addr('add', self.ifaces[0], '172.16.1.1', 24)
addr = [x for x in self.ip.get_addr()
if x.get_attr('IFA_LOCAL') == '172.16.1.1'][0]
assert 'IFA_F_PERMANENT' in addr.flags2names(addr['flags'])
def test_symbolic_flags_ifinfmsg(self):
require_user('root')
self.ip.link('set', index=self.ifaces[0], flags=['IFF_UP'])
iface = self.ip.get_links(self.ifaces[0])[0]
assert iface['flags'] & 1
assert 'IFF_UP' in iface.flags2names(iface['flags'])
self.ip.link('set', index=self.ifaces[0], flags=['!IFF_UP'])
assert not (self.ip.get_links(self.ifaces[0])[0]['flags'] & 1)
def test_updown_link(self):
require_user('root')
try:
self.ip.link_up(*self.ifaces)
except NetlinkError:
pass
assert self.ip.get_links(*self.ifaces)[0]['flags'] & 1
try:
self.ip.link_down(*self.ifaces)
except NetlinkError:
pass
assert not (self.ip.get_links(*self.ifaces)[0]['flags'] & 1)
def test_callbacks_positive(self):
require_user('root')
dev = self.ifaces[0]
self.cb_counter = 0
self.ip.register_callback(_callback,
lambda x: x.get('index', None) == dev,
(self, ))
self.test_updown_link()
assert self.cb_counter > 0
self.ip.unregister_callback(_callback)
def test_callbacks_negative(self):
require_user('root')
self.cb_counter = 0
self.ip.register_callback(_callback,
lambda x: x.get('index', None) == -1,
(self, ))
self.test_updown_link()
assert self.cb_counter == 0
self.ip.unregister_callback(_callback)
def test_rename_link(self):
require_user('root')
dev = self.ifaces[0]
try:
self.ip.link_rename(dev, 'bala')
except NetlinkError:
pass
assert len(self.ip.link_lookup(ifname='bala')) == 1
try:
self.ip.link_rename(dev, self.dev)
except NetlinkError:
pass
assert len(self.ip.link_lookup(ifname=self.dev)) == 1
def test_rules(self):
assert len(get_ip_rules('-4')) == \
len(self.ip.get_rules(socket.AF_INET))
assert len(get_ip_rules('-6')) == \
len(self.ip.get_rules(socket.AF_INET6))
def test_addr(self):
assert len(get_ip_addr()) == len(self.ip.get_addr())
def test_links(self):
assert len(get_ip_link()) == len(self.ip.get_links())
def test_one_link(self):
lo = self.ip.get_links(1)[0]
assert lo.get_attr('IFLA_IFNAME') == 'lo'
def test_default_routes(self):
assert len(get_ip_default_routes()) == \
len(self.ip.get_default_routes(family=socket.AF_INET, table=254))
def test_routes(self):
assert len(get_ip_route()) == \
len(self.ip.get_routes(family=socket.AF_INET, table=255))
示例3: TestData
# 需要导入模块: from pyroute2 import IPRoute [as 别名]
# 或者: from pyroute2.IPRoute import link_down [as 别名]
class TestData(object):
def setup(self):
create_link('dummyX', 'dummy')
self.release = Event()
self.ip = IPRoute()
self.dev = self.ip.link_lookup(ifname='dummyX')
def teardown(self):
self.ip.release()
self.release.set()
remove_link('dummyX')
remove_link('bala')
def test_add_addr(self):
require_user('root')
dev = self.dev[0]
self.ip.addr('add', dev, address='172.16.0.1', mask=24)
assert '172.16.0.1/24' in get_ip_addr()
def test_remove_link(self):
require_user('root')
create_link('bala', 'dummy')
dev = self.ip.link_lookup(ifname='bala')[0]
try:
self.ip.link_remove(dev)
except NetlinkError:
pass
assert len(self.ip.link_lookup(ifname='bala')) == 0
def test_route(self):
require_user('root')
create_link('bala', 'dummy')
dev = self.ip.link_lookup(ifname='bala')[0]
self.ip.link('set', index=dev, state='up')
self.ip.addr('add', dev, address='172.16.0.2', mask=24)
self.ip.route('add',
prefix='172.16.1.0',
mask=24,
gateway='172.16.0.1')
assert grep('ip route show', pattern='172.16.1.0/24.*172.16.0.1')
remove_link('bala')
def test_updown_link(self):
require_user('root')
dev = self.dev[0]
assert not (self.ip.get_links(dev)[0]['flags'] & 1)
try:
self.ip.link_up(dev)
except NetlinkError:
pass
assert self.ip.get_links(dev)[0]['flags'] & 1
try:
self.ip.link_down(dev)
except NetlinkError:
pass
assert not (self.ip.get_links(dev)[0]['flags'] & 1)
def test_callbacks_positive(self):
require_user('root')
dev = self.dev[0]
self.cb_counter = 0
self.ip.register_callback(_callback,
lambda e, x: x.get('index', None) == dev,
(self, ))
self.test_updown_link()
assert self.cb_counter > 0
self.ip.unregister_callback(_callback)
def test_callbacks_negative(self):
require_user('root')
self.cb_counter = 0
self.ip.register_callback(_callback,
lambda e, x: x.get('index', None) == 'bala',
(self, ))
self.test_updown_link()
assert self.cb_counter == 0
self.ip.unregister_callback(_callback)
def test_rename_link(self):
require_user('root')
dev = self.dev[0]
try:
self.ip.link_rename(dev, 'bala')
except NetlinkError:
pass
assert len(self.ip.link_lookup(ifname='bala')) == 1
try:
self.ip.link_rename(dev, 'dummyX')
except NetlinkError:
pass
assert len(self.ip.link_lookup(ifname='dummyX')) == 1
def test_rules(self):
assert len(get_ip_rules('-4')) == \
len(self.ip.get_rules(socket.AF_INET))
assert len(get_ip_rules('-6')) == \
len(self.ip.get_rules(socket.AF_INET6))
#.........这里部分代码省略.........
示例4: TestData
# 需要导入模块: from pyroute2 import IPRoute [as 别名]
# 或者: from pyroute2.IPRoute import link_down [as 别名]
class TestData(object):
def setup(self):
create_link('dummyX', 'dummy')
self.ip = IPRoute()
self.dev = self.ip.link_lookup(ifname='dummyX')
def teardown(self):
self.ip.release()
remove_link('dummyX')
remove_link('bala')
def test_nla_operators(self):
require_user('root')
dev = self.dev[0]
self.ip.addr('add', dev, address='172.16.0.1', mask=24)
self.ip.addr('add', dev, address='172.16.0.2', mask=24)
r = [x for x in self.ip.get_addr() if x['index'] == dev]
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') == 'dummyX'
assert intersection['prefixlen'] == 24
assert intersection['index'] == dev
def test_add_addr(self):
require_user('root')
dev = self.dev[0]
self.ip.addr('add', dev, address='172.16.0.1', mask=24)
assert '172.16.0.1/24' in get_ip_addr()
def test_remove_link(self):
require_user('root')
create_link('bala', 'dummy')
dev = self.ip.link_lookup(ifname='bala')[0]
try:
self.ip.link_remove(dev)
except NetlinkError:
pass
assert len(self.ip.link_lookup(ifname='bala')) == 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_route_table_2048(self):
require_user('root')
create_link('bala', 'dummy')
dev = self.ip.link_lookup(ifname='bala')[0]
self.ip.link('set', index=dev, state='up')
self.ip.addr('add', dev, address='172.16.0.2', mask=24)
self.ip.route('add',
prefix='172.16.1.0',
mask=24,
gateway='172.16.0.1',
table=2048)
assert grep('ip route show table 2048',
pattern='172.16.1.0/24.*172.16.0.1')
remove_link('bala')
def test_updown_link(self):
require_user('root')
dev = self.dev[0]
assert not (self.ip.get_links(dev)[0]['flags'] & 1)
try:
self.ip.link_up(dev)
except NetlinkError:
pass
assert self.ip.get_links(dev)[0]['flags'] & 1
try:
self.ip.link_down(dev)
except NetlinkError:
pass
assert not (self.ip.get_links(dev)[0]['flags'] & 1)
def test_callbacks_positive(self):
require_user('root')
dev = self.dev[0]
self.cb_counter = 0
self.ip.register_callback(_callback,
lambda e, x: x.get('index', None) == dev,
(self, ))
self.test_updown_link()
assert self.cb_counter > 0
self.ip.unregister_callback(_callback)
def test_callbacks_negative(self):
require_user('root')
#.........这里部分代码省略.........
示例5: TestIPRoute
# 需要导入模块: from pyroute2 import IPRoute [as 别名]
# 或者: from pyroute2.IPRoute import link_down [as 别名]
#.........这里部分代码省略.........
dst='172.16.1.0',
mask=24,
gateway='172.16.0.1',
table=2048)
assert grep('ip route show table 2048',
pattern='172.16.1.0/24.*172.16.0.1')
remove_link('bala')
def test_symbolic_flags_ifaddrmsg(self):
require_user('root')
self.ip.link('set', index=self.ifaces[0], state='up')
self.ip.addr('add', self.ifaces[0], '172.16.1.1', 24)
addr = [x for x in self.ip.get_addr()
if x.get_attr('IFA_LOCAL') == '172.16.1.1'][0]
assert 'IFA_F_PERMANENT' in addr.flags2names(addr['flags'])
def test_symbolic_flags_ifinfmsg(self):
require_user('root')
self.ip.link('set', index=self.ifaces[0], flags=['IFF_UP'])
iface = self.ip.get_links(self.ifaces[0])[0]
assert iface['flags'] & 1
assert 'IFF_UP' in iface.flags2names(iface['flags'])
self.ip.link('set', index=self.ifaces[0], flags=['!IFF_UP'])
assert not (self.ip.get_links(self.ifaces[0])[0]['flags'] & 1)
def test_updown_link(self):
require_user('root')
try:
self.ip.link_up(*self.ifaces)
except NetlinkError:
pass
assert self.ip.get_links(*self.ifaces)[0]['flags'] & 1
try:
self.ip.link_down(*self.ifaces)
except NetlinkError:
pass
assert not (self.ip.get_links(*self.ifaces)[0]['flags'] & 1)
def test_callbacks_positive(self):
require_user('root')
dev = self.ifaces[0]
self.cb_counter = 0
self.ip.register_callback(_callback,
lambda x: x.get('index', None) == dev,
(self, ))
self.test_updown_link()
assert self.cb_counter > 0
self.ip.unregister_callback(_callback)
def test_callbacks_negative(self):
require_user('root')
self.cb_counter = 0
self.ip.register_callback(_callback,
lambda x: x.get('index', None) == -1,
(self, ))
self.test_updown_link()
assert self.cb_counter == 0
self.ip.unregister_callback(_callback)
def test_link_filter(self):
l = self.ip.link('dump', ifname='lo')
assert len(l) == 1
assert l[0].get_attr('IFLA_IFNAME') == 'lo'
示例6: main
# 需要导入模块: from pyroute2 import IPRoute [as 别名]
# 或者: from pyroute2.IPRoute import link_down [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):
#.........这里部分代码省略.........