虚拟网络可以使用networkLookupByName,networkLookupByUUID,networkLookupByUUIDString和listNetworks这几种方法来发现,可以用下面几种例子展示。
import sys
import libvirt
conn = None
try:
conn = libvirt.open("qemu:///system")
except libvirt.libvirtError as e:
print(repr(e), file=sys.stderr)
exit(1)
# discover all the virtual networks
networks = conn.listNetworks()
print('Virtual networks:')
for network in networks:
print(' '+network)
print()
# lookup the default network by name
network = conn.networkLookupByName('default')
print('Virtual network default:')
print(' name: '+network.name())
uuid = network.UUIDString()
print(' UUID: '+uuid)
print(' bridge: '+network.bridgeName())
print()
# lookup the default network by name
network = conn.networkLookupByUUIDString(uuid)
print('Virtual network default:')
print(' name: '+network.name())
print(' UUID: '+network.UUIDString())
print(' bridge: '+network.bridgeName())
conn.close()
exit(0)
import sys
import libvirt
xml = """
<network>
<name>mynetwork</name>
<bridge name="virbr1" />
<forward mode="nat"/>
<ip address="192.168.142.1" netmask="255.255.255.0">
<dhcp>
<range start="192.168.142.2" end="192.168.142.254" />
</dhcp>
</ip>
</network>"""
conn = None
try:
conn = libvirt.open("qemu:///system")
except libvirt.libvirtError as e:
print(repr(e), file=sys.stderr)
exit(1)
# create a persistent virtual network
network = conn.networkDefineXML(xml)
if network == None:
print('Failed to create a virtual network', file=sys.stderr)
exit(1)
active = network.isActive()
if active == 1:
print('The new persistent virtual network is active')
else:
print('The new persistent virtual network is not active')
network.create() # set the network active
active = network.isActive()
if active == 1:
print('The new transient virtual network is active')
else:
print('The new transient virtual network is not active')
# now destroy the persistent virtual network
network.destroy()
# erase the persistent virtual network
network.undefine()
print()
# create a transient virtual network
network = conn.networkCreateXML(xml)
if network == None:
print('Failed to define a virtual network', file=sys.stderr)
exit(1)
active = network.isActive()
if active == 1:
print('The new transient virtual network is active')
else:
print('The new transient virtual network is not active')
# now destroy the transient virtual network
network.destroy()
conn.close()
exit(0)
在这里插入代码片
```import sys
import libvirt
conn = None
try:
conn = libvirt.open("qemu:///system")
except libvirt.libvirtError as e:
print(repr(e), file=sys.stderr)
exit(1)
# lookup the default network by name
network = conn.networkLookupByName('default')
print('Virtual network default:')
print(' name: '+network.name())
print(' UUID: '+network.UUIDString())
print(' bridge: '+network.bridgeName())
print(' autostart: '+str(network.autostart()))
print(' is active: '+str(network.isActive()))
print(' is persistent: '+str(network.isPersistent()))
print()
print('Unsetting autostart')
network.setAutostart(0)
print(' autostart: '+str(network.autostart()))
print('Setting autostart')
network.setAutostart(1)
print(' autostart: '+str(network.autostart()))
print()
xml = network.XMLDesc(0)
print('XML description:')
print(xml)
conn.close()
exit(0)