访问远程libvirtd服务
因为是在一个可信环境中运行,所以可以忽略安全方面的操作,步骤如下:
(1)更改libvirtd配置
1.1 更改/ect/sysconfig/libvirtd文件,打开LIBVIRTD_ARGS="--listen"设置
1.2 更改/etc/libvirt/libvirtd.conf
listen_tls = 0 #关闭tls
listen_tcp = 1 # 打开tcp
tcp_port = "16509" # 侦听关口
listen_addr="0.0.0.0" # 侦听地址
auth_tcp = "none" # 允许匿名访问
(2) 可以通过 virsh -c qemu+tcp://10.2.3.123:16509/system来访问10.2.3.123上面的libvirtd服务
(3) python代码示例如下:
import libvirt import sys import logging ''' enum virDomainState { VIR_DOMAIN_NOSTATE = 0 no state VIR_DOMAIN_RUNNING = 1 the domain is running VIR_DOMAIN_BLOCKED = 2 the domain is blocked on resource VIR_DOMAIN_PAUSED = 3 the domain is paused by user VIR_DOMAIN_SHUTDOWN = 4 the domain is being shut down VIR_DOMAIN_SHUTOFF = 5 the domain is shut off VIR_DOMAIN_CRASHED = 6 the domain is crashed VIR_DOMAIN_PMSUSPENDED = 7 the domain is suspended by guest power management VIR_DOMAIN_LAST = 8 NB: this enum value will increase over time as new events are added to the libvirt API. It reflects the last state supported by this version of the libvirt API. } struct virDomainInfo { unsigned char state the running state, one of virDomainState unsigned long maxMem the maximum memory in KBytes allowed unsigned long memory the memory in KBytes used by the domain unsigned short nrVirtCpu the number of virtual CPUs for the domain unsigned long long cpuTime the CPU time used in nanoseconds } ''' def get_VM_infos(host, port=16509): ''' list domains of the specified host ''' infos = [] try: pass uri = 'qemu+tcp://%s:%s/system' % (host, port) conn = libvirt.openReadOnly(uri) # list the defined but inactive domains domains = [conn.lookupByName(name) for name in conn.listDefinedDomains()] # list active domains domains.extend([conn.lookupByID(i) for i in conn.listDomainsID()]) # for dom in domains: # print 'ID = %d' % dom.ID() # print 'Name = %s' % dom.name() # infos = dom.info() # print 'State = %d' % infos[0] # # print 'State = %s' %s dom.state(0) # print 'Max Memory = %s' % infos[1] # print 'Memory = %s' % info[2] # # print 'Max Memory = %d' % dom.maxMemory # print 'Number of virt CPUs = %d' % infos[3] # # print 'Number of virt CPUs = %s' % dom.maxVcpus() # print 'CPU Time (in ns) = %d' % infos[4] # print ' ' for dom in domains: infos.append(dom.info()) except Exception, e: print e return infos if __name__ == '__main__': infos = get_VM_infos('10.2.3.250', 16509) print infos