通过python创建vlan并将其添加到网络命名空间

时间:2015-11-21 23:25:46

标签: python namespaces vlan

是否可以在python中编写以下命令?

在终端中:

#create a vlan
ip link add name Lan1 link eth0 type vlan id 10
ip addr add 192.168.1.11/24 brd 192.168.1.255 dev Lan1
#create a namespace
ip netns add vnsp1
#connect the vlan with the namespace
ip link set Lan1 netns vnsp1
ip netns exec vnsp1 ifconfig Lan1 192.168.1.11/24 up
#execute a program inside the namespace
ip netns exec vnsp1 ssh bananapi@192.168.1.11

在python3.4中,我用pyroute2

成功创建了一个vlan
from pyroute2 import IPDB
from subprocess import PIPE
from pyroute2 import NetNS, NSPopen, netns

ipdb = IPDB()
link_iface = self.ipdb.interfaces['eth0']
with self.ipdb.create(kind="vlan", ifname='Lan1', link=link_iface, vlan_id=10) as i:
    i.add_ip('192.168.1.11', 24)
    i.mtu = 1400

但有没有办法将此vlan添加到命名空间并在此命名空间中执行进程?

我的方法(不起作用):

vnsp = netns.create('vnsp0')
self.ipdb.interfaces['Lan1'].net_ns_fd = vnsp_name
nsp = NSPopen('vnsp0', 'ping','-c 1','192.168.1.12', stdout=PIPE)
print("output: " + str(nsp.communicate()))

0 个答案:

没有答案