mirror of
https://gitlab.com/libvirt/libvirt-python.git
synced 2025-07-25 04:58:57 +03:00
examples: Refactor domipaddrs
This patch makes domipaddrs example compatible with Python3. Signed-off-by: Radostin Stoyanov <rstoyanov@fedoraproject.org>
This commit is contained in:
@ -4,55 +4,59 @@
|
|||||||
import libvirt
|
import libvirt
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
def usage():
|
IPTYPE = {
|
||||||
print "Usage: %s [URI] DOMAIN" % sys.argv[0]
|
libvirt.VIR_IP_ADDR_TYPE_IPV4: "ipv4",
|
||||||
print " Print domain interfaces along with their MAC and IP addresses"
|
libvirt.VIR_IP_ADDR_TYPE_IPV6: "ipv6",
|
||||||
|
}
|
||||||
|
|
||||||
uri = None
|
|
||||||
name = None
|
|
||||||
args = len(sys.argv)
|
|
||||||
|
|
||||||
if args == 2:
|
def print_dom_ifaces(dom):
|
||||||
name = sys.argv[1]
|
ifaces = dom.interfaceAddresses(libvirt.VIR_DOMAIN_INTERFACE_ADDRESSES_SRC_LEASE)
|
||||||
elif args == 3:
|
if (ifaces == None):
|
||||||
uri = sys.argv[1]
|
print("Failed to get domain interfaces")
|
||||||
name = sys.argv[2]
|
sys.exit(0)
|
||||||
else:
|
|
||||||
usage()
|
|
||||||
sys.exit(2)
|
|
||||||
|
|
||||||
try:
|
print(" {0:10} {1:20} {2:12} {3}".format(
|
||||||
conn = libvirt.open(uri)
|
"Interface", "MAC address", "Protocol", "Address"))
|
||||||
except libvirt.libvirtError:
|
|
||||||
print "Unable to open connection to libvirt"
|
|
||||||
sys.exit(1)
|
|
||||||
|
|
||||||
try:
|
for (name, val) in ifaces.items():
|
||||||
dom = conn.lookupByName(name)
|
if val['addrs']:
|
||||||
except libvirt.libvirtError:
|
for addr in val['addrs']:
|
||||||
print "Domain %s not found" % name
|
print (" {0:10} {1:19} {2:12} {3}/{4}".format(
|
||||||
sys.exit(0)
|
name,
|
||||||
|
val['hwaddr'],
|
||||||
|
IPTYPE[addr['type']],
|
||||||
|
addr['addr'],
|
||||||
|
addr['prefix']))
|
||||||
|
else:
|
||||||
|
print(" {0:10} {1:19} {2:12} {3}".format(name, val['hwaddr'], "N/A", "N/A"))
|
||||||
|
|
||||||
ifaces = dom.interfaceAddresses(libvirt.VIR_DOMAIN_INTERFACE_ADDRESSES_SRC_LEASE);
|
|
||||||
if (ifaces == None):
|
|
||||||
print "Failed to get domain interfaces"
|
|
||||||
sys.exit(0)
|
|
||||||
|
|
||||||
print " {0:10} {1:20} {2:12} {3}".format("Interface", "MAC address", "Protocol", "Address")
|
if __name__ == "__main__":
|
||||||
|
uri = None
|
||||||
|
name = None
|
||||||
|
args = len(sys.argv)
|
||||||
|
|
||||||
def toIPAddrType(addrType):
|
if args == 2:
|
||||||
if addrType == libvirt.VIR_IP_ADDR_TYPE_IPV4:
|
name = sys.argv[1]
|
||||||
return "ipv4"
|
elif args == 3:
|
||||||
elif addrType == libvirt.VIR_IP_ADDR_TYPE_IPV6:
|
uri = sys.argv[1]
|
||||||
return "ipv6"
|
name = sys.argv[2]
|
||||||
|
|
||||||
for (name, val) in ifaces.iteritems():
|
|
||||||
if val['addrs']:
|
|
||||||
for addr in val['addrs']:
|
|
||||||
print " {0:10} {1:19}".format(name, val['hwaddr']),
|
|
||||||
print " {0:12} {1}/{2} ".format(toIPAddrType(addr['type']), addr['addr'], addr['prefix']),
|
|
||||||
print
|
|
||||||
else:
|
else:
|
||||||
print " {0:10} {1:19}".format(name, val['hwaddr']),
|
print("Usage: %s [URI] DOMAIN" % sys.argv[0])
|
||||||
print " {0:12} {1}".format("N/A", "N/A"),
|
print(" Print domain interfaces along with their MAC and IP addresses")
|
||||||
print
|
sys.exit(2)
|
||||||
|
|
||||||
|
try:
|
||||||
|
conn = libvirt.open(uri)
|
||||||
|
except libvirt.libvirtError:
|
||||||
|
raise SystemExit("Unable to open connection to libvirt")
|
||||||
|
|
||||||
|
try:
|
||||||
|
dom = conn.lookupByName(name)
|
||||||
|
except libvirt.libvirtError:
|
||||||
|
print("Domain %s not found" % name)
|
||||||
|
sys.exit(0)
|
||||||
|
|
||||||
|
print_dom_ifaces(dom)
|
||||||
|
conn.close()
|
||||||
|
Reference in New Issue
Block a user