Add virDomainCheckpoint APIs
[libvirt-python/ericb.git] / examples / domipaddrs.py
blobd6d5cacf749dc47cbf5863a41dff94c352296b15
1 #!/usr/bin/env python
2 # domipaddrs - print domain interfaces along with their MAC and IP addresses
4 import libvirt
5 import sys
7 def usage():
8 print "Usage: %s [URI] DOMAIN" % sys.argv[0]
9 print " Print domain interfaces along with their MAC and IP addresses"
11 uri = None
12 name = None
13 args = len(sys.argv)
15 if args == 2:
16 name = sys.argv[1]
17 elif args == 3:
18 uri = sys.argv[1]
19 name = sys.argv[2]
20 else:
21 usage()
22 sys.exit(2)
24 conn = libvirt.open(uri)
25 if conn == None:
26 print "Unable to open connection to libvirt"
27 sys.exit(1)
29 try:
30 dom = conn.lookupByName(name)
31 except libvirt.libvirtError:
32 print "Domain %s not found" % name
33 sys.exit(0)
35 ifaces = dom.interfaceAddresses(libvirt.VIR_DOMAIN_INTERFACE_ADDRESSES_SRC_LEASE);
36 if (ifaces == None):
37 print "Failed to get domain interfaces"
38 sys.exit(0)
40 print " {0:10} {1:20} {2:12} {3}".format("Interface", "MAC address", "Protocol", "Address")
42 def toIPAddrType(addrType):
43 if addrType == libvirt.VIR_IP_ADDR_TYPE_IPV4:
44 return "ipv4"
45 elif addrType == libvirt.VIR_IP_ADDR_TYPE_IPV6:
46 return "ipv6"
48 for (name, val) in ifaces.iteritems():
49 if val['addrs']:
50 for addr in val['addrs']:
51 print " {0:10} {1:19}".format(name, val['hwaddr']),
52 print " {0:12} {1}/{2} ".format(toIPAddrType(addr['type']), addr['addr'], addr['prefix']),
53 print
54 else:
55 print " {0:10} {1:19}".format(name, val['hwaddr']),
56 print " {0:12} {1}".format("N/A", "N/A"),
57 print