博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
access remote libvirtd
阅读量:7296 次
发布时间:2019-06-30

本文共 2767 字,大约阅读时间需要 9 分钟。

访问远程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 libvirtimport sysimport logging'''enum virDomainState {VIR_DOMAIN_NOSTATE    =     0    no stateVIR_DOMAIN_RUNNING    =     1    the domain is runningVIR_DOMAIN_BLOCKED    =     2    the domain is blocked on resourceVIR_DOMAIN_PAUSED    =     3    the domain is paused by userVIR_DOMAIN_SHUTDOWN    =     4    the domain is being shut downVIR_DOMAIN_SHUTOFF    =     5    the domain is shut offVIR_DOMAIN_CRASHED    =     6    the domain is crashedVIR_DOMAIN_PMSUSPENDED    =     7    the domain is suspended by guest power managementVIR_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 virDomainStateunsigned long    maxMem    the maximum memory in KBytes allowedunsigned long    memory    the memory in KBytes used by the domainunsigned short    nrVirtCpu    the number of virtual CPUs for the domainunsigned 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 infosif __name__ == '__main__':    infos = get_VM_infos('10.2.3.250', 16509)    print infos

 

转载于:https://www.cnblogs.com/Jerryshome/p/3484204.html

你可能感兴趣的文章
【cocos2d-x 024】 LINK : fatal error LNK1123: 转换到 COFF 期间失败: 文件无效或损坏
查看>>
概述C# Cast()
查看>>
LeetCode - 9. Palindrome Number
查看>>
IOS的 new Date()格式化问题
查看>>
sharepoint webpart 获取文档库下的所有文件夹名
查看>>
java数据结构和算法--------第六章
查看>>
关于php如何连贯操作类方法(以数据库为例)
查看>>
25th SEP, 要好好的走下去
查看>>
BZOJ4822[Cqoi2017]老C的任务——树状数组(二维数点)
查看>>
BZOJ1299[LLH邀请赛]巧克力棒——Nim游戏+搜索
查看>>
未能为 SSL/TLS 安全通道建立信任的解决办法
查看>>
cmake是什么
查看>>
使用MASM10(变量的使用)- Win32汇编语言018
查看>>
【Docker学习笔记】----基于centos 7 的Docker安装
查看>>
Android笔记之OnLongClickListener
查看>>
Java客户端:调用EyeKey HTTP接口进行人脸对比
查看>>
SQL之分区函数
查看>>
创业公司如何实施敏捷开发
查看>>
Django使用AJAX调用自己写的API接口
查看>>
数据科学求职准备
查看>>