71 lines
2.0 KiB
Python
71 lines
2.0 KiB
Python
from app import *
|
|
import ipaddress
|
|
from xml.dom import minidom
|
|
|
|
def diff_net(min_dhcp,max_dhcp):
|
|
ip1 = ipaddress.IPv4Address(min_dhcp)
|
|
ip2 = ipaddress.IPv4Address(max_dhcp)
|
|
nb_ip = int(ip2) - int(ip1) + 1
|
|
return nb_ip
|
|
|
|
def get_int(net_name):
|
|
network = conn.networkLookupByName(net_name)
|
|
net_int = network.bridgeName()
|
|
return net_int
|
|
|
|
def create_vswitch_int(net_int):
|
|
cmd="ovs-vsctl add-br "+net_int
|
|
subprocess.call(cmd, shell=True)
|
|
|
|
def delete_network(net_name,net_int):
|
|
cmd = "ovs-vsctl del-br "+net_int
|
|
subprocess.call(cmd, shell=True)
|
|
network = conn.networkLookupByName(net_name) #Lapin
|
|
network.destroy()
|
|
network.undefine()
|
|
cmd2="kill $(cat /run/dhypecp_"+net_int+".pid)"
|
|
subprocess.call(cmd2, shell=True)
|
|
cmd3="rm -f /run/dhypecp_"+net_int+".pid"
|
|
subprocess.call(cmd3, shell=True)
|
|
|
|
def create_network(net_name,net_int):
|
|
create_vswitch_int(net_int)
|
|
xml = f"""
|
|
<network>
|
|
<name>{ net_name }</name>
|
|
<forward mode='bridge'/>
|
|
<bridge name='{ net_int }'/>
|
|
<virtualport type='openvswitch'/>
|
|
</network>
|
|
"""
|
|
conn.networkDefineXML(xml)
|
|
net_use = conn.networkLookupByName(net_name)
|
|
net_use.create()
|
|
net_use.setAutostart(True)
|
|
|
|
def list_network_VM():
|
|
net_tree = []
|
|
net_na = []
|
|
#Host Interfaces
|
|
net_hard_int = conn.listInterfaces()
|
|
#Virtual Interfaces
|
|
net_list = conn.listAllNetworks()
|
|
for net_name in net_list:
|
|
if net_name.isActive():
|
|
net_sub_tree=[]
|
|
net_sub_tree.append(net_name.name())
|
|
net_use = conn.networkLookupByName(net_name.name())
|
|
ports = net_use.listAllPorts()
|
|
net_ports = []
|
|
for port in ports:
|
|
raw_xml = port.XMLDesc()
|
|
net_xml = minidom.parseString(raw_xml)
|
|
net_element = net_xml.documentElement
|
|
net_VM = net_element.getElementsByTagName("name")[0]
|
|
net_ports.append(net_VM.firstChild.data)
|
|
net_sub_tree.append(net_ports)
|
|
net_tree.append(net_sub_tree)
|
|
else:
|
|
net_na.append(net_name.name())
|
|
return net_hard_int,net_tree,net_na
|